github.com/streamdal/segmentio-kafka-go@v0.4.47-streamdal/offsetfetch_test.go (about) 1 package kafka 2 3 import ( 4 "bufio" 5 "bytes" 6 "context" 7 "reflect" 8 "testing" 9 "time" 10 11 ktesting "github.com/segmentio/kafka-go/testing" 12 ) 13 14 func TestOffsetFetchResponseV1(t *testing.T) { 15 item := offsetFetchResponseV1{ 16 Responses: []offsetFetchResponseV1Response{ 17 { 18 Topic: "a", 19 PartitionResponses: []offsetFetchResponseV1PartitionResponse{ 20 { 21 Partition: 2, 22 Offset: 3, 23 Metadata: "b", 24 ErrorCode: 4, 25 }, 26 }, 27 }, 28 }, 29 } 30 31 b := bytes.NewBuffer(nil) 32 w := &writeBuffer{w: b} 33 item.writeTo(w) 34 35 var found offsetFetchResponseV1 36 remain, err := (&found).readFrom(bufio.NewReader(b), b.Len()) 37 if err != nil { 38 t.Error(err) 39 t.FailNow() 40 } 41 if remain != 0 { 42 t.Errorf("expected 0 remain, got %v", remain) 43 t.FailNow() 44 } 45 if !reflect.DeepEqual(item, found) { 46 t.Error("expected item and found to be the same") 47 t.FailNow() 48 } 49 } 50 51 func TestOffsetFetchRequestWithNoTopic(t *testing.T) { 52 if !ktesting.KafkaIsAtLeast("0.10.2.0") { 53 t.Logf("Test %s is not applicable for kafka versions below 0.10.2.0", t.Name()) 54 t.SkipNow() 55 } 56 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) 57 topic1 := makeTopic() 58 defer deleteTopic(t, topic1) 59 topic2 := makeTopic() 60 defer deleteTopic(t, topic2) 61 consumeGroup := makeGroupID() 62 numMsgs := 50 63 defer cancel() 64 r1 := NewReader(ReaderConfig{ 65 Brokers: []string{"localhost:9092"}, 66 Topic: topic1, 67 GroupID: consumeGroup, 68 MinBytes: 1, 69 MaxBytes: 100, 70 MaxWait: 100 * time.Millisecond, 71 }) 72 defer r1.Close() 73 prepareReader(t, ctx, r1, makeTestSequence(numMsgs)...) 74 r2 := NewReader(ReaderConfig{ 75 Brokers: []string{"localhost:9092"}, 76 Topic: topic2, 77 GroupID: consumeGroup, 78 MinBytes: 1, 79 MaxBytes: 100, 80 MaxWait: 100 * time.Millisecond, 81 }) 82 defer r2.Close() 83 prepareReader(t, ctx, r2, makeTestSequence(numMsgs)...) 84 85 for i := 0; i < numMsgs; i++ { 86 if _, err := r1.ReadMessage(ctx); err != nil { 87 t.Fatal(err) 88 } 89 } 90 for i := 0; i < numMsgs; i++ { 91 if _, err := r2.ReadMessage(ctx); err != nil { 92 t.Fatal(err) 93 } 94 } 95 96 client := Client{Addr: TCP("localhost:9092")} 97 98 topicOffsets, err := client.OffsetFetch(ctx, &OffsetFetchRequest{GroupID: consumeGroup}) 99 100 if err != nil { 101 t.Error(err) 102 t.FailNow() 103 } 104 105 if len(topicOffsets.Topics) != 2 { 106 t.Error(err) 107 t.FailNow() 108 } 109 110 } 111 112 func TestOffsetFetchRequestWithOneTopic(t *testing.T) { 113 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) 114 topic1 := makeTopic() 115 defer deleteTopic(t, topic1) 116 topic2 := makeTopic() 117 defer deleteTopic(t, topic2) 118 consumeGroup := makeGroupID() 119 numMsgs := 50 120 defer cancel() 121 r1 := NewReader(ReaderConfig{ 122 Brokers: []string{"localhost:9092"}, 123 Topic: topic1, 124 GroupID: consumeGroup, 125 MinBytes: 1, 126 MaxBytes: 100, 127 MaxWait: 100 * time.Millisecond, 128 }) 129 defer r1.Close() 130 prepareReader(t, ctx, r1, makeTestSequence(numMsgs)...) 131 r2 := NewReader(ReaderConfig{ 132 Brokers: []string{"localhost:9092"}, 133 Topic: topic2, 134 GroupID: consumeGroup, 135 MinBytes: 1, 136 MaxBytes: 100, 137 MaxWait: 100 * time.Millisecond, 138 }) 139 defer r2.Close() 140 prepareReader(t, ctx, r2, makeTestSequence(numMsgs)...) 141 142 for i := 0; i < numMsgs; i++ { 143 if _, err := r1.ReadMessage(ctx); err != nil { 144 t.Fatal(err) 145 } 146 } 147 for i := 0; i < numMsgs; i++ { 148 if _, err := r2.ReadMessage(ctx); err != nil { 149 t.Fatal(err) 150 } 151 } 152 153 client := Client{Addr: TCP("localhost:9092")} 154 topicOffsets, err := client.OffsetFetch(ctx, &OffsetFetchRequest{GroupID: consumeGroup, Topics: map[string][]int{ 155 topic1: {0}, 156 }}) 157 158 if err != nil { 159 t.Error(err) 160 t.FailNow() 161 } 162 163 if len(topicOffsets.Topics) != 1 { 164 t.Error(err) 165 t.FailNow() 166 } 167 }