-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMonitorDataExchangeTests.cs
More file actions
283 lines (222 loc) · 12.3 KB
/
Copy pathMonitorDataExchangeTests.cs
File metadata and controls
283 lines (222 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using HealthMonitoring.Model;
using HealthMonitoring.Monitors.Core.Exchange;
using HealthMonitoring.Monitors.Core.Exchange.Client;
using HealthMonitoring.Monitors.Core.Registers;
using HealthMonitoring.TestUtils.Awaitable;
using Moq;
using Xunit;
namespace HealthMonitoring.Monitors.Core.UnitTests
{
public class MonitorDataExchangeTests
{
private readonly IHealthMonitorRegistry _monitorRegistry;
private readonly Mock<IHealthMonitorExchangeClient> _exchangeClient;
private readonly Mock<IMonitorableEndpointRegistry> _endpointRegistry;
private readonly AwaitableFactory _awaitableFactory;
private static readonly TimeSpan TestMaxWaitTime = TimeSpan.FromSeconds(5);
private static readonly TimeSpan PostRunDelay = TimeSpan.FromMilliseconds(100);
private const string MonitorTypeName = "test";
public MonitorDataExchangeTests()
{
_awaitableFactory = new AwaitableFactory();
_monitorRegistry = new HealthMonitorRegistry(new[] { Mock.Of<IHealthMonitor>(cfg => cfg.Name == MonitorTypeName) });
_exchangeClient = new Mock<IHealthMonitorExchangeClient>();
_endpointRegistry = new Mock<IMonitorableEndpointRegistry>();
}
private MonitorDataExchange CreateExchange()
{
return CreateExchange(new DataExchangeConfig(1024, 64, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1), EndpointMetadata.DefaultMonitorTag));
}
private MonitorDataExchange CreateExchange(DataExchangeConfig config)
{
return new MonitorDataExchange(_monitorRegistry, _exchangeClient.Object, _endpointRegistry.Object, config);
}
[Fact]
public async Task Exchange_should_try_upload_supported_monitor_types_on_start_until_they_succeeds()
{
var countdown = new AsyncCountdown(nameof(IHealthMonitorExchangeClient.RegisterMonitorsAsync), 1);
SetupExchangeClient(
c => c.RegisterMonitorsAsync(It.IsAny<IEnumerable<string>>(), It.IsAny<CancellationToken>()),
() => { throw new InvalidOperationException(); },
() => { throw new InvalidOperationException(); },
() => _awaitableFactory.Return().WithCountdown(countdown).RunAsync()
);
SetupDefaultEndpointIdentitiesMock();
using (CreateExchange(new DataExchangeConfig(100, 10, TimeSpan.FromMilliseconds(1), TimeSpan.FromSeconds(1), EndpointMetadata.DefaultMonitorTag)))
await countdown.WaitAsync(TestMaxWaitTime);
}
[Fact]
public async Task Exchange_should_retry_uploading_supported_monitor_types_until_disposal()
{
var countdown = new AsyncCountdown(nameof(IHealthMonitorExchangeClient.RegisterMonitorsAsync), 10);
var counter = new AsyncCounter();
_exchangeClient
.Setup(c => c.RegisterMonitorsAsync(It.IsAny<IEnumerable<string>>(), It.IsAny<CancellationToken>()))
.Returns(() => _awaitableFactory.Throw(new InvalidOperationException()).WithCountdown(countdown).WithCounter(counter).RunAsync());
SetupDefaultEndpointIdentitiesMock();
using (CreateExchange(new DataExchangeConfig(100, 10, TimeSpan.FromMilliseconds(1), TimeSpan.FromSeconds(1), EndpointMetadata.DefaultMonitorTag)))
await countdown.WaitAsync(TestMaxWaitTime);
var capturedCalls = counter.Value;
Assert.True(capturedCalls > 0, "capturedCalls>0");
await Task.Delay(PostRunDelay);
Assert.Equal(capturedCalls, counter.Value);
}
[Fact]
public async Task Exchange_should_download_list_of_endpoints()
{
SetupDefaultRegisterEndpointsMock();
var endpointIdentitiesGetCountdown = new AsyncCountdown(nameof(IHealthMonitorExchangeClient.GetEndpointIdentitiesAsync), 1);
var ids = SetupDefaultEndpointIdentitiesMock(cfg => cfg.WithCountdown(endpointIdentitiesGetCountdown));
using (CreateExchange())
await endpointIdentitiesGetCountdown.WaitAsync(TimeSpan.FromSeconds(5));
_endpointRegistry.Verify(r => r.UpdateEndpoints(ids));
}
[Fact]
public async Task Exchange_should_try_download_list_of_endpoints_periodically()
{
SetupDefaultRegisterEndpointsMock();
var endpointIdentities1 = new[] { new EndpointIdentity(Guid.NewGuid(), MonitorTypeName, "address1"), new EndpointIdentity(Guid.NewGuid(), MonitorTypeName, "address2") };
var endpointIdentities2 = new[] { new EndpointIdentity(Guid.NewGuid(), MonitorTypeName, "address1"), new EndpointIdentity(Guid.NewGuid(), MonitorTypeName, "address3") };
var countdown1 = new AsyncCountdown("endpointIdentities1", 1);
var countdown2 = new AsyncCountdown("endpointIdentities2", 1);
var monitorTag = "some_tag";
SetupExchangeClient(c => c.GetEndpointIdentitiesAsync(monitorTag, It.IsAny<CancellationToken>()),
() => { throw new InvalidOperationException(); },
() => _awaitableFactory.Return(endpointIdentities1).WithCountdown(countdown1).RunAsync(),
() => { throw new InvalidOperationException(); },
() => _awaitableFactory.Return(endpointIdentities2).WithCountdown(countdown2).RunAsync());
using (CreateExchange(new DataExchangeConfig(100, 10, TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(1), monitorTag)))
{
await countdown1.WaitAsync(TestMaxWaitTime);
await countdown2.WaitAsync(TestMaxWaitTime);
}
}
[Fact]
public async Task Exchange_should_try_download_list_of_endpoints_periodically_until_disposal()
{
var countdown = new AsyncCountdown(nameof(IHealthMonitorExchangeClient.RegisterMonitorsAsync), 10);
var counter = new AsyncCounter();
SetupDefaultRegisterEndpointsMock();
_exchangeClient.Setup(c => c.GetEndpointIdentitiesAsync(EndpointMetadata.DefaultMonitorTag, It.IsAny<CancellationToken>()))
.Returns(() => _awaitableFactory.Throw<EndpointIdentity[]>(new InvalidOperationException()).WithCountdown(countdown).WithCounter(counter).RunAsync());
using (CreateExchange(new DataExchangeConfig(100, 10, TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(1), EndpointMetadata.DefaultMonitorTag)))
await countdown.WaitAsync(TestMaxWaitTime);
var capturedCalls = counter.Value;
Assert.True(capturedCalls > 0, "capturedCalls>0");
await Task.Delay(PostRunDelay);
Assert.Equal(capturedCalls, counter.Value);
}
[Fact]
public async Task Exchange_should_upload_monitor_healths_in_buckets()
{
SetupDefaultRegisterEndpointsMock();
var identity = SetupDefaultEndpointIdentitiesMock().First();
int bucketCount = 10;
int bucketSize = 10;
var uploads = new List<int>();
var countdown = new AsyncCountdown(nameof(IHealthMonitorExchangeClient.UploadHealthAsync), bucketCount);
SetupDefaultHealthUploadMock(x => { uploads.Add(x.Length); countdown.Decrement(); });
var healths = PrepareEndpointHealths(bucketSize * bucketCount);
using (var ex = CreateExchange(new DataExchangeConfig(bucketSize * bucketCount, bucketSize, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1), EndpointMetadata.DefaultMonitorTag)))
{
foreach (var health in healths)
ex.UpdateHealth(identity.Id, health);
await countdown.WaitAsync(TestMaxWaitTime);
}
Assert.Equal(new[] { bucketSize }, uploads.Distinct());
}
[Fact]
public async Task Exchange_should_retry_health_update_send_until_disposal()
{
var countdown = new AsyncCountdown(nameof(IHealthMonitorExchangeClient.UploadHealthAsync), 10);
SetupDefaultRegisterEndpointsMock();
var identity = SetupDefaultEndpointIdentitiesMock().First();
int calls = 0;
SetupDefaultHealthUploadMock(x =>
{
Interlocked.Increment(ref calls);
countdown.Decrement();
if (calls > 1)
throw new InvalidOperationException();
});
var healths = PrepareEndpointHealths(2);
using (var ex = CreateExchange(new DataExchangeConfig(100, 1, TimeSpan.FromMilliseconds(1), TimeSpan.FromSeconds(1), EndpointMetadata.DefaultMonitorTag)))
{
foreach (var health in healths)
ex.UpdateHealth(identity.Id, health);
await countdown.WaitAsync(TestMaxWaitTime);
}
var capturedCalls = calls;
Assert.True(capturedCalls > 0, "capturedCalls>0");
await Task.Delay(PostRunDelay);
Assert.Equal(capturedCalls, calls);
}
[Fact]
public async Task Exchange_should_retry_health_update_send_in_case_of_errors()
{
var count = 10;
var countdown = new AsyncCountdown(nameof(IHealthMonitorExchangeClient.UploadHealthAsync), count);
SetupDefaultRegisterEndpointsMock();
var identity = SetupDefaultEndpointIdentitiesMock().First();
var attempts = 10;
SetupDefaultHealthUploadMock(updates =>
{
if (attempts-- > 0)
throw new Exception();
foreach (var update in updates)
countdown.Decrement();
});
var config = new DataExchangeConfig(100, count, TimeSpan.FromMilliseconds(1), TimeSpan.FromSeconds(1), EndpointMetadata.DefaultMonitorTag);
var healths = PrepareEndpointHealths(count);
using (var ex = CreateExchange(config))
{
foreach (var health in healths)
ex.UpdateHealth(identity.Id, health);
await countdown.WaitAsync(TestMaxWaitTime);
}
}
private void SetupDefaultHealthUploadMock(Action<EndpointHealthUpdate[]> action)
{
_exchangeClient.Setup(c => c.UploadHealthAsync(It.IsAny<EndpointHealthUpdate[]>(), It.IsAny<CancellationToken>()))
.Returns((EndpointHealthUpdate[] u, CancellationToken c) =>
{
action(u);
return Task.FromResult(0);
});
}
private EndpointIdentity[] SetupDefaultEndpointIdentitiesMock(Func<AwaitableBuilder<EndpointIdentity[]>, AwaitableBuilder<EndpointIdentity[]>> mockConfiguration)
{
var endpointIdentities = new[] { new EndpointIdentity(Guid.NewGuid(), MonitorTypeName, "address1"), new EndpointIdentity(Guid.NewGuid(), MonitorTypeName, "address2") };
_exchangeClient
.Setup(c => c.GetEndpointIdentitiesAsync(EndpointMetadata.DefaultMonitorTag, It.IsAny<CancellationToken>()))
.Returns(() => mockConfiguration(_awaitableFactory.Return(endpointIdentities)).RunAsync());
return endpointIdentities;
}
private EndpointIdentity[] SetupDefaultEndpointIdentitiesMock()
{
return SetupDefaultEndpointIdentitiesMock(cfg => cfg);
}
private void SetupDefaultRegisterEndpointsMock()
{
_exchangeClient.Setup(c => c.RegisterMonitorsAsync(It.IsAny<IEnumerable<string>>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(0));
}
private void SetupExchangeClient<T>(Expression<Func<IHealthMonitorExchangeClient, T>> clientExpression, params Func<T>[] actions)
{
var queue = new Queue<Func<T>>(actions);
_exchangeClient.Setup(clientExpression).Returns(() => queue.Dequeue().Invoke());
}
private static EndpointHealth[] PrepareEndpointHealths(int count)
{
return Enumerable.Range(0, count)
.Select(i => new EndpointHealth(DateTime.Today, TimeSpan.FromMilliseconds(i), EndpointStatus.Healthy))
.ToArray();
}
}
}