@@ -39,7 +39,7 @@ const promClientMock = {
3939 this . labels = jest . fn ( ) . mockReturnValue ( this ) ;
4040 this . reset = jest . fn ( ) ;
4141 this . setToCurrentTime = jest . fn ( ) ;
42- this . startTimer = jest . fn ( ) ;
42+ this . startTimer = jest . fn ( ) . mockReturnValue ( ( ) => jest . fn ( ) ) ;
4343 this . remove = jest . fn ( ) ;
4444 }
4545 } ,
@@ -54,7 +54,7 @@ const promClientMock = {
5454 constructor ( options ) {
5555
5656 this . observe = jest . fn ( ) ;
57- this . startTimer = jest . fn ( ) ;
57+ this . startTimer = jest . fn ( ) . mockReturnValue ( ( ) => jest . fn ( ) ) ;
5858 this . labels = jest . fn ( ) . mockReturnValue ( this ) ;
5959 this . reset = jest . fn ( ) ;
6060 this . remove = jest . fn ( ) ;
@@ -93,7 +93,6 @@ describe('KafkaKeyValue', function () {
9393 pixyHost : 'http://pixy' ,
9494 topicName : 'testtopic01' ,
9595 fetchImpl : fetchMock ,
96- updateDebounceTimeoutMs : 1
9796 } ) ;
9897
9998 const offset = await kkv . put ( 'key1' , 'value1' ) ;
@@ -118,7 +117,6 @@ describe('KafkaKeyValue', function () {
118117 pixyHost : 'http://pixy' ,
119118 topicName : 'testtopic01' ,
120119 fetchImpl : fetchMock ,
121- updateDebounceTimeoutMs : 1
122120 } ) ;
123121
124122 try {
@@ -171,6 +169,42 @@ describe('KafkaKeyValue', function () {
171169 expect ( onValue ) . toBeCalledWith ( { foo : 'bar' } )
172170 expect ( onValue ) . toBeCalledWith ( { foo : 'bar2' } )
173171 } ) ;
172+
173+ it ( 'updates last seen offset metric based on header value' , async function ( ) {
174+ const response = {
175+ body : new EventEmitter ( ) ,
176+ headers : new Map ( [
177+ [ 'x-kkv-last-seen-offsets' , JSON . stringify ( [
178+ { topic : 'testtopic01' , partition : 0 , offset : 17 }
179+ ] ) ]
180+ ] )
181+ } ;
182+
183+ const fetchMock = jest . fn ( ) . mockReturnValueOnce ( response ) ;
184+
185+ const metrics = KafkaKeyValue . createMetrics ( promClientMock . Counter , promClientMock . Gauge , promClientMock . Histogram ) ;
186+ const kkv = new KafkaKeyValue ( {
187+ cacheHost : 'http://cache-kkv' ,
188+ metrics,
189+ pixyHost : 'http://pixy' ,
190+ topicName : 'testtopic01' ,
191+ fetchImpl : fetchMock
192+ } ) ;
193+
194+ const streaming = kkv . streamValues ( ( ) => { } ) ;
195+ await Promise . resolve ( ) ;
196+ response . body . emit ( 'end' ) ;
197+
198+ await streaming ;
199+
200+ expect ( metrics . kafka_key_value_last_seen_offset . set ) . toHaveBeenCalledWith (
201+ {
202+ topic : 'testtopic01' ,
203+ partition : 0
204+ } ,
205+ 17
206+ )
207+ } ) ;
174208 } ) ;
175209
176210 describe ( 'onupdate handlers' , function ( ) {
@@ -183,7 +217,6 @@ describe('KafkaKeyValue', function () {
183217 metrics,
184218 pixyHost : 'http://pixy' ,
185219 topicName : 'testtopic01' ,
186- updateDebounceTimeoutMs : 1
187220 } ) ;
188221
189222 const onUpdateSpy = jest . fn ( ) ;
@@ -212,6 +245,22 @@ describe('KafkaKeyValue', function () {
212245 expect ( metrics . kafka_key_value_last_seen_offset . labels ) . toHaveBeenCalledTimes ( 1 ) ;
213246 expect ( metrics . kafka_key_value_last_seen_offset . labels ) . toHaveBeenCalledWith ( 'cache-kkv' , 'testtopic01' , '0' ) ;
214247 expect ( metrics . kafka_key_value_last_seen_offset . set ) . toHaveBeenCalledWith ( 28262 ) ;
248+
249+ updateEvents . emit ( 'update' , {
250+ v : 1 ,
251+ topic : 'testtopic01' ,
252+ offsets : {
253+ '0' : 28263
254+ } ,
255+ updates : {
256+ 'bd3f6188-d865-443d-8646-03e8f1c643cb' : { }
257+ }
258+ } ) ;
259+
260+ // Promises needs to resolve before we get new value
261+ await new Promise ( resolve => setTimeout ( resolve , 10 ) ) ;
262+
263+ expect ( onUpdateSpy ) . toHaveBeenCalledTimes ( 2 ) ;
215264 } ) ;
216265
217266 it ( 'only handles updates for the same key once if called within the debounce timeout period' , async function ( ) {
@@ -221,7 +270,6 @@ describe('KafkaKeyValue', function () {
221270 metrics,
222271 pixyHost : 'http://pixy' ,
223272 topicName : 'testtopic01' ,
224- updateDebounceTimeoutMs : 10
225273 } ) ;
226274
227275 const onUpdateSpy = jest . fn ( ) ;
@@ -298,6 +346,21 @@ describe('KafkaKeyValue', function () {
298346 expect ( onUpdateSpy ) . toHaveBeenCalledTimes ( 2 ) ;
299347 expect ( onUpdateSpy ) . toHaveBeenCalledWith ( 'bd3f6188-d865-443d-8646-03e8f1c643cb' , { foo : 'bar' } )
300348 expect ( onUpdateSpy ) . toHaveBeenCalledWith ( 'aaaa6188-d865-443d-8646-03e8f1c643cb' , { foo : 'bar' } )
349+
350+ updateEvents . emit ( 'update' , {
351+ v : 1 ,
352+ topic : 'testtopic01' ,
353+ offsets : {
354+ '0' : 28265
355+ } ,
356+ updates : {
357+ 'aaaa6188-d865-443d-8646-03e8f1c643cb' : { }
358+ }
359+ } ) ;
360+
361+ await Promise . resolve ( ) ;
362+
363+ expect ( onUpdateSpy ) . toHaveBeenCalledTimes ( 3 ) ;
301364 } ) ;
302365 } ) ;
303366
@@ -309,7 +372,6 @@ describe('KafkaKeyValue', function () {
309372 metrics,
310373 pixyHost : 'http://pixy' ,
311374 topicName : 'testtopic01' ,
312- updateDebounceTimeoutMs : 1
313375 } ) ;
314376
315377 kkv . updatePartitionOffsetMetrics ( {
0 commit comments