@@ -10,24 +10,33 @@ var t = require('assert');
1010var crypto = require ( 'crypto' ) ;
1111
1212var eventListener = require ( './listener' ) ;
13+ const { createTopics, deleteTopics } = require ( './topicUtils' ) ;
1314
1415var KafkaConsumer = require ( '../' ) . KafkaConsumer ;
1516
1617var kafkaBrokerList = process . env . KAFKA_HOST || 'localhost:9092' ;
17- var topic = 'test' ;
1818
1919describe ( 'Consumer' , function ( ) {
2020 var gcfg ;
21+ let topic ;
22+ let createdTopics = [ ] ;
2123
22- beforeEach ( function ( ) {
24+ beforeEach ( function ( done ) {
2325 var grp = 'kafka-mocha-grp-' + crypto . randomBytes ( 20 ) . toString ( 'hex' ) ;
26+ topic = 'test' + crypto . randomBytes ( 20 ) . toString ( 'hex' ) ;
2427 gcfg = {
2528 'bootstrap.servers' : kafkaBrokerList ,
2629 'group.id' : grp ,
2730 'debug' : 'all' ,
2831 'rebalance_cb' : true ,
2932 'enable.auto.commit' : false
3033 } ;
34+ createTopics ( [ { topic, num_partitions : 1 , replication_factor : 1 } ] , kafkaBrokerList , done ) ;
35+ createdTopics . push ( topic ) ;
36+ } ) ;
37+
38+ after ( function ( done ) {
39+ deleteTopics ( createdTopics , kafkaBrokerList , done ) ;
3140 } ) ;
3241
3342 describe ( 'commit' , function ( ) {
@@ -94,34 +103,50 @@ describe('Consumer', function() {
94103 t . equal ( position . length , 0 ) ;
95104 } ) ;
96105
97- it ( 'after assign, should get committed array without offsets ' , function ( done ) {
98- consumer . assign ( [ { topic :topic , partition :0 } ] ) ;
99- // Defer this for a second
100- setTimeout ( function ( ) {
101- consumer . committed ( null , 1000 , function ( err , committed ) {
102- t . ifError ( err ) ;
103- t . equal ( committed . length , 1 ) ;
104- t . equal ( typeof committed [ 0 ] , 'object' , 'TopicPartition should be an object' ) ;
105- t . deepStrictEqual ( committed [ 0 ] . partition , 0 ) ;
106- t . equal ( committed [ 0 ] . offset , undefined ) ;
107- done ( ) ;
108- } ) ;
106+ it ( 'after assign, should get committed array without offsets ' , function ( done ) {
107+ consumer . assign ( [ { topic : topic , partition : 0 } ] ) ;
108+ consumer . committed ( null , 1000 , function ( err , committed ) {
109+ t . ifError ( err ) ;
110+ t . equal ( committed . length , 1 ) ;
111+ t . equal ( typeof committed [ 0 ] , 'object' , 'TopicPartition should be an object' ) ;
112+ t . deepStrictEqual ( committed [ 0 ] . partition , 0 ) ;
113+ t . equal ( committed [ 0 ] . offset , undefined ) ;
114+ done ( ) ;
109115 } , 1000 ) ;
110116 } ) ;
111117
112- it ( 'after assign and commit, should get committed offsets' , function ( done ) {
118+ it ( 'after assign and commit, should get committed offsets with same metadata ' , function ( done ) {
113119 consumer . assign ( [ { topic :topic , partition :0 } ] ) ;
114- consumer . commitSync ( { topic :topic , partition :0 , offset :1000 } ) ;
120+ consumer . commitSync ( { topic :topic , partition :0 , offset :1000 , metadata : 'A string with unicode ǂ' } ) ;
115121 consumer . committed ( null , 1000 , function ( err , committed ) {
116122 t . ifError ( err ) ;
117123 t . equal ( committed . length , 1 ) ;
118124 t . equal ( typeof committed [ 0 ] , 'object' , 'TopicPartition should be an object' ) ;
119125 t . deepStrictEqual ( committed [ 0 ] . partition , 0 ) ;
120126 t . deepStrictEqual ( committed [ 0 ] . offset , 1000 ) ;
127+ t . deepStrictEqual ( committed [ 0 ] . metadata , 'A string with unicode ǂ' ) ;
121128 done ( ) ;
122129 } ) ;
123130 } ) ;
124131
132+ it ( 'after assign and commit, a different consumer should get the same committed offsets and metadata' , function ( done ) {
133+ consumer . assign ( [ { topic :topic , partition :0 } ] ) ;
134+ consumer . commitSync ( { topic :topic , partition :0 , offset :1000 , metadata : 'A string with unicode ǂ' } ) ;
135+
136+ let consumer2 = new KafkaConsumer ( gcfg , { } ) ;
137+ consumer2 . connect ( { timeout : 2000 } , function ( err , info ) {
138+ consumer2 . committed ( [ { topic, partition : 0 } ] , 1000 , function ( err , committed ) {
139+ t . ifError ( err ) ;
140+ t . equal ( committed . length , 1 ) ;
141+ t . equal ( typeof committed [ 0 ] , 'object' , 'TopicPartition should be an object' ) ;
142+ t . deepStrictEqual ( committed [ 0 ] . partition , 0 ) ;
143+ t . deepStrictEqual ( committed [ 0 ] . offset , 1000 ) ;
144+ t . deepStrictEqual ( committed [ 0 ] . metadata , 'A string with unicode ǂ' ) ;
145+ consumer2 . disconnect ( done ) ;
146+ } ) ;
147+ } ) ;
148+ } ) ;
149+
125150 it ( 'after assign, before consume, position should return an array without offsets' , function ( done ) {
126151 consumer . assign ( [ { topic :topic , partition :0 } ] ) ;
127152 var position = consumer . position ( ) ;
@@ -154,7 +179,7 @@ describe('Consumer', function() {
154179 consumer . connect ( { timeout : 2000 } , function ( err , info ) {
155180 t . ifError ( err ) ;
156181 consumer . assign ( [ {
157- topic : 'test' ,
182+ topic,
158183 partition : 0 ,
159184 offset : 0
160185 } ] ) ;
@@ -172,7 +197,7 @@ describe('Consumer', function() {
172197
173198 it ( 'should be able to seek' , function ( cb ) {
174199 consumer . seek ( {
175- topic : 'test' ,
200+ topic,
176201 partition : 0 ,
177202 offset : 0
178203 } , 1 , function ( err ) {
@@ -183,7 +208,7 @@ describe('Consumer', function() {
183208
184209 it ( 'should be able to seek with a timeout of 0' , function ( cb ) {
185210 consumer . seek ( {
186- topic : 'test' ,
211+ topic,
187212 partition : 0 ,
188213 offset : 0
189214 } , 0 , function ( err ) {
@@ -217,7 +242,7 @@ describe('Consumer', function() {
217242 t . equal ( 0 , consumer . subscription ( ) . length ) ;
218243 consumer . subscribe ( [ topic ] ) ;
219244 t . equal ( 1 , consumer . subscription ( ) . length ) ;
220- t . equal ( 'test' , consumer . subscription ( ) [ 0 ] ) ;
245+ t . equal ( topic , consumer . subscription ( ) [ 0 ] ) ;
221246 t . equal ( 0 , consumer . assignments ( ) . length ) ;
222247 } ) ;
223248
@@ -308,6 +333,7 @@ describe('Consumer', function() {
308333
309334 consumer . subscribe ( [ topic ] ) ;
310335
336+ consumer . setDefaultConsumeTimeout ( 500 ) ; // Topic might not have any messages.
311337 consumer . consume ( 1 , function ( err , messages ) {
312338 t . ifError ( err ) ;
313339
0 commit comments