-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathKafkaJsonSourceTests.cs
More file actions
441 lines (409 loc) · 14.2 KB
/
KafkaJsonSourceTests.cs
File metadata and controls
441 lines (409 loc) · 14.2 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
using System.Diagnostics;
using System.Dynamic;
using System.Text.Json;
using System.Threading.Tasks.Dataflow;
using ALE.ETLBox.Common.ControlFlow;
using ALE.ETLBox.DataFlow;
using Confluent.Kafka;
using ETLBox.Primitives;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
using Moq;
using Xunit.Abstractions;
using CancellationTokenSource = System.Threading.CancellationTokenSource;
namespace ETLBox.Kafka.Tests;
public partial class KafkaJsonSourceTests : IClassFixture<KafkaFixture>
{
private readonly KafkaFixture _fixture;
private readonly ITestOutputHelper _output;
private string TopicName { get; } = $"test-{Guid.NewGuid()}";
private ConsumerConfig GetConsumerConfig(bool enablePartitionEof, string? topicName = null)
{
return new ConsumerConfig
{
BootstrapServers = _fixture.BootstrapAddress,
GroupId = $"{topicName ?? TopicName}-group",
AutoOffsetReset = AutoOffsetReset.Earliest,
EnablePartitionEof = enablePartitionEof,
SocketTimeoutMs = 1000,
};
}
public KafkaJsonSourceTests(KafkaFixture fixture, ITestOutputHelper output)
{
_fixture = fixture;
_output = output;
ControlFlow.LoggerFactory = new LoggerFactory(
new[] { new TestOutputLoggerProvider(_output) }
);
}
[Fact]
public void ShouldProduceAndConsumeDirectlyToKafka()
{
// Arrange
const string jsonString = "{\"name\":\"test\"}";
// Act
ProduceJson(jsonString);
var result = ConsumeJson(true, CancellationToken.None).ToArray();
// Assert
Assert.Single(result);
Assert.Equal(jsonString, result[0]);
}
[Fact]
public void ShouldProduceAndConsumeDirectlyToKafkaWithMultipleTopics()
{
// Arrange
var preTopic = $"{TopicName}-pre";
ProduceJson("{\"name\":\"direct-test-pre\"}", preTopic); // Add first message synchronously to create topic
ProduceJson("{\"name\":\"direct-test-0\"}"); // Add first message synchronously to create topic
ProduceJson("{\"name\":\"direct-test-1\"}");
var timeout = TimeSpan.FromSeconds(1.0);
// Act
var preResults = ConsumeJson(true, CancellationToken.None, preTopic).ToList();
var watch = new Stopwatch();
watch.Start();
using var tokenSource = new CancellationTokenSource(timeout);
var cancellationToken = tokenSource.Token;
var results = ConsumeJson(false, cancellationToken).ToList();
watch.Stop();
// Assert
Assert.InRange(watch.ElapsedMilliseconds, timeout.TotalMilliseconds, 10000);
Assert.Single(preResults);
Assert.Equal(2, results.Count);
}
[Fact]
public void ShouldReadDynamicObject()
{
// Arrange
const string jsonString =
"{\"name\": \"test\", \"true\": true, \"false\": false, \"null\": null, \"array\": [1,2,3], \"object\": {\"key\": \"value\"}}";
ProduceJson(jsonString);
var (block, target) = SetupMockTarget();
target.Setup(t => t.TargetBlock).Returns(block.Object);
var kafkaSource = new KafkaJsonSource<ExpandoObject>
{
ConsumerConfig = GetConsumerConfig(true),
Topic = TopicName,
};
kafkaSource.LinkTo(target.Object);
// Act
kafkaSource.Execute();
// Assert
block.Verify(b =>
b.OfferMessage(
It.IsAny<DataflowMessageHeader>(),
It.IsAny<ExpandoObject>(),
It.IsAny<ISourceBlock<ExpandoObject>>(),
It.IsAny<bool>()
)
);
dynamic result = block.Invocations[0].Arguments[1];
Assert.Equivalent(
new Dictionary<string, object?>()
{
["name"] = "test",
["true"] = true,
["false"] = false,
["null"] = null,
["array"] = new[] { 1, 2, 3 },
["object"] = new Dictionary<string, object>() { ["key"] = "value" },
},
result
);
}
[Fact]
public void ShouldProduceErrorOnInvalidJson()
{
// Arrange
const string jsonString = "{\"name\": test\"}";
ProduceJson(jsonString);
var (block, target) = SetupMockTarget();
target.Setup(t => t.TargetBlock).Returns(block.Object);
var errorTarget = new Mock<IDataFlowLinkTarget<ETLBoxError>>();
var errorTargetBlock = new Mock<ITargetBlock<ETLBoxError>>();
errorTarget.Setup(x => x.TargetBlock).Returns(errorTargetBlock.Object);
var kafkaSource = new KafkaJsonSource<ExpandoObject>
{
ConsumerConfig = GetConsumerConfig(true),
Topic = TopicName,
};
kafkaSource.LinkTo(target.Object);
kafkaSource.LinkErrorTo(errorTarget.Object);
// Act
kafkaSource.Execute();
// Assert
block.Verify(
b =>
b.OfferMessage(
It.IsAny<DataflowMessageHeader>(),
It.IsAny<ExpandoObject>(),
It.IsAny<ISourceBlock<ExpandoObject>>(),
It.IsAny<bool>()
),
Times.Never
);
errorTarget.Verify(t => t.TargetBlock);
errorTargetBlock.Verify(
b =>
b.OfferMessage(
It.IsAny<DataflowMessageHeader>(),
It.Is<ETLBoxError>(e =>
e.Exception is JsonException && e.RecordAsJson == jsonString
),
It.IsAny<ISourceBlock<ETLBoxError>>(),
It.IsAny<bool>()
),
Times.Once
);
}
[UsedImplicitly]
public record TestRecord(
string Name,
int IntValue,
bool BoolValue,
DateTime DateValue,
string CamelCase
);
[Fact]
public void ShouldReadTypedObject()
{
// Arrange
const string jsonString =
@"{
""name"":""test"",
""intValue"": 1,
""boolValue"": true,
""dateValue"": ""2021-01-01T00:00:00"",
""camelCase"": ""test""
}";
ProduceJson(jsonString);
var block = new Mock<ITargetBlock<TestRecord>>();
var target = new Mock<IDataFlowDestination<TestRecord>>();
target.Setup(t => t.TargetBlock).Returns(block.Object);
var kafkaSource = new KafkaJsonSource<TestRecord>
{
ConsumerConfig = GetConsumerConfig(true),
Topic = TopicName,
JsonSerializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
},
};
kafkaSource.LinkTo(target.Object);
// Act
kafkaSource.Execute();
// Assert
block.Verify(b =>
b.OfferMessage(
It.IsAny<DataflowMessageHeader>(),
It.Is(
new TestRecord(
"test",
1,
true,
new DateTime(2021, 1, 1, 0, 0, 0, DateTimeKind.Local),
"test"
),
EqualityComparer<TestRecord>.Default
),
It.IsAny<ISourceBlock<TestRecord>>(),
It.IsAny<bool>()
)
);
}
[Fact]
public void ShouldReadMultipleObjects()
{
// Arrange
var (block, target) = SetupMockTarget();
var kafkaSource = new KafkaJsonSource<ExpandoObject>
{
ConsumerConfig = GetConsumerConfig(true),
Topic = TopicName,
};
kafkaSource.LinkTo(target.Object);
// Act
for (var i = 0; i < 10; i++)
{
ProduceJson($"{{\"name\":\"test{i}\"}}");
}
kafkaSource.Execute();
target.Object.Wait();
// Assert
block.Verify(
b =>
b.OfferMessage(
It.IsAny<DataflowMessageHeader>(),
It.IsAny<ExpandoObject>(),
It.IsAny<ISourceBlock<ExpandoObject>>(),
It.IsAny<bool>()
),
Times.Exactly(10)
);
}
[Fact]
public void ShouldReadContinuously()
{
// Arrange
ProduceJson("{\"name\":\"test0\"}"); // Add first message synchronously to create topic
var generator = Task.Run(async () =>
{
for (var i = 1; i < 10; i++)
{
await Task.Delay(100, CancellationToken.None).ConfigureAwait(false);
ProduceJson($"{{\"name\":\"test{i}\"}}");
_output.WriteLine($"Produced test {i} to topic {TopicName}");
}
});
using var tokenSource = new CancellationTokenSource();
var offerMessageInvokeCounter = 0;
var (block, target) = SetupMockTarget(() =>
{
offerMessageInvokeCounter++;
if (offerMessageInvokeCounter >= 10)
{
try
{
tokenSource.Cancel();
}
catch (ObjectDisposedException)
{
// ignore
}
}
});
var kafkaSource = new TestKafkaJsonSource(_output)
{
ConsumerConfig = GetConsumerConfig(false),
Topic = TopicName,
};
kafkaSource.LinkTo(target.Object);
// Act
var timer = Stopwatch.StartNew();
var executeTask = kafkaSource.ExecuteAsync(tokenSource.Token);
var destinationTask = target.Object.Completion;
// Assert
_output.WriteLine("Waiting for completion...");
var e = Record.Exception(() => Task.WaitAll(executeTask, generator, destinationTask));
Assert.Multiple(
() => Assert.IsType<TaskCanceledException>((e as AggregateException)?.InnerException),
() => Assert.Equal(TaskStatus.Canceled, executeTask.Status),
() => Assert.Null(generator.Exception),
() => Assert.Null(destinationTask.Exception)
);
timer.Stop();
// Should take longer than timeout (which is more than generation time)
Assert.InRange(timer.ElapsedMilliseconds, 1500, 300000);
block.Verify(
b =>
b.OfferMessage(
It.IsAny<DataflowMessageHeader>(),
It.IsAny<ExpandoObject>(),
It.IsAny<ISourceBlock<ExpandoObject>>(),
It.IsAny<bool>()
),
Times.Exactly(10)
);
}
private (
Mock<ITargetBlock<ExpandoObject>> block,
Mock<IDataFlowDestination<ExpandoObject>> target
) SetupMockTarget(Action? doOnInvokeOfferMessage = null)
{
var block = new Mock<ITargetBlock<ExpandoObject>>();
block
.Setup(b =>
b.OfferMessage(
It.IsAny<DataflowMessageHeader>(),
It.IsAny<ExpandoObject>(),
It.IsAny<ISourceBlock<ExpandoObject>>(),
It.IsAny<bool>()
)
)
.Returns(
(
DataflowMessageHeader _,
ExpandoObject messageValue,
ISourceBlock<ExpandoObject> _,
bool _
) =>
{
_output.WriteLine($"Received message {messageValue}");
doOnInvokeOfferMessage?.Invoke();
return DataflowMessageStatus.Accepted;
}
);
var target = new Mock<IDataFlowDestination<ExpandoObject>>();
target.Setup(t => t.TargetBlock).Returns(block.Object);
return (block, target);
}
private IEnumerable<string> ConsumeJson(
bool enablePartitionEof,
CancellationToken cancellationToken,
string? topicName = null
)
{
using var consumer = new ConsumerBuilder<Ignore, string>(
GetConsumerConfig(enablePartitionEof, topicName)
).Build();
_output.WriteLine($"Subscribing to topic {topicName ?? TopicName}...");
consumer.Subscribe(topicName ?? TopicName);
while (true)
{
ConsumeResult<Ignore, string> consumeResult;
try
{
consumeResult = consumer.Consume(cancellationToken);
_output.WriteLine(
$"Consumed direct message {consumeResult.Message?.Value ?? "null"}"
);
}
catch (OperationCanceledException)
{
break;
}
if (consumeResult.IsPartitionEOF || consumeResult.Message is null)
{
break;
}
yield return consumeResult.Message.Value;
}
}
private void ProduceJson(string jsonString, string? topicName = null)
{
var config = new ProducerConfig
{
BootstrapServers = _fixture.BootstrapAddress,
SocketTimeoutMs = 1000,
};
using var producer = new ProducerBuilder<Null, string>(config).Build();
var message = new Message<Null, string> { Value = jsonString };
_output.WriteLine(
$"Producing message {message.Value} to topic {topicName ?? TopicName}..."
);
producer.Produce(topicName ?? TopicName, message);
for (var i = 0; i < 10; i++)
{
if (producer.Flush(TimeSpan.FromMilliseconds(100)) == 0)
break;
if (i == 9)
throw new TimeoutException("Flush operation timed out after 10 attempts");
Thread.Sleep(500);
}
}
}
public class TestKafkaJsonSource : KafkaJsonSource<ExpandoObject>
{
private readonly ITestOutputHelper _output;
public TestKafkaJsonSource(ITestOutputHelper output)
{
_output = output;
}
protected override ExpandoObject? ConvertToOutputValue(
string kafkaValue,
Action<Exception, string>? logRowOnError = null
)
{
_output.WriteLine($"Converting message {kafkaValue}");
return base.ConvertToOutputValue(kafkaValue, logRowOnError);
}
}