github.com/hoveychen/kafka-go@v0.4.42/offsetdelete_test.go (about) 1 package kafka 2 3 import ( 4 "context" 5 "log" 6 "os" 7 "strconv" 8 "testing" 9 "time" 10 11 ktesting "github.com/hoveychen/kafka-go/testing" 12 ) 13 14 func TestClientDeleteOffset(t *testing.T) { 15 if !ktesting.KafkaIsAtLeast("2.4.0") { 16 return 17 } 18 19 topic := makeTopic() 20 client, shutdown := newLocalClientWithTopic(topic, 3) 21 defer shutdown() 22 now := time.Now() 23 24 const N = 10 * 3 25 records := make([]Record, 0, N) 26 for i := 0; i < N; i++ { 27 records = append(records, Record{ 28 Time: now, 29 Value: NewBytes([]byte("test-message-" + strconv.Itoa(i))), 30 }) 31 } 32 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) 33 defer cancel() 34 res, err := client.Produce(ctx, &ProduceRequest{ 35 Topic: topic, 36 RequiredAcks: RequireAll, 37 Records: NewRecordReader(records...), 38 }) 39 if err != nil { 40 t.Fatal(err) 41 } 42 43 if res.Error != nil { 44 t.Error(res.Error) 45 } 46 47 for index, err := range res.RecordErrors { 48 t.Fatalf("record at index %d produced an error: %v", index, err) 49 } 50 ctx, cancel = context.WithTimeout(context.Background(), time.Second*30) 51 defer cancel() 52 groupID := makeGroupID() 53 54 group, err := NewConsumerGroup(ConsumerGroupConfig{ 55 ID: groupID, 56 Topics: []string{topic}, 57 Brokers: []string{"localhost:9092"}, 58 HeartbeatInterval: 2 * time.Second, 59 RebalanceTimeout: 2 * time.Second, 60 RetentionTime: time.Hour, 61 Logger: log.New(os.Stdout, "cg-test: ", 0), 62 }) 63 if err != nil { 64 t.Fatal(err) 65 } 66 67 gen, err := group.Next(ctx) 68 if err != nil { 69 t.Fatal(err) 70 } 71 72 ocr, err := client.OffsetCommit(ctx, &OffsetCommitRequest{ 73 Addr: nil, 74 GroupID: groupID, 75 GenerationID: int(gen.ID), 76 MemberID: gen.MemberID, 77 Topics: map[string][]OffsetCommit{ 78 topic: { 79 {Partition: 0, Offset: 10}, 80 {Partition: 1, Offset: 10}, 81 {Partition: 2, Offset: 10}, 82 }, 83 }, 84 }) 85 if err != nil { 86 t.Fatal(err) 87 } 88 89 group.Close() 90 91 resps := ocr.Topics[topic] 92 if len(resps) != 3 { 93 t.Fatalf("expected 3 offsetcommitpartition responses; got %d", len(resps)) 94 } 95 96 for _, resp := range resps { 97 if resp.Error != nil { 98 t.Fatal(resp.Error) 99 } 100 } 101 102 ofr, err := client.OffsetFetch(ctx, &OffsetFetchRequest{ 103 GroupID: groupID, 104 Topics: map[string][]int{topic: {0, 1, 2}}, 105 }) 106 if err != nil { 107 t.Fatal(err) 108 } 109 110 if ofr.Error != nil { 111 t.Error(res.Error) 112 } 113 114 fetresps := ofr.Topics[topic] 115 if len(fetresps) != 3 { 116 t.Fatalf("expected 3 offsetfetchpartition responses; got %d", len(resps)) 117 } 118 119 for _, r := range fetresps { 120 if r.Error != nil { 121 t.Fatal(r.Error) 122 } 123 124 if r.CommittedOffset != 10 { 125 t.Fatalf("expected committed offset to be 10; got: %v for partition: %v", r.CommittedOffset, r.Partition) 126 } 127 } 128 129 // Remove offsets 130 odr, err := client.OffsetDelete(ctx, &OffsetDeleteRequest{ 131 GroupID: groupID, 132 Topics: map[string][]int{topic: {0, 1, 2}}, 133 }) 134 if err != nil { 135 t.Fatal(err) 136 } 137 138 if odr.Error != nil { 139 t.Error(odr.Error) 140 } 141 142 // Fetch the offsets again 143 ofr, err = client.OffsetFetch(ctx, &OffsetFetchRequest{ 144 GroupID: groupID, 145 Topics: map[string][]int{topic: {0, 1, 2}}, 146 }) 147 if err != nil { 148 t.Fatal(err) 149 } 150 151 if ofr.Error != nil { 152 t.Error(res.Error) 153 } 154 155 for _, r := range ofr.Topics[topic] { 156 if r.CommittedOffset != -1 { 157 t.Fatalf("expected committed offset to be -1; got: %v for partition: %v", r.CommittedOffset, r.Partition) 158 } 159 } 160 }