-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathboth.spec.js
726 lines (586 loc) · 20.2 KB
/
both.spec.js
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/*
* node-rdkafka - Node.js wrapper for RdKafka C/C++ library
*
* Copyright (c) 2016 Blizzard Entertainment
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE.txt file for details.
*/
var crypto = require('crypto');
var t = require('assert');
var Kafka = require('../');
var kafkaBrokerList = process.env.KAFKA_HOST || 'localhost:9092';
var eventListener = require('./listener');
var topic = 'test';
var topic2 = 'test2';
describe('Consumer/Producer', function() {
var producer;
var consumer;
beforeEach(function(done) {
var finished = 0;
var called = false;
function maybeDone(err) {
if (called) {
return;
}
finished++;
if (err) {
called = true;
return done(err);
}
if (finished === 2) {
done();
}
}
var grp = 'kafka-mocha-grp-' + crypto.randomBytes(20).toString('hex');
consumer = new Kafka.KafkaConsumer({
'metadata.broker.list': kafkaBrokerList,
'group.id': grp,
'fetch.wait.max.ms': 1000,
'session.timeout.ms': 10000,
'enable.auto.commit': true,
'enable.partition.eof': true,
'debug': 'all'
// paused: true,
}, {
'auto.offset.reset': 'largest'
});
consumer.connect({}, function(err, d) {
t.ifError(err);
t.equal(typeof d, 'object', 'metadata should be returned');
maybeDone(err);
});
eventListener(consumer);
producer = new Kafka.Producer({
'client.id': 'kafka-mocha',
'metadata.broker.list': kafkaBrokerList,
'fetch.wait.max.ms': 1,
'debug': 'all',
'dr_cb': true
}, {
'produce.offset.report': true
});
producer.connect({}, function(err, d) {
t.ifError(err);
t.equal(typeof d, 'object', 'metadata should be returned');
maybeDone(err);
});
eventListener(producer);
});
afterEach(function(done) {
var finished = 0;
var called = false;
function maybeDone(err) {
if (called) {
return;
}
finished++;
if (err) {
called = true;
return done(err);
}
if (finished === 2) {
done();
}
}
consumer.disconnect(function(err) {
maybeDone(err);
});
producer.disconnect(function(err) {
maybeDone(err);
});
});
it('should be able to produce, consume messages, read position: subscribe/consumeOnce', function(done) {
crypto.randomBytes(4096, function(ex, buffer) {
producer.setPollInterval(10);
var offset;
producer.once('delivery-report', function(err, report) {
t.ifError(err);
offset = report.offset;
});
consumer.setDefaultConsumeTimeout(10);
consumer.subscribe([topic]);
var ct;
var consumeOne = function() {
consumer.consume(1, function(err, messages) {
if (err && err.code === -185) {
ct = setTimeout(consumeOne, 100);
return;
} else if (messages.length === 0 || (err && err.code === -191)) {
producer.produce(topic, null, buffer, null);
ct = setTimeout(consumeOne, 100);
return;
} else if (err) {
return;
}
var message = messages[0];
t.equal(Array.isArray(consumer.assignments()), true, 'Assignments should be an array');
t.equal(consumer.assignments().length > 0, true, 'Should have at least one assignment');
t.equal(buffer.toString(), message.value.toString(),
'message is not equal to buffer');
// test consumer.position as we have consumed
var position = consumer.position();
t.equal(position.length, 1);
t.deepStrictEqual(position[0].partition, 0);
t.ok(position[0].offset >= 0);
done();
});
};
// Consume until we get it or time out
consumeOne();
});
});
it('should return ready messages on partition EOF', function(done) {
crypto.randomBytes(4096, function(ex, buffer) {
producer.setPollInterval(10);
producer.once('delivery-report', function(err, report) {
t.ifError(err);
});
consumer.subscribe([topic]);
var consumeAll = function() {
// Make sure we get the message fast when consuming with large timeout
consumer.setDefaultConsumeTimeout(1000000);
consumer.consume(100000, function(err, messages) {
t.ifError(err);
t.equal(messages.length, 1);
done();
});
};
var consumeNone = function() {
// With no new messages, the consume should wait whole timeout
var start = Date.now();
// Set the timeout to 2000ms to see that it actually waits the whole time
// (Needs to be higher than fetch.max.wait.ms which is 1000 here
// to ensure we don't only wait that long)
consumer.setDefaultConsumeTimeout(2000);
consumer.consume(100000, function(err, messages) {
t.ifError(err);
t.ok(Date.now() - start >= 1998);
t.equal(messages.length, 0);
// Produce one message to cause EOF with waiting message when consuming all
producer.produce(topic, null, buffer, null);
consumeAll();
});
};
consumeNone();
});
});
it('should emit partition.eof event when reaching end of partition', function(done) {
crypto.randomBytes(4096, function(ex, buffer) {
producer.setPollInterval(10);
producer.once('delivery-report', function(err, report) {
t.ifError(err);
});
consumer.subscribe([topic]);
var events = [];
consumer.once('data', function(msg) {
events.push("data");
});
consumer.once('partition.eof', function(eof) {
events.push("partition.eof");
});
setTimeout(function() {
producer.produce(topic, null, buffer, null);
}, 500)
consumer.setDefaultConsumeTimeout(2000);
consumer.consume(1000, function(err, messages) {
t.ifError(err);
t.equal(messages.length, 1);
t.deepStrictEqual(events, ["data", "partition.eof"]);
done();
});
});
});
it('should emit partition.eof when already at end of partition', function(done) {
crypto.randomBytes(4096, function(ex, buffer) {
producer.setPollInterval(10);
producer.once('delivery-report', function(err, report) {
t.ifError(err);
});
consumer.subscribe([topic]);
var events = [];
consumer.once('data', function(msg) {
events.push("data");
});
consumer.on('partition.eof', function(eof) {
events.push("partition.eof");
});
setTimeout(function() {
producer.produce(topic, null, buffer, null);
}, 2000)
consumer.setDefaultConsumeTimeout(3000);
consumer.consume(1000, function(err, messages) {
t.ifError(err);
t.equal(messages.length, 1);
t.deepStrictEqual(events, ["partition.eof", "data", "partition.eof"]);
done();
});
});
});
it('should be able to produce and consume messages: consumeLoop', function(done) {
var key = 'key';
crypto.randomBytes(4096, function(ex, buffer) {
producer.setPollInterval(10);
producer.once('delivery-report', function(err, report) {
if (!err) {
t.equal(topic, report.topic, 'invalid delivery-report topic');
t.equal(key, report.key, 'invalid delivery-report key');
t.ok(report.offset >= 0, 'invalid delivery-report offset');
}
});
consumer.on('data', function(message) {
t.equal(buffer.toString(), message.value.toString(), 'invalid message value');
t.equal(key, message.key, 'invalid message key');
t.equal(topic, message.topic, 'invalid message topic');
t.ok(message.offset >= 0, 'invalid message offset');
done();
});
consumer.subscribe([topic]);
consumer.consume();
setTimeout(function() {
producer.produce(topic, null, buffer, key);
}, 2000);
});
});
it('should emit \'partition.eof\' events in consumeLoop', function(done) {
crypto.randomBytes(4096, function(ex, buffer) {
producer.setPollInterval(10);
producer.once('delivery-report', function(err, report) {
t.ifError(err);
});
var events = [];
var offsets = [];
consumer.on('data', function(message) {
t.equal(message.topic, topic);
t.equal(message.partition, 0);
offsets.push(message.offset);
events.push('data');
});
consumer.on('partition.eof', function(eofEvent) {
t.equal(eofEvent.topic, topic);
t.equal(eofEvent.partition, 0);
offsets.push(eofEvent.offset);
events.push('partition.eof');
});
consumer.subscribe([topic]);
consumer.consume();
setTimeout(function() {
producer.produce(topic, null, buffer);
}, 2000);
setTimeout(function() {
producer.produce(topic, null, buffer);
}, 4000);
setTimeout(function() {
t.deepStrictEqual(events, ['partition.eof', 'data', 'partition.eof', 'data', 'partition.eof']);
var startOffset = offsets[0];
t.deepStrictEqual(offsets,
[ startOffset,
startOffset,
startOffset + 1,
startOffset + 1,
startOffset + 2 ]);
done();
}, 6000);
});
});
it('should emit [warning] event on UNKNOWN_TOPIC_OR_PART error: consumeLoop', function(done) {
consumer.on('warning', function (err) {
if (err.code === Kafka.CODES.ERRORS.ERR_UNKNOWN_TOPIC_OR_PART) {
consumer.disconnect(function() {
done();
});
} else {
t.ifError(err);
}
});
consumer.subscribe(['non_existing_topic']);
consumer.consume();
});
it('should be able to produce and consume messages with one header value as string: consumeLoop', function(done) {
var headers = [
{ key: "value" }
];
run_headers_test(done, headers);
});
it('should be able to produce and consume messages with one header value as buffer: consumeLoop', function(done) {
var headers = [
{ key: Buffer.from('value') }
];
run_headers_test(done, headers);
});
it('should be able to produce and consume messages with one header value as int: consumeLoop', function(done) {
var headers = [
{ key: 10 }
];
run_headers_test(done, headers);
});
it('should be able to produce and consume messages with one header value as float: consumeLoop', function(done) {
var headers = [
{ key: 1.11 }
];
run_headers_test(done, headers);
});
it('should be able to produce and consume messages with multiple headers value as buffer: consumeLoop', function(done) {
var headers = [
{ key1: Buffer.from('value1') },
{ key2: Buffer.from('value2') },
{ key3: Buffer.from('value3') },
{ key4: Buffer.from('value4') },
];
run_headers_test(done, headers);
});
it('should be able to produce and consume messages with multiple headers value as string: consumeLoop', function(done) {
var headers = [
{ key1: 'value1' },
{ key2: 'value2' },
{ key3: 'value3' },
{ key4: 'value4' },
];
run_headers_test(done, headers);
});
it('should be able to produce and consume messages with multiple headers with mixed values: consumeLoop', function(done) {
var headers = [
{ key1: 'value1' },
{ key2: Buffer.from('value2') },
{ key3: 100 },
{ key4: 10.1 },
];
run_headers_test(done, headers);
});
it('should be able to produce and consume messages: empty buffer key and empty value', function(done) {
var emptyString = '';
var key = Buffer.from(emptyString);
var value = Buffer.from('');
producer.setPollInterval(10);
consumer.once('data', function(message) {
t.notEqual(message.value, null, 'message should not be null');
t.equal(value.toString(), message.value.toString(), 'invalid message value');
t.equal(emptyString, message.key, 'invalid message key');
done();
});
consumer.subscribe([topic]);
consumer.consume();
setTimeout(function() {
producer.produce(topic, null, value, key);
}, 2000);
});
it('should be able to produce and consume messages: empty key and empty value', function(done) {
var key = '';
var value = Buffer.from('');
producer.setPollInterval(10);
consumer.once('data', function(message) {
t.notEqual(message.value, null, 'message should not be null');
t.equal(value.toString(), message.value.toString(), 'invalid message value');
t.equal(key, message.key, 'invalid message key');
done();
});
consumer.subscribe([topic]);
consumer.consume();
setTimeout(function() {
producer.produce(topic, null, value, key);
}, 2000);
});
it('should be able to produce and consume messages: null key and null value', function(done) {
var key = null;
var value = null;
producer.setPollInterval(10);
consumer.once('data', function(message) {
t.equal(value, message.value, 'invalid message value');
t.equal(key, message.key, 'invalid message key');
done();
});
consumer.subscribe([topic]);
consumer.consume();
setTimeout(function() {
producer.produce(topic, null, value, key);
}, 2000);
});
describe('Exceptional case - offset_commit_cb true', function() {
var grp = 'kafka-mocha-grp-' + crypto.randomBytes(20).toString('hex');
var consumerOpts = {
'metadata.broker.list': kafkaBrokerList,
'group.id': grp,
'fetch.wait.max.ms': 1000,
'session.timeout.ms': 10000,
'enable.auto.commit': false,
'debug': 'all',
'offset_commit_cb': true
};
beforeEach(function(done) {
consumer = new Kafka.KafkaConsumer(consumerOpts, {
'auto.offset.reset': 'largest',
});
consumer.connect({}, function(err, d) {
t.ifError(err);
t.equal(typeof d, 'object', 'metadata should be returned');
done();
});
eventListener(consumer);
});
afterEach(function(done) {
consumer.disconnect(function() {
done();
});
});
it('should async commit after consuming', function(done) {
var key = '';
var value = Buffer.from('');
var lastOffset = null;
consumer.once('data', function(message) {
lastOffset = message.offset;
// disconnect in offset commit callback
consumer.on('offset.commit', function(err, offsets) {
t.ifError(err);
t.equal(typeof offsets, 'object', 'offsets should be returned');
consumer.disconnect(function() {
// reconnect in disconnect callback
consumer.connect({}, function(err, d) {
t.ifError(err);
t.equal(typeof d, 'object', 'metadata should be returned');
// check that no new messages arrive, as the offset was committed
consumer.once('data', function(message) {
done(new Error('Should never be here'));
});
consumer.subscribe([topic]);
consumer.consume();
setTimeout(function() {
done();
}, 5000);
});
});
});
consumer.commitMessage(message);
});
consumer.subscribe([topic]);
consumer.consume();
setTimeout(function() {
producer.produce(topic, null, value, key);
}, 2000);
});
});
describe('Exceptional case - offset_commit_cb function', function() {
var grp = 'kafka-mocha-grp-' + crypto.randomBytes(20).toString('hex');
afterEach(function(done) {
consumer.disconnect(function() {
done();
});
});
it('should callback offset_commit_cb after commit', function(done) {
var consumerOpts = {
'metadata.broker.list': kafkaBrokerList,
'group.id': grp,
'fetch.wait.max.ms': 1000,
'session.timeout.ms': 10000,
'enable.auto.commit': false,
'debug': 'all',
'offset_commit_cb': function(offset) {
done();
}
};
consumer = new Kafka.KafkaConsumer(consumerOpts, {
'auto.offset.reset': 'largest',
});
eventListener(consumer);
consumer.connect({}, function(err, d) {
t.ifError(err);
t.equal(typeof d, 'object', 'metadata should be returned');
consumer.subscribe([topic]);
consumer.consume();
setTimeout(function() {
producer.produce(topic, null, Buffer.from(''), '');
}, 2000);
});
consumer.once('data', function(message) {
consumer.commitMessage(message);
});
});
});
describe('Cooperative sticky', function() {
var consumer;
beforeEach(function(done) {
var grp = 'kafka-mocha-grp-' + crypto.randomBytes(20).toString('hex');
var consumerOpts = {
'metadata.broker.list': kafkaBrokerList,
'group.id': grp,
'fetch.wait.max.ms': 1000,
'session.timeout.ms': 10000,
'enable.auto.commit': false,
'debug': 'all',
'partition.assignment.strategy': 'cooperative-sticky'
};
consumer = new Kafka.KafkaConsumer(consumerOpts, {
'auto.offset.reset': 'largest',
});
consumer.connect({}, function(err, d) {
t.ifError(err);
t.equal(typeof d, 'object', 'metadata should be returned');
done();
});
eventListener(consumer);
});
afterEach(function(done) {
consumer.disconnect(function() {
done();
});
});
it('should be able to produce and consume messages', function (done) {
var key = 'key';
crypto.randomBytes(4096, function(ex, buffer) {
producer.setPollInterval(10);
consumer.on('data', function(message) {
t.equal(buffer.toString(), message.value.toString(), 'invalid message value');
t.equal(key, message.key, 'invalid message key');
t.equal(topic, message.topic, 'invalid message topic');
t.ok(message.offset >= 0, 'invalid message offset');
done();
});
consumer.subscribe([topic]);
consumer.consume();
setTimeout(function() {
producer.produce(topic, null, buffer, key);
}, 2000);
});
});
});
function assert_headers_match(expectedHeaders, messageHeaders) {
t.equal(expectedHeaders.length, messageHeaders.length, 'Headers length does not match expected length');
for (var i = 0; i < expectedHeaders.length; i++) {
var expectedKey = Object.keys(expectedHeaders[i])[0];
var messageKey = Object.keys(messageHeaders[i]);
t.equal(messageKey.length, 1, 'Expected only one Header key');
t.equal(expectedKey, messageKey[0], 'Expected key does not match message key');
var expectedValue = Buffer.isBuffer(expectedHeaders[i][expectedKey]) ?
expectedHeaders[i][expectedKey].toString() :
expectedHeaders[i][expectedKey];
var actualValue = messageHeaders[i][expectedKey].toString();
t.equal(expectedValue, actualValue, 'invalid message header');
}
}
function run_headers_test(done, headers) {
var key = 'key';
crypto.randomBytes(4096, function(ex, buffer) {
producer.setPollInterval(10);
producer.once('delivery-report', function(err, report) {
if (!err) {
t.equal(topic, report.topic, 'invalid delivery-report topic');
t.equal(key, report.key, 'invalid delivery-report key');
t.ok(report.offset >= 0, 'invalid delivery-report offset');
}
});
consumer.on('data', function(message) {
t.equal(buffer.toString(), message.value.toString(), 'invalid message value');
t.equal(key, message.key, 'invalid message key');
t.equal(topic, message.topic, 'invalid message topic');
t.ok(message.offset >= 0, 'invalid message offset');
assert_headers_match(headers, message.headers);
done();
});
consumer.subscribe([topic]);
consumer.consume();
setTimeout(function() {
var timestamp = new Date().getTime();
producer.produce(topic, null, buffer, key, timestamp, "", headers);
}, 2000);
});
}
});