@@ -487,29 +487,73 @@ func testAdminAPIsDescribeConsumerGroups(
487
487
488
488
func testAdminAPIsDescribeTopics (
489
489
what string , a * AdminClient , expDuration time.Duration , t * testing.T ) {
490
- ctx , cancel := context .WithTimeout (context .Background (), expDuration )
491
- defer cancel ()
492
- descres , err := a .DescribeTopics (
493
- ctx , NewTopicCollectionOfTopicNames (nil ), SetAdminRequestTimeout (time .Second ))
494
- if descres .TopicDescriptions != nil || err == nil {
495
- t .Fatalf ("Expected DescribeTopics to fail, but got result: %v, err: %v" ,
496
- descres , err )
497
- }
498
- if err .(Error ).Code () != ErrInvalidArg {
499
- t .Fatalf ("Expected ErrInvalidArg with empty topics list, but got %s" , err )
500
- }
490
+ requestTimeout := SetAdminRequestTimeout (time .Second )
491
+ for _ , options := range [][]DescribeTopicsAdminOption {
492
+ {},
493
+ {requestTimeout },
494
+ {requestTimeout , SetAdminOptionIncludeAuthorizedOperations (true )},
495
+ {SetAdminOptionIncludeAuthorizedOperations (false )},
496
+ } {
497
+
498
+ // nil slice gives error
499
+ ctx , cancel := context .WithTimeout (context .Background (), expDuration )
500
+ defer cancel ()
501
+ descres , err := a .DescribeTopics (
502
+ ctx , NewTopicCollectionOfTopicNames (nil ), options ... )
503
+ if descres .TopicDescriptions != nil || err == nil {
504
+ t .Fatalf ("Expected DescribeTopics to fail, but got result: %v, err: %v" ,
505
+ descres , err )
506
+ }
507
+ if err .(Error ).Code () != ErrInvalidArg {
508
+ t .Fatalf ("Expected ErrInvalidArg with nil slice, but got %s" , err )
509
+ }
501
510
502
- ctx , cancel = context .WithTimeout (context .Background (), expDuration )
503
- defer cancel ()
504
- descres , err = a .DescribeTopics (
505
- ctx , NewTopicCollectionOfTopicNames ([]string {"test" }),
506
- SetAdminRequestTimeout (time .Second ))
507
- if descres .TopicDescriptions != nil || err == nil {
508
- t .Fatalf ("Expected DescribeTopics to fail, but got result: %v, err: %v" ,
509
- descres , err )
510
- }
511
- if ctx .Err () != context .DeadlineExceeded {
512
- t .Fatalf ("Expected DeadlineExceeded, not %s %v" , err .(Error ).Code (), ctx .Err ())
511
+ // Empty slice returns empty TopicDescription slice
512
+ ctx , cancel = context .WithTimeout (context .Background (), expDuration )
513
+ defer cancel ()
514
+ descres , err = a .DescribeTopics (
515
+ ctx , NewTopicCollectionOfTopicNames ([]string {}), options ... )
516
+ if descres .TopicDescriptions == nil || err != nil {
517
+ t .Fatalf ("Expected DescribeTopics to succeed, but got result: %v, err: %v" ,
518
+ descres , err )
519
+ }
520
+ if len (descres .TopicDescriptions ) > 0 {
521
+ t .Fatalf ("Expected an empty TopicDescription slice, but got %d elements" ,
522
+ len (descres .TopicDescriptions ))
523
+ }
524
+
525
+ // Empty topic names
526
+ for _ , topicCollection := range []TopicCollection {
527
+ NewTopicCollectionOfTopicNames ([]string {"" }),
528
+ NewTopicCollectionOfTopicNames ([]string {"correct" , "" }),
529
+ } {
530
+ ctx , cancel = context .WithTimeout (context .Background (), expDuration )
531
+ defer cancel ()
532
+ descres , err = a .DescribeTopics (
533
+ ctx , topicCollection ,
534
+ options ... )
535
+ if descres .TopicDescriptions != nil || err == nil {
536
+ t .Fatalf ("Expected DescribeTopics to fail, but got result: %v, err: %v" ,
537
+ descres , err )
538
+ }
539
+ if err .(Error ).Code () != ErrInvalidArg {
540
+ t .Fatalf ("Expected ErrInvalidArg, not %d %v" , err .(Error ).Code (), err .Error ())
541
+ }
542
+ }
543
+
544
+ // Normal call
545
+ ctx , cancel = context .WithTimeout (context .Background (), expDuration )
546
+ defer cancel ()
547
+ descres , err = a .DescribeTopics (
548
+ ctx , NewTopicCollectionOfTopicNames ([]string {"test" }),
549
+ options ... )
550
+ if descres .TopicDescriptions != nil || err == nil {
551
+ t .Fatalf ("Expected DescribeTopics to fail, but got result: %v, err: %v" ,
552
+ descres , err )
553
+ }
554
+ if ctx .Err () != context .DeadlineExceeded {
555
+ t .Fatalf ("Expected DeadlineExceeded, not %s %v" , err .(Error ).Code (), ctx .Err ())
556
+ }
513
557
}
514
558
}
515
559
@@ -638,6 +682,135 @@ func testAdminAPIsAlterConsumerGroupOffsets(
638
682
t .Fatalf ("Expected DeadlineExceeded, not %v" , ctx .Err ())
639
683
}
640
684
}
685
+
686
+ func testAdminAPIsListOffsets (
687
+ what string , a * AdminClient , expDuration time.Duration , t * testing.T ) {
688
+ topic := "test"
689
+ invalidTopic := ""
690
+ requestTimeout := SetAdminRequestTimeout (time .Second )
691
+
692
+ // Invalid option value
693
+ ctx , cancel := context .WithTimeout (context .Background (), expDuration )
694
+ defer cancel ()
695
+ result , err := a .ListOffsets (
696
+ ctx ,
697
+ map [TopicPartition ]OffsetSpec {}, SetAdminRequestTimeout (- 1 ))
698
+ if result .ResultInfos != nil || err == nil {
699
+ t .Fatalf ("Expected ListOffsets to fail, but got result: %v, err: %v" ,
700
+ result , err )
701
+ }
702
+ if err .(Error ).Code () != ErrInvalidArg {
703
+ t .Fatalf ("Expected ErrInvalidArg, not %v" , err )
704
+ }
705
+
706
+ for _ , options := range [][]ListOffsetsAdminOption {
707
+ {},
708
+ {requestTimeout },
709
+ {requestTimeout , SetAdminIsolationLevel (IsolationLevelReadUncommitted )},
710
+ {SetAdminIsolationLevel (IsolationLevelReadCommitted )},
711
+ } {
712
+ // nil argument should fail, not being treated as empty
713
+ ctx , cancel := context .WithTimeout (context .Background (), expDuration )
714
+ defer cancel ()
715
+ result , err := a .ListOffsets (
716
+ ctx ,
717
+ nil , options ... )
718
+ if result .ResultInfos != nil || err == nil {
719
+ t .Fatalf ("Expected ListOffsets to fail, but got result: %v, err: %v" ,
720
+ result , err )
721
+ }
722
+ if err .(Error ).Code () != ErrInvalidArg {
723
+ t .Fatalf ("Expected ErrInvalidArg, not %v" , err )
724
+ }
725
+
726
+ // Empty map returns empty ResultInfos
727
+ ctx , cancel = context .WithTimeout (context .Background (), expDuration )
728
+ defer cancel ()
729
+ result , err = a .ListOffsets (
730
+ ctx ,
731
+ map [TopicPartition ]OffsetSpec {}, options ... )
732
+ if result .ResultInfos == nil || err != nil {
733
+ t .Fatalf ("Expected ListOffsets to succeed, but got result: %v, err: %v" ,
734
+ result , err )
735
+ }
736
+ if len (result .ResultInfos ) > 0 {
737
+ t .Fatalf ("Expected empty ResultInfos, not %v" , result .ResultInfos )
738
+ }
739
+
740
+ // Invalid TopicPartition
741
+ for _ , topicPartitionOffsets := range []map [TopicPartition ]OffsetSpec {
742
+ {{Topic : & invalidTopic , Partition : 0 }: EarliestOffsetSpec },
743
+ {{Topic : & topic , Partition : - 1 }: EarliestOffsetSpec },
744
+ } {
745
+ ctx , cancel = context .WithTimeout (context .Background (), expDuration )
746
+ defer cancel ()
747
+ result , err = a .ListOffsets (
748
+ ctx ,
749
+ topicPartitionOffsets , options ... )
750
+ if result .ResultInfos != nil || err == nil {
751
+ t .Fatalf ("Expected ListOffsets to fail, but got result: %v, err: %v" ,
752
+ result , err )
753
+ }
754
+ if err .(Error ).Code () != ErrInvalidArg {
755
+ t .Fatalf ("Expected ErrInvalidArg, not %v" , err )
756
+ }
757
+ }
758
+
759
+ // Same partition with different offsets
760
+ ctx , cancel = context .WithTimeout (context .Background (), expDuration )
761
+ defer cancel ()
762
+ topicPartitionOffsets := map [TopicPartition ]OffsetSpec {
763
+ {Topic : & topic , Partition : 0 , Offset : 10 }: EarliestOffsetSpec ,
764
+ {Topic : & topic , Partition : 0 , Offset : 20 }: LatestOffsetSpec ,
765
+ }
766
+ result , err = a .ListOffsets (
767
+ ctx ,
768
+ topicPartitionOffsets , options ... )
769
+ if result .ResultInfos != nil || err == nil {
770
+ t .Fatalf ("Expected ListOffsets to fail, but got result: %v, err: %v" ,
771
+ result , err )
772
+ }
773
+ if err .(Error ).Code () != ErrInvalidArg {
774
+ t .Fatalf ("Expected ErrInvalidArg, not %v" , err )
775
+ }
776
+
777
+ // Two different partitions
778
+ ctx , cancel = context .WithTimeout (context .Background (), expDuration )
779
+ defer cancel ()
780
+ topicPartitionOffsets = map [TopicPartition ]OffsetSpec {
781
+ {Topic : & topic , Partition : 0 , Offset : 10 }: EarliestOffsetSpec ,
782
+ {Topic : & topic , Partition : 1 , Offset : 20 }: EarliestOffsetSpec ,
783
+ }
784
+ result , err = a .ListOffsets (
785
+ ctx ,
786
+ topicPartitionOffsets , options ... )
787
+ if result .ResultInfos != nil || err == nil {
788
+ t .Fatalf ("Expected ListOffsets to fail, but got result: %v, err: %v" ,
789
+ result , err )
790
+ }
791
+ if ctx .Err () != context .DeadlineExceeded {
792
+ t .Fatalf ("Expected DeadlineExceeded, not %v" , ctx .Err ())
793
+ }
794
+
795
+ // Single partition
796
+ ctx , cancel = context .WithTimeout (context .Background (), expDuration )
797
+ defer cancel ()
798
+ topicPartitionOffsets = map [TopicPartition ]OffsetSpec {
799
+ {Topic : & topic , Partition : 0 }: EarliestOffsetSpec ,
800
+ }
801
+ result , err = a .ListOffsets (
802
+ ctx ,
803
+ topicPartitionOffsets , options ... )
804
+ if result .ResultInfos != nil || err == nil {
805
+ t .Fatalf ("Expected ListOffsets to fail, but got result: %v, err: %v" ,
806
+ result , err )
807
+ }
808
+ if ctx .Err () != context .DeadlineExceeded {
809
+ t .Fatalf ("Expected DeadlineExceeded, not %v" , ctx .Err ())
810
+ }
811
+ }
812
+ }
813
+
641
814
func testAdminAPIsUserScramCredentials (what string , a * AdminClient , expDuration time.Duration , t * testing.T ) {
642
815
var users []string
643
816
@@ -958,6 +1131,7 @@ func testAdminAPIs(what string, a *AdminClient, t *testing.T) {
958
1131
959
1132
testAdminAPIsListConsumerGroupOffsets (what , a , expDuration , t )
960
1133
testAdminAPIsAlterConsumerGroupOffsets (what , a , expDuration , t )
1134
+ testAdminAPIsListOffsets (what , a , expDuration , t )
961
1135
962
1136
testAdminAPIsUserScramCredentials (what , a , expDuration , t )
963
1137
}
0 commit comments