github.com/streamdal/segmentio-kafka-go@v0.4.47-streamdal/initproducerid_test.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"net"
     7  	"strconv"
     8  	"testing"
     9  	"time"
    10  
    11  	ktesting "github.com/segmentio/kafka-go/testing"
    12  )
    13  
    14  func TestClientInitProducerId(t *testing.T) {
    15  	if !ktesting.KafkaIsAtLeast("0.11.0") {
    16  		return
    17  	}
    18  	client, shutdown := newLocalClient()
    19  	defer shutdown()
    20  
    21  	tid := makeTransactionalID()
    22  	// Wait for kafka setup and Coordinator to be available.
    23  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    24  	defer cancel()
    25  	respc, err := waitForCoordinatorIndefinitely(ctx, client, &FindCoordinatorRequest{
    26  		Addr:    client.Addr,
    27  		Key:     tid,
    28  		KeyType: CoordinatorKeyTypeTransaction,
    29  	})
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	// Now establish a connection with the transaction coordinator
    35  	transactionCoordinator := TCP(net.JoinHostPort(respc.Coordinator.Host, strconv.Itoa(int(respc.Coordinator.Port))))
    36  	client, shutdown = newClient(transactionCoordinator)
    37  	defer shutdown()
    38  
    39  	// Check if producer epoch increases and PID remains the same when producer is
    40  	// initialized again with the same transactionalID
    41  	resp, err := client.InitProducerID(context.Background(), &InitProducerIDRequest{
    42  		Addr:                 transactionCoordinator,
    43  		TransactionalID:      tid,
    44  		TransactionTimeoutMs: 30000,
    45  	})
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	if resp.Error != nil {
    51  		t.Fatal(resp.Error)
    52  	}
    53  
    54  	epoch1 := resp.Producer.ProducerEpoch
    55  	pid1 := resp.Producer.ProducerID
    56  
    57  	resp, err = client.InitProducerID(context.Background(), &InitProducerIDRequest{
    58  		Addr:                 transactionCoordinator,
    59  		TransactionalID:      tid,
    60  		TransactionTimeoutMs: 30000,
    61  		ProducerID:           pid1,
    62  		ProducerEpoch:        epoch1,
    63  	})
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  
    68  	if resp.Error != nil {
    69  		t.Fatal(resp.Error)
    70  	}
    71  
    72  	epoch2 := resp.Producer.ProducerEpoch
    73  	pid2 := resp.Producer.ProducerID
    74  
    75  	if pid1 != pid2 {
    76  		t.Fatalf("PID should stay the same across producer sessions; expected: %v got: %v", pid1, pid2)
    77  	}
    78  
    79  	if epoch2-epoch1 <= 0 {
    80  		t.Fatal("Epoch should increase when producer is initialized again with the same transactionID")
    81  	}
    82  
    83  	// Checks if transaction timeout is too high
    84  	// Transaction timeout should never be higher than broker config `transaction.max.timeout.ms`
    85  	resp, _ = client.InitProducerID(context.Background(), &InitProducerIDRequest{
    86  		Addr:                 client.Addr,
    87  		TransactionalID:      tid,
    88  		TransactionTimeoutMs: 30000000,
    89  	})
    90  	if !errors.Is(resp.Error, InvalidTransactionTimeout) {
    91  		t.Fatal("Should have errored with: Transaction timeout specified is higher than `transaction.max.timeout.ms`")
    92  	}
    93  }