-
Notifications
You must be signed in to change notification settings - Fork 630
/
test.consumer.js
738 lines (666 loc) · 25.2 KB
/
test.consumer.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
727
728
729
730
731
732
733
734
735
736
737
738
'use strict';
var libPath = process.env['KAFKA_COV'] ? '../lib-cov/' : '../lib/';
var Consumer = require(libPath + 'consumer');
var Producer = require(libPath + 'producer');
var Offset = require(libPath + 'offset');
var Client = require(libPath + 'kafkaClient');
var should = require('should');
var uuid = require('uuid');
var TopicsNotExistError = require(libPath + 'errors').TopicsNotExistError;
var FakeClient = require('./mocks/mockClient');
var InvalidConfigError = require('../lib/errors/InvalidConfigError');
const createTopic = require('../docker/createTopic');
const sendMessage = require('./helpers/sendMessage');
const sendMessageEach = require('./helpers/sendMessageEach');
const _ = require('lodash');
var client, producer, offset;
var TOPIC_POSTFIX = '_test_' + Date.now();
var EXISTS_TOPIC_1 = '_exists_1' + TOPIC_POSTFIX;
var EXISTS_TOPIC_2 = '_exists_2' + TOPIC_POSTFIX;
var EXISTS_GZIP = '_exists_gzip'; // + TOPIC_POSTFIX
var EXISTS_SNAPPY = '_exists_snappy'; // + TOPIC_POSTFIX
// Compression-friendly to be interesting
var SNAPPY_MESSAGE = new Array(20).join('snappy');
var host = process.env['KAFKA_TEST_HOST'] || '';
function noop () {
console.log(arguments);
}
describe('Consumer', function () {
describe('#setOffset', function () {
var client, consumer;
beforeEach(function () {
client = new FakeClient();
consumer = new Consumer(client, [{ topic: 'fake-topic' }]);
clearTimeout(consumer.checkPartitionOwnershipInterval);
consumer.payloads = [
{ topic: 'fake-topic', partition: '0', offset: 0, maxBytes: 1048576, metadata: 'm' },
{ topic: 'fake-topic', partition: '1', offset: 0, maxBytes: 1048576, metadata: 'm' }
];
});
it('should setOffset correctly given partition is a string', function () {
consumer.setOffset('fake-topic', '0', 86);
consumer.payloads[0].offset.should.be.eql(86);
consumer.setOffset('fake-topic', '1', 23);
consumer.payloads[1].offset.should.be.eql(23);
});
it('should setOffset correctly if given partition is a number', function () {
consumer.setOffset('fake-topic', 0, 67);
consumer.payloads[0].offset.should.be.eql(67);
consumer.setOffset('fake-topic', 1, 98);
consumer.payloads[1].offset.should.be.eql(98);
});
});
describe('validate topics', function () {
function validateThrowsInvalidConfigError (topics) {
should.throws(function () {
var client = new FakeClient(host);
// eslint-disable-next-line no-new
new Consumer(client, topics);
}, InvalidConfigError);
}
function validateDoesNotThrowInvalidConfigError (topics) {
should.doesNotThrow(function () {
var client = new FakeClient(host);
// eslint-disable-next-line no-new
new Consumer(client, topics);
}, InvalidConfigError);
}
it('should throw an error on invalid topic partitions', function () {
validateThrowsInvalidConfigError([
{
topic: 'another-topic'
},
{
topic: 'my-fake-topic',
partition: '7'
}
]);
validateThrowsInvalidConfigError([
{
topic: 'another-topic',
partition: 3
},
{
topic: 'my-fake-topic',
partition: {}
}
]);
validateThrowsInvalidConfigError([
{
topic: 'my-fake-topic',
partition: false
}
]);
});
it('should not throw an error for valid topic partition', function () {
validateDoesNotThrowInvalidConfigError([
{
topic: 'fake-topic',
partition: 7
},
{
topic: 'another-topic'
}
]);
validateDoesNotThrowInvalidConfigError([
{
topic: 'another-topic'
},
{
topic: 'fake-topic',
partition: 0
}
]);
});
});
describe('Compression', function () {
describe('by topic', function () {
let topic, messages;
before(function () {
if (process.env.KAFKA_VERSION === '0.9') {
this.skip();
}
topic = uuid.v4();
messages = _.times(10, function () {
return new Array(100).join(Math.random().toString(36));
});
return createTopic(topic, 1, 1, 'compression.type=gzip').then(function () {
return new Promise(function (resolve, reject) {
sendMessage(messages, topic, function (error) {
if (error) {
return reject(error);
}
resolve();
});
});
});
});
it('should not throw offsetOutOfRange error', function (done) {
const client = new Client({ kafkaHost: '127.0.0.1:9092' });
const consumer = new Consumer(client, [
{
topic,
partition: 0
}
]);
let verifyOffset = 0;
consumer.on('offsetOutOfRange', done);
consumer.on('message', function (message) {
message.offset.should.be.equal(verifyOffset++);
message.partition.should.be.equal(0);
if (_.pull(messages, message.value).length === 0) {
setTimeout(function () {
consumer.close(done);
}, 50);
}
});
});
});
describe('by message', function () {
let topic, messages;
before(function () {
if (process.env.KAFKA_VERSION === '0.9') {
this.skip();
}
topic = uuid.v4();
messages = _.times(10, function () {
return new Array(100).join(Math.random().toString(36));
});
return createTopic(topic, 1, 1).then(function () {
return new Promise(function (resolve, reject) {
sendMessageEach(messages, topic, function (error) {
if (error) {
return reject(error);
}
resolve();
});
});
});
});
it('should not throw offsetOutOfRange error', function (done) {
const client = new Client({ kafkaHost: '127.0.0.1:9092' });
const consumer = new Consumer(client, [
{
topic,
partition: 0
}
]);
let verifyOffset = 0;
consumer.on('offsetOutOfRange', done);
consumer.on('message', function (message) {
message.offset.should.be.equal(verifyOffset++);
message.partition.should.be.equal(0);
if (_.pull(messages, message.value).length === 0) {
setTimeout(function () {
consumer.close(done);
}, 50);
}
});
});
});
});
describe('validate groupId', function () {
function validateThrowsInvalidConfigError (groupId) {
should.throws(function () {
var client = new FakeClient(host);
// eslint-disable-next-line no-new
new Consumer(client, [{ topic: EXISTS_TOPIC_2 }], { groupId: groupId });
}, InvalidConfigError);
}
function validateDoesNotThrowInvalidConfigError (groupId) {
should.doesNotThrow(function () {
var client = new FakeClient(host);
// eslint-disable-next-line no-new
new Consumer(client, [{ topic: EXISTS_TOPIC_2 }], { groupId: groupId });
});
}
it('should throws an error on invalid group IDs', function () {
validateThrowsInvalidConfigError('myGroupId:12345');
validateThrowsInvalidConfigError('myGroupId,12345');
validateThrowsInvalidConfigError('myGroupId"12345"');
validateThrowsInvalidConfigError('myGroupId?12345');
});
it('should not throw on valid group IDs', function () {
validateDoesNotThrowInvalidConfigError('myGroupId.12345');
validateDoesNotThrowInvalidConfigError('something_12345');
validateDoesNotThrowInvalidConfigError('myGroupId-12345');
});
});
function offsetOutOfRange (topic, consumer) {
topic.maxNum = 2;
offset.fetch([topic], function (err, offsets) {
if (err) consumer.emit('error', err);
var min = Math.min.apply(null, offsets[topic.topic][topic.partition]);
consumer.setOffset(topic.topic, topic.partition, min);
});
}
[
{
name: 'PLAINTEXT Consumer'
},
{
name: 'SSL Consumer',
sslOptions: {
rejectUnauthorized: false
},
suiteTimeout: 30000
}
].forEach(function (testParameters) {
var sslOptions = testParameters.sslOptions || undefined;
var suiteTimeout = testParameters.suiteTimeout || null;
var suiteName = testParameters.name;
const kafkaHost = '127.0.0.1:' + (sslOptions == null ? '9092' : '9093');
function createClient () {
return new Client({ kafkaHost, sslOptions });
}
describe(suiteName, function () {
before(function (done) {
if (suiteTimeout) {
this.timeout(suiteTimeout);
}
client = createClient();
producer = new Producer(client);
offset = new Offset(client);
producer.on('ready', function () {
producer.createTopics([EXISTS_TOPIC_1, EXISTS_TOPIC_2, EXISTS_GZIP, EXISTS_SNAPPY], true, function (err) {
if (err) return done(err);
producer.send(
[
{ topic: EXISTS_TOPIC_2, messages: 'hello kafka' },
{ topic: EXISTS_GZIP, messages: 'hello gzip', attributes: 1 },
{ topic: EXISTS_SNAPPY, messages: SNAPPY_MESSAGE, attributes: 2 }
],
done
);
});
});
});
after(function (done) {
client.close(done);
});
afterEach(function () {
client.removeAllListeners();
});
describe('events', function () {
it('should emit message when get new message', function (done) {
if (suiteTimeout) {
this.timeout(suiteTimeout);
}
var topics = [{ topic: EXISTS_TOPIC_2 }];
var options = { autoCommit: false, groupId: '_groupId_1_test' };
var consumer = new Consumer(client, topics, options);
var count = 0;
consumer.on('error', noop);
consumer.on('offsetOutOfRange', function (topic) {
offsetOutOfRange(topic, this);
});
consumer.on('message', function (message) {
message.topic.should.equal(EXISTS_TOPIC_2);
message.value.should.equal('hello kafka');
message.highWaterOffset.should.be.above(0);
message.partition.should.equal(0);
offset.commit('_groupId_1_test', [message], function (err) {
if (count++ === 0) done(err);
});
});
});
it('should decode gzip messages', function (done) {
if (suiteTimeout) {
this.timeout(suiteTimeout);
}
var topics = [{ topic: EXISTS_GZIP }];
var options = { autoCommit: false, groupId: '_groupId_gzip_test' };
var client = createClient();
var consumer = new Consumer(client, topics, options);
var count = 0;
consumer.on('error', noop);
consumer.on('offsetOutOfRange', function (topic) {
offsetOutOfRange(topic, this);
});
consumer.on('message', function (message) {
message.topic.should.equal(EXISTS_GZIP);
message.value.should.equal('hello gzip');
message.highWaterOffset.should.be.above(0);
offset.commit('_groupId_gzip_test', [message], function (err) {
if (count++ === 0) {
consumer.close(function () {
done(err);
});
}
});
});
});
it('should decode snappy messages', function (done) {
if (suiteTimeout) {
this.timeout(suiteTimeout);
}
var topics = [{ topic: EXISTS_SNAPPY }];
var options = { autoCommit: false, groupId: '_groupId_snappy_test' };
var client = createClient();
var consumer = new Consumer(client, topics, options);
var count = 0;
consumer.on('error', noop);
consumer.on('offsetOutOfRange', function (topic) {
offsetOutOfRange(topic, this);
});
consumer.once('message', function (message) {
message.topic.should.equal(EXISTS_SNAPPY);
message.value.should.equal(SNAPPY_MESSAGE);
message.highWaterOffset.should.be.above(0);
offset.commit('_groupId_snappy_test', [message], function (err) {
if (count++ === 0) {
consumer.close(function () {
done(err);
});
}
});
});
});
it('should emit error when topic not exists', function (done) {
var topics = [{ topic: '_not_exist_topic_1_test' }];
var consumer = new Consumer(client, topics);
consumer.on('error', function (error) {
error.should.be.an
.instanceOf(TopicsNotExistError)
.and.have.property('message', 'The topic(s) _not_exist_topic_1_test do not exist');
done();
});
});
it('should emit offsetOutOfRange when offset out of range', function (done) {
var topics = [{ topic: EXISTS_TOPIC_1, offset: 100 }];
var options = { fromOffset: true, autoCommit: false };
var count = 0;
var client = createClient();
var consumer = new Consumer(client, topics, options);
consumer.on('offsetOutOfRange', function (topic) {
topic.topic.should.equal(EXISTS_TOPIC_1);
topic.partition.should.equal(0);
topic.message.should.equal('OffsetOutOfRange');
if (count++ === 0) {
consumer.close(function () {
done();
});
}
});
consumer.on('error', noop);
});
});
describe('#addTopics', function () {
describe('when topic need to added not exist', function () {
it('should return error', function (done) {
var options = { autoCommit: false, groupId: '_groupId_1_test' };
var topics = ['_not_exist_topic_1_test'];
var consumer = new Consumer(client, [], options);
consumer.on('error', noop);
consumer.addTopics(topics, function (err) {
err.topics.length.should.equal(1);
err.topics.should.eql(topics);
done();
});
});
it('should return error when using payload as well', function (done) {
var options = { autoCommit: false, groupId: '_groupId_1_test' };
var topics = [{ topic: '_not_exist_topic_1_test', offset: 42 }];
var consumer = new Consumer(client, [], options);
consumer.on('error', noop);
consumer.addTopics(
topics,
function (err) {
err.topics.length.should.equal(1);
err.topics.should.eql(
topics.map(function (p) {
return p.topic;
})
);
done();
},
true
);
});
});
describe('when topic need to added exist', function () {
it('should added successfully', function (done) {
var options = { autoCommit: false, groupId: '_groupId_addTopics_test' };
var topics = [EXISTS_TOPIC_2];
var consumer = new Consumer(client, [], options);
consumer.on('error', noop);
consumer.addTopics(topics, function (err, data) {
data.should.eql(topics);
consumer.payloads.some(function (p) {
return p.topic === topics[0];
}).should.be.ok;
done(err);
});
});
it('should add with given offset', function (done) {
var options = { autoCommit: false, groupId: '_groupId_addTopics_test' };
var topics = [{ topic: EXISTS_TOPIC_2, offset: 42 }];
var consumer = new Consumer(client, [], options);
consumer.on('error', noop);
consumer.addTopics(
topics,
function (err, data) {
data.should.eql(
topics.map(function (p) {
return p.topic;
})
);
consumer.payloads.some(function (p) {
return p.topic === topics[0].topic && p.offset === topics[0].offset;
}).should.be.ok;
done(err);
},
true
);
});
});
describe('#removeTopics', function () {
it('should remove topics successfully', function (done) {
var options = { autoCommit: false, groupId: '_groupId_addTopics_test' };
var topics = [{ topic: EXISTS_TOPIC_2 }, { topic: EXISTS_TOPIC_1 }];
var consumer = new Consumer(client, topics, options);
consumer.on('error', noop);
consumer.payloads.length.should.equal(2);
consumer.removeTopics(EXISTS_TOPIC_2, function (err, removed) {
removed.should.equal(1);
consumer.payloads.length.should.equal(1);
done(err);
});
});
});
describe('#setOffset', function () {
it('should update the offset in consumer', function () {
var options = { autoCommit: false, groupId: '_groupId_addTopics_test' };
var topics = [{ topic: EXISTS_TOPIC_2 }];
var consumer = new Consumer(client, topics, options);
consumer.on('error', noop);
consumer.setOffset(EXISTS_TOPIC_2, 0, 100);
consumer.payloads
.filter(function (p) {
return p.topic === EXISTS_TOPIC_2;
})[0]
.offset.should.equal(100);
});
});
describe('#commit', function () {
it('should commit offset of current topics', function (done) {
var topics = [{ topic: EXISTS_TOPIC_2 }];
var options = { autoCommit: false, groupId: '_groupId_commit_test' };
var client = new Client();
var consumer = new Consumer(client, topics, options);
var count = 0;
consumer.on('error', noop);
consumer.on('offsetOutOfRange', function (topic) {
offsetOutOfRange(topic, this);
});
consumer.on('message', function (message) {
consumer.commit(true, function (err) {
if (!err && count++ === 0) done(err);
});
});
});
});
describe('#buildPayloads', function () {
it('should set default config on the topic', function () {
var defaults = {
groupId: 'kafka-node-group',
autoCommit: true,
autoCommitIntervalMs: 5000,
encoding: 'utf8',
fetchMaxWaitMs: 100,
fetchMinBytes: 1,
fetchMaxBytes: 1024 * 1024,
fromOffset: false
};
var consumer = new Consumer(client, []);
consumer.options.should.eql(defaults);
var topic = consumer.buildPayloads([{ topic: 'topic' }])[0];
topic.partition.should.equal(0);
topic.offset.should.equal(0);
topic.metadata.should.equal('m');
topic.maxBytes.should.equal(1024 * 1024);
});
it('should support custom options', function () {
var options = {
groupId: 'custom-group',
autoCommit: false,
autoCommitIntervalMs: 1000,
fetchMaxWaitMs: 200,
fetchMinBytes: 1,
fetchMaxBytes: 1024,
fromOffset: false
};
var consumer = new Consumer(client, [], options);
consumer.options.should.equal(options);
var topic = consumer.buildPayloads([{ topic: 'topic' }])[0];
topic.partition.should.equal(0);
topic.offset.should.equal(0);
topic.metadata.should.equal('m');
topic.maxBytes.should.equal(1024);
});
it('should return right payloads when arguments is string', function () {
var consumer = new Consumer(client, []);
var topics = ['topic'];
var topic = consumer.buildPayloads(topics)[0];
topic.should.be.an.instanceof(Object);
topic.partition.should.equal(0);
topic.offset.should.equal(0);
topic.metadata.should.equal('m');
topic.maxBytes.should.equal(1024 * 1024);
});
});
describe('#close', function () {
it('should close the consumer', function (done) {
var client = new Client();
var topics = [{ topic: EXISTS_TOPIC_2 }];
var options = { autoCommit: false, groupId: '_groupId_close_test' };
var consumer = new Consumer(client, topics, options);
consumer.once('message', function (message) {
consumer.close(function (err) {
done(err);
});
});
});
it('should commit the offset if force', function (done) {
var client = new Client();
var topics = [{ topic: EXISTS_TOPIC_2 }];
var force = true;
var options = { autoCommit: false, groupId: '_groupId_close_test' };
var consumer = new Consumer(client, topics, options);
consumer.once('message', function (message) {
consumer.close(force, function (err) {
done(err);
});
});
});
});
describe('#pauseTopics|resumeTopics', function () {
const suffix = uuid.v4();
const topic1 = `_test_topic_1_${suffix}`;
const topic2 = `_test_topic_2_${suffix}`;
const createTopic = require('../docker/createTopic');
before(function () {
return Promise.all([createTopic(topic1, 2, 1), createTopic(topic2, 2, 1)]).then(function () {
return new Promise(function (resolve, reject) {
producer.send([{ topic: topic2, messages: 'hello kafka' }], function (error) {
if (error) {
return reject(error);
}
resolve();
});
});
});
});
it('should pause or resume the topics', function (done) {
var client = createClient();
var topics = [
{ topic: topic1, partition: 0 },
{ topic: topic1, partition: 1 },
{ topic: topic2, partition: 0 },
{ topic: topic2, partition: 1 }
];
var consumer = new Consumer(client, topics, {});
consumer.on('error', function () {});
function normalize (p) {
return { topic: p.topic, partition: p.partition };
}
function compare (p1, p2) {
return p1.topic === p2.topic ? p1.partition - p2.partition : p1.topic > p2.topic;
}
consumer.payloads.should.eql(topics);
consumer.pauseTopics([topic1, { topic: topic2, partition: 0 }]);
consumer.payloads.map(normalize).should.eql([{ topic: topic2, partition: 1 }]);
consumer.resumeTopics([{ topic: topic1, partition: 0 }]);
consumer.payloads
.map(normalize)
.sort(compare)
.should.eql([{ topic: topic1, partition: 0 }, { topic: topic2, partition: 1 }]);
consumer.pausedPayloads
.map(normalize)
.sort(compare)
.should.eql([{ topic: topic1, partition: 1 }, { topic: topic2, partition: 0 }]);
consumer.resumeTopics([topic1, topic2]);
consumer.payloads.sort(compare).should.eql(topics);
consumer.pausedPayloads.should.eql([]);
consumer.once('message', function () {
consumer.close(true, done);
});
});
});
describe('Consumer', function () {
before(function (done) {
if (suiteTimeout) {
this.timeout(suiteTimeout);
}
client = createClient();
producer = new Producer(client);
offset = new Offset(client);
producer.on('ready', function () {
producer.createTopics([EXISTS_TOPIC_1, EXISTS_TOPIC_2, EXISTS_GZIP, EXISTS_SNAPPY], false, function (
err,
created
) {
if (err) return done(err);
function useNewTopics () {
producer.send(
[
{ topic: EXISTS_TOPIC_2, messages: 'hello kafka' },
{ topic: EXISTS_GZIP, messages: 'hello gzip', attributes: 1 },
{ topic: EXISTS_SNAPPY, messages: SNAPPY_MESSAGE, attributes: 2 }
],
done
);
}
// Ensure leader selection happened
setTimeout(useNewTopics, 1000);
});
});
});
});
after(function (done) {
client.close(done);
});
});
});
});
});