| 1 |
package kafka |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"net" |
| 6 |
"strconv" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
ktesting "github.com/segmentio/kafka-go/testing" |
| 11 |
) |
| 12 |
|
| 13 |
func TestClientAddPartitionsToTxn(t *testing.T) { |
| 14 |
if !ktesting.KafkaIsAtLeast("0.11.0") { |
| 15 |
t.Skip("Skipping test because kafka version is not high enough.") |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
if ktesting.KafkaIsAtLeast("3.0.0") { |
| 21 |
t.Skip("Skipping test because it fails on Kafka version 3.0.0 or higher.") |
| 22 |
} |
| 23 |
|
| 24 |
topic1 := makeTopic() |
| 25 |
topic2 := makeTopic() |
| 26 |
|
| 27 |
client, shutdown := newLocalClient() |
| 28 |
defer shutdown() |
| 29 |
|
| 30 |
err := clientCreateTopic(client, topic1, 3) |
| 31 |
if err != nil { |
| 32 |
t.Fatal(err) |
| 33 |
} |
| 34 |
|
| 35 |
err = clientCreateTopic(client, topic2, 3) |
| 36 |
if err != nil { |
| 37 |
t.Fatal(err) |
| 38 |
} |
| 39 |
|
| 40 |
transactionalID := makeTransactionalID() |
| 41 |
|
| 42 |
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
| 43 |
defer cancel() |
| 44 |
respc, err := waitForCoordinatorIndefinitely(ctx, client, &FindCoordinatorRequest{ |
| 45 |
Addr: client.Addr, |
| 46 |
Key: transactionalID, |
| 47 |
KeyType: CoordinatorKeyTypeTransaction, |
| 48 |
}) |
| 49 |
if err != nil { |
| 50 |
t.Fatal(err) |
| 51 |
} |
| 52 |
|
| 53 |
transactionCoordinator := TCP(net.JoinHostPort(respc.Coordinator.Host, strconv.Itoa(int(respc.Coordinator.Port)))) |
| 54 |
client, shutdown = newClient(transactionCoordinator) |
| 55 |
defer shutdown() |
| 56 |
|
| 57 |
ipResp, err := client.InitProducerID(ctx, &InitProducerIDRequest{ |
| 58 |
TransactionalID: transactionalID, |
| 59 |
TransactionTimeoutMs: 10000, |
| 60 |
}) |
| 61 |
if err != nil { |
| 62 |
t.Fatal(err) |
| 63 |
} |
| 64 |
|
| 65 |
if ipResp.Error != nil { |
| 66 |
t.Fatal(ipResp.Error) |
| 67 |
} |
| 68 |
|
| 69 |
defer func() { |
| 70 |
err := clientEndTxn(client, &EndTxnRequest{ |
| 71 |
TransactionalID: transactionalID, |
| 72 |
ProducerID: ipResp.Producer.ProducerID, |
| 73 |
ProducerEpoch: ipResp.Producer.ProducerEpoch, |
| 74 |
Committed: false, |
| 75 |
}) |
| 76 |
if err != nil { |
| 77 |
t.Fatal(err) |
| 78 |
} |
| 79 |
}() |
| 80 |
|
| 81 |
ctx, cancel = context.WithTimeout(context.Background(), time.Second*30) |
| 82 |
defer cancel() |
| 83 |
resp, err := client.AddPartitionsToTxn(ctx, &AddPartitionsToTxnRequest{ |
| 84 |
TransactionalID: transactionalID, |
| 85 |
ProducerID: ipResp.Producer.ProducerID, |
| 86 |
ProducerEpoch: ipResp.Producer.ProducerEpoch, |
| 87 |
Topics: map[string][]AddPartitionToTxn{ |
| 88 |
topic1: { |
| 89 |
{ |
| 90 |
Partition: 0, |
| 91 |
}, |
| 92 |
{ |
| 93 |
Partition: 1, |
| 94 |
}, |
| 95 |
{ |
| 96 |
Partition: 2, |
| 97 |
}, |
| 98 |
}, |
| 99 |
topic2: { |
| 100 |
{ |
| 101 |
Partition: 0, |
| 102 |
}, |
| 103 |
{ |
| 104 |
Partition: 2, |
| 105 |
}, |
| 106 |
}, |
| 107 |
}, |
| 108 |
}) |
| 109 |
if err != nil { |
| 110 |
t.Fatal(err) |
| 111 |
} |
| 112 |
|
| 113 |
if len(resp.Topics) != 2 { |
| 114 |
t.Errorf("expected responses for 2 topics; got: %d", len(resp.Topics)) |
| 115 |
} |
| 116 |
for topic, partitions := range resp.Topics { |
| 117 |
if topic == topic1 { |
| 118 |
if len(partitions) != 3 { |
| 119 |
t.Errorf("expected 3 partitions in response for topic %s; got: %d", topic, len(partitions)) |
| 120 |
} |
| 121 |
} |
| 122 |
if topic == topic2 { |
| 123 |
if len(partitions) != 2 { |
| 124 |
t.Errorf("expected 2 partitions in response for topic %s; got: %d", topic, len(partitions)) |
| 125 |
} |
| 126 |
} |
| 127 |
for _, partition := range partitions { |
| 128 |
if partition.Error != nil { |
| 129 |
t.Error(partition.Error) |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |