@@ -379,86 +379,118 @@ func TestConsumerShutsDownOutOfRange(t *testing.T) {
379
379
// requested, then such messages are ignored.
380
380
func TestConsumerExtraOffsets (t * testing.T ) {
381
381
// Given
382
- broker0 := NewMockBroker (t , 0 )
383
- fetchResponse1 := & FetchResponse {}
384
- fetchResponse1 .AddMessage ("my_topic" , 0 , nil , testMsg , 1 )
385
- fetchResponse1 .AddMessage ("my_topic" , 0 , nil , testMsg , 2 )
386
- fetchResponse1 .AddMessage ("my_topic" , 0 , nil , testMsg , 3 )
387
- fetchResponse1 .AddMessage ("my_topic" , 0 , nil , testMsg , 4 )
388
- fetchResponse2 := & FetchResponse {}
389
- fetchResponse2 .AddError ("my_topic" , 0 , ErrNoError )
390
- broker0 .SetHandlerByMap (map [string ]MockResponse {
391
- "MetadataRequest" : NewMockMetadataResponse (t ).
392
- SetBroker (broker0 .Addr (), broker0 .BrokerID ()).
393
- SetLeader ("my_topic" , 0 , broker0 .BrokerID ()),
394
- "OffsetRequest" : NewMockOffsetResponse (t ).
395
- SetOffset ("my_topic" , 0 , OffsetNewest , 1234 ).
396
- SetOffset ("my_topic" , 0 , OffsetOldest , 0 ),
397
- "FetchRequest" : NewMockSequence (fetchResponse1 , fetchResponse2 ),
398
- })
382
+ legacyFetchResponse := & FetchResponse {}
383
+ legacyFetchResponse .AddMessage ("my_topic" , 0 , nil , testMsg , 1 )
384
+ legacyFetchResponse .AddMessage ("my_topic" , 0 , nil , testMsg , 2 )
385
+ legacyFetchResponse .AddMessage ("my_topic" , 0 , nil , testMsg , 3 )
386
+ legacyFetchResponse .AddMessage ("my_topic" , 0 , nil , testMsg , 4 )
387
+ newFetchResponse := & FetchResponse {Version : 4 }
388
+ newFetchResponse .AddRecord ("my_topic" , 0 , nil , testMsg , 1 )
389
+ newFetchResponse .AddRecord ("my_topic" , 0 , nil , testMsg , 2 )
390
+ newFetchResponse .AddRecord ("my_topic" , 0 , nil , testMsg , 3 )
391
+ newFetchResponse .AddRecord ("my_topic" , 0 , nil , testMsg , 4 )
392
+ newFetchResponse .SetLastStableOffset ("my_topic" , 0 , 4 )
393
+ for _ , fetchResponse1 := range []* FetchResponse {legacyFetchResponse , newFetchResponse } {
394
+ var offsetResponseVersion int16
395
+ cfg := NewConfig ()
396
+ if fetchResponse1 .Version >= 4 {
397
+ cfg .Version = V0_11_0_0
398
+ offsetResponseVersion = 1
399
+ }
399
400
400
- master , err := NewConsumer ([]string {broker0 .Addr ()}, nil )
401
- if err != nil {
402
- t .Fatal (err )
403
- }
401
+ broker0 := NewMockBroker (t , 0 )
402
+ fetchResponse2 := & FetchResponse {}
403
+ fetchResponse2 .Version = fetchResponse1 .Version
404
+ fetchResponse2 .AddError ("my_topic" , 0 , ErrNoError )
405
+ broker0 .SetHandlerByMap (map [string ]MockResponse {
406
+ "MetadataRequest" : NewMockMetadataResponse (t ).
407
+ SetBroker (broker0 .Addr (), broker0 .BrokerID ()).
408
+ SetLeader ("my_topic" , 0 , broker0 .BrokerID ()),
409
+ "OffsetRequest" : NewMockOffsetResponse (t ).
410
+ SetVersion (offsetResponseVersion ).
411
+ SetOffset ("my_topic" , 0 , OffsetNewest , 1234 ).
412
+ SetOffset ("my_topic" , 0 , OffsetOldest , 0 ),
413
+ "FetchRequest" : NewMockSequence (fetchResponse1 , fetchResponse2 ),
414
+ })
415
+
416
+ master , err := NewConsumer ([]string {broker0 .Addr ()}, cfg )
417
+ if err != nil {
418
+ t .Fatal (err )
419
+ }
404
420
405
- // When
406
- consumer , err := master .ConsumePartition ("my_topic" , 0 , 3 )
407
- if err != nil {
408
- t .Fatal (err )
409
- }
421
+ // When
422
+ consumer , err := master .ConsumePartition ("my_topic" , 0 , 3 )
423
+ if err != nil {
424
+ t .Fatal (err )
425
+ }
410
426
411
- // Then: messages with offsets 1 and 2 are not returned even though they
412
- // are present in the response.
413
- assertMessageOffset (t , <- consumer .Messages (), 3 )
414
- assertMessageOffset (t , <- consumer .Messages (), 4 )
427
+ // Then: messages with offsets 1 and 2 are not returned even though they
428
+ // are present in the response.
429
+ assertMessageOffset (t , <- consumer .Messages (), 3 )
430
+ assertMessageOffset (t , <- consumer .Messages (), 4 )
415
431
416
- safeClose (t , consumer )
417
- safeClose (t , master )
418
- broker0 .Close ()
432
+ safeClose (t , consumer )
433
+ safeClose (t , master )
434
+ broker0 .Close ()
435
+ }
419
436
}
420
437
421
438
// It is fine if offsets of fetched messages are not sequential (although
422
439
// strictly increasing!).
423
440
func TestConsumerNonSequentialOffsets (t * testing.T ) {
424
441
// Given
425
- broker0 := NewMockBroker ( t , 0 )
426
- fetchResponse1 := & FetchResponse {}
427
- fetchResponse1 .AddMessage ("my_topic" , 0 , nil , testMsg , 5 )
428
- fetchResponse1 .AddMessage ("my_topic" , 0 , nil , testMsg , 7 )
429
- fetchResponse1 . AddMessage ( "my_topic" , 0 , nil , testMsg , 11 )
430
- fetchResponse2 := & FetchResponse {}
431
- fetchResponse2 . AddError ("my_topic" , 0 , ErrNoError )
432
- broker0 . SetHandlerByMap ( map [ string ] MockResponse {
433
- "MetadataRequest" : NewMockMetadataResponse ( t ).
434
- SetBroker ( broker0 . Addr (), broker0 . BrokerID ()).
435
- SetLeader ( "my_topic" , 0 , broker0 . BrokerID ()),
436
- "OffsetRequest" : NewMockOffsetResponse ( t ).
437
- SetOffset ( "my_topic" , 0 , OffsetNewest , 1234 ).
438
- SetOffset ( "my_topic" , 0 , OffsetOldest , 0 ),
439
- "FetchRequest" : NewMockSequence ( fetchResponse1 , fetchResponse2 ),
440
- })
442
+ legacyFetchResponse := & FetchResponse {}
443
+ legacyFetchResponse . AddMessage ( "my_topic" , 0 , nil , testMsg , 5 )
444
+ legacyFetchResponse .AddMessage ("my_topic" , 0 , nil , testMsg , 7 )
445
+ legacyFetchResponse .AddMessage ("my_topic" , 0 , nil , testMsg , 11 )
446
+ newFetchResponse := & FetchResponse { Version : 4 }
447
+ newFetchResponse . AddRecord ( "my_topic" , 0 , nil , testMsg , 5 )
448
+ newFetchResponse . AddRecord ("my_topic" , 0 , nil , testMsg , 7 )
449
+ newFetchResponse . AddRecord ( "my_topic" , 0 , nil , testMsg , 11 )
450
+ newFetchResponse . SetLastStableOffset ( "my_topic" , 0 , 11 )
451
+ for _ , fetchResponse1 := range [] * FetchResponse { legacyFetchResponse , newFetchResponse } {
452
+ var offsetResponseVersion int16
453
+ cfg := NewConfig ()
454
+ if fetchResponse1 . Version >= 4 {
455
+ cfg . Version = V0_11_0_0
456
+ offsetResponseVersion = 1
457
+ }
441
458
442
- master , err := NewConsumer ([]string {broker0 .Addr ()}, nil )
443
- if err != nil {
444
- t .Fatal (err )
445
- }
459
+ broker0 := NewMockBroker (t , 0 )
460
+ fetchResponse2 := & FetchResponse {Version : fetchResponse1 .Version }
461
+ fetchResponse2 .AddError ("my_topic" , 0 , ErrNoError )
462
+ broker0 .SetHandlerByMap (map [string ]MockResponse {
463
+ "MetadataRequest" : NewMockMetadataResponse (t ).
464
+ SetBroker (broker0 .Addr (), broker0 .BrokerID ()).
465
+ SetLeader ("my_topic" , 0 , broker0 .BrokerID ()),
466
+ "OffsetRequest" : NewMockOffsetResponse (t ).
467
+ SetVersion (offsetResponseVersion ).
468
+ SetOffset ("my_topic" , 0 , OffsetNewest , 1234 ).
469
+ SetOffset ("my_topic" , 0 , OffsetOldest , 0 ),
470
+ "FetchRequest" : NewMockSequence (fetchResponse1 , fetchResponse2 ),
471
+ })
472
+
473
+ master , err := NewConsumer ([]string {broker0 .Addr ()}, cfg )
474
+ if err != nil {
475
+ t .Fatal (err )
476
+ }
446
477
447
- // When
448
- consumer , err := master .ConsumePartition ("my_topic" , 0 , 3 )
449
- if err != nil {
450
- t .Fatal (err )
451
- }
478
+ // When
479
+ consumer , err := master .ConsumePartition ("my_topic" , 0 , 3 )
480
+ if err != nil {
481
+ t .Fatal (err )
482
+ }
452
483
453
- // Then: messages with offsets 1 and 2 are not returned even though they
454
- // are present in the response.
455
- assertMessageOffset (t , <- consumer .Messages (), 5 )
456
- assertMessageOffset (t , <- consumer .Messages (), 7 )
457
- assertMessageOffset (t , <- consumer .Messages (), 11 )
484
+ // Then: messages with offsets 1 and 2 are not returned even though they
485
+ // are present in the response.
486
+ assertMessageOffset (t , <- consumer .Messages (), 5 )
487
+ assertMessageOffset (t , <- consumer .Messages (), 7 )
488
+ assertMessageOffset (t , <- consumer .Messages (), 11 )
458
489
459
- safeClose (t , consumer )
460
- safeClose (t , master )
461
- broker0 .Close ()
490
+ safeClose (t , consumer )
491
+ safeClose (t , master )
492
+ broker0 .Close ()
493
+ }
462
494
}
463
495
464
496
// If leadership for a partition is changing then consumer resolves the new
0 commit comments