github.com/QuangHoangHao/kafka-go@v0.4.36/addoffsetstotxn_test.go (about) 1 package kafka 2 3 import ( 4 "context" 5 "log" 6 "net" 7 "os" 8 "strconv" 9 "testing" 10 "time" 11 12 ktesting "github.com/QuangHoangHao/kafka-go/testing" 13 ) 14 15 func TestClientAddOffsetsToTxn(t *testing.T) { 16 if !ktesting.KafkaIsAtLeast("0.11.0") { 17 t.Skip("Skipping test because kafka version is not high enough.") 18 } 19 topic := makeTopic() 20 transactionalID := makeTransactionalID() 21 client, shutdown := newLocalClient() 22 defer shutdown() 23 24 err := clientCreateTopic(client, topic, 3) 25 if err != nil { 26 t.Fatal(err) 27 } 28 29 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) 30 defer cancel() 31 respc, err := waitForCoordinatorIndefinitely(ctx, client, &FindCoordinatorRequest{ 32 Addr: client.Addr, 33 Key: transactionalID, 34 KeyType: CoordinatorKeyTypeConsumer, 35 }) 36 if err != nil { 37 t.Fatal(err) 38 } 39 40 if respc.Error != nil { 41 t.Fatal(err) 42 } 43 44 groupID := makeGroupID() 45 46 group, err := NewConsumerGroup(ConsumerGroupConfig{ 47 ID: groupID, 48 Topics: []string{topic}, 49 Brokers: []string{"localhost:9092"}, 50 HeartbeatInterval: 2 * time.Second, 51 RebalanceTimeout: 2 * time.Second, 52 RetentionTime: time.Hour, 53 Logger: log.New(os.Stdout, "cg-test: ", 0), 54 }) 55 if err != nil { 56 t.Fatal(err) 57 } 58 defer group.Close() 59 60 ctx, cancel = context.WithTimeout(context.Background(), time.Second*30) 61 defer cancel() 62 _, err = group.Next(ctx) 63 if err != nil { 64 t.Fatal(err) 65 } 66 67 ctx, cancel = context.WithTimeout(context.Background(), time.Second*30) 68 defer cancel() 69 respc, err = waitForCoordinatorIndefinitely(ctx, client, &FindCoordinatorRequest{ 70 Addr: client.Addr, 71 Key: transactionalID, 72 KeyType: CoordinatorKeyTypeTransaction, 73 }) 74 if err != nil { 75 t.Fatal(err) 76 } 77 78 transactionCoordinator := TCP(net.JoinHostPort(respc.Coordinator.Host, strconv.Itoa(int(respc.Coordinator.Port)))) 79 client, shutdown = newClient(transactionCoordinator) 80 defer shutdown() 81 82 ipResp, err := client.InitProducerID(ctx, &InitProducerIDRequest{ 83 TransactionalID: transactionalID, 84 TransactionTimeoutMs: 10000, 85 }) 86 if err != nil { 87 t.Fatal(err) 88 } 89 90 if ipResp.Error != nil { 91 t.Fatal(ipResp.Error) 92 } 93 94 defer func() { 95 err := clientEndTxn(client, &EndTxnRequest{ 96 TransactionalID: transactionalID, 97 ProducerID: ipResp.Producer.ProducerID, 98 ProducerEpoch: ipResp.Producer.ProducerEpoch, 99 Committed: false, 100 }) 101 if err != nil { 102 t.Fatal(err) 103 } 104 }() 105 106 ctx, cancel = context.WithTimeout(context.Background(), time.Second*30) 107 defer cancel() 108 109 resp, err := client.AddOffsetsToTxn(ctx, &AddOffsetsToTxnRequest{ 110 TransactionalID: transactionalID, 111 ProducerID: ipResp.Producer.ProducerID, 112 ProducerEpoch: ipResp.Producer.ProducerEpoch, 113 GroupID: groupID, 114 }) 115 if err != nil { 116 t.Fatal(err) 117 } 118 119 if resp.Error != nil { 120 t.Fatal(err) 121 } 122 }