@@ -10,24 +10,33 @@ var t = require('assert');
10
10
var crypto = require ( 'crypto' ) ;
11
11
12
12
var eventListener = require ( './listener' ) ;
13
+ const { createTopics, deleteTopics } = require ( './topicUtils' ) ;
13
14
14
15
var KafkaConsumer = require ( '../' ) . KafkaConsumer ;
15
16
16
17
var kafkaBrokerList = process . env . KAFKA_HOST || 'localhost:9092' ;
17
- var topic = 'test' ;
18
18
19
19
describe ( 'Consumer' , function ( ) {
20
20
var gcfg ;
21
+ let topic ;
22
+ let createdTopics = [ ] ;
21
23
22
- beforeEach ( function ( ) {
24
+ beforeEach ( function ( done ) {
23
25
var grp = 'kafka-mocha-grp-' + crypto . randomBytes ( 20 ) . toString ( 'hex' ) ;
26
+ topic = 'test' + crypto . randomBytes ( 20 ) . toString ( 'hex' ) ;
24
27
gcfg = {
25
28
'bootstrap.servers' : kafkaBrokerList ,
26
29
'group.id' : grp ,
27
30
'debug' : 'all' ,
28
31
'rebalance_cb' : true ,
29
32
'enable.auto.commit' : false
30
33
} ;
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 ) ;
31
40
} ) ;
32
41
33
42
describe ( 'commit' , function ( ) {
@@ -94,34 +103,50 @@ describe('Consumer', function() {
94
103
t . equal ( position . length , 0 ) ;
95
104
} ) ;
96
105
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 ( ) ;
109
115
} , 1000 ) ;
110
116
} ) ;
111
117
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 ) {
113
119
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 ǂ' } ) ;
115
121
consumer . committed ( null , 1000 , function ( err , committed ) {
116
122
t . ifError ( err ) ;
117
123
t . equal ( committed . length , 1 ) ;
118
124
t . equal ( typeof committed [ 0 ] , 'object' , 'TopicPartition should be an object' ) ;
119
125
t . deepStrictEqual ( committed [ 0 ] . partition , 0 ) ;
120
126
t . deepStrictEqual ( committed [ 0 ] . offset , 1000 ) ;
127
+ t . deepStrictEqual ( committed [ 0 ] . metadata , 'A string with unicode ǂ' ) ;
121
128
done ( ) ;
122
129
} ) ;
123
130
} ) ;
124
131
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
+
125
150
it ( 'after assign, before consume, position should return an array without offsets' , function ( done ) {
126
151
consumer . assign ( [ { topic :topic , partition :0 } ] ) ;
127
152
var position = consumer . position ( ) ;
@@ -154,7 +179,7 @@ describe('Consumer', function() {
154
179
consumer . connect ( { timeout : 2000 } , function ( err , info ) {
155
180
t . ifError ( err ) ;
156
181
consumer . assign ( [ {
157
- topic : 'test' ,
182
+ topic,
158
183
partition : 0 ,
159
184
offset : 0
160
185
} ] ) ;
@@ -172,7 +197,7 @@ describe('Consumer', function() {
172
197
173
198
it ( 'should be able to seek' , function ( cb ) {
174
199
consumer . seek ( {
175
- topic : 'test' ,
200
+ topic,
176
201
partition : 0 ,
177
202
offset : 0
178
203
} , 1 , function ( err ) {
@@ -183,7 +208,7 @@ describe('Consumer', function() {
183
208
184
209
it ( 'should be able to seek with a timeout of 0' , function ( cb ) {
185
210
consumer . seek ( {
186
- topic : 'test' ,
211
+ topic,
187
212
partition : 0 ,
188
213
offset : 0
189
214
} , 0 , function ( err ) {
@@ -217,7 +242,7 @@ describe('Consumer', function() {
217
242
t . equal ( 0 , consumer . subscription ( ) . length ) ;
218
243
consumer . subscribe ( [ topic ] ) ;
219
244
t . equal ( 1 , consumer . subscription ( ) . length ) ;
220
- t . equal ( 'test' , consumer . subscription ( ) [ 0 ] ) ;
245
+ t . equal ( topic , consumer . subscription ( ) [ 0 ] ) ;
221
246
t . equal ( 0 , consumer . assignments ( ) . length ) ;
222
247
} ) ;
223
248
@@ -308,6 +333,7 @@ describe('Consumer', function() {
308
333
309
334
consumer . subscribe ( [ topic ] ) ;
310
335
336
+ consumer . setDefaultConsumeTimeout ( 500 ) ; // Topic might not have any messages.
311
337
consumer . consume ( 1 , function ( err , messages ) {
312
338
t . ifError ( err ) ;
313
339
0 commit comments