github.com/QuangHoangHao/kafka-go@v0.4.36/createtopics_test.go (about)

     1  package kafka
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"context"
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func TestClientCreateTopics(t *testing.T) {
    12  	const (
    13  		topic1 = "client-topic-1"
    14  		topic2 = "client-topic-2"
    15  		topic3 = "client-topic-3"
    16  	)
    17  
    18  	client, shutdown := newLocalClient()
    19  	defer shutdown()
    20  
    21  	config := []ConfigEntry{{
    22  		ConfigName:  "retention.ms",
    23  		ConfigValue: "3600000",
    24  	}}
    25  
    26  	res, err := client.CreateTopics(context.Background(), &CreateTopicsRequest{
    27  		Topics: []TopicConfig{
    28  			{
    29  				Topic:             topic1,
    30  				NumPartitions:     -1,
    31  				ReplicationFactor: -1,
    32  				ReplicaAssignments: []ReplicaAssignment{
    33  					{
    34  						Partition: 0,
    35  						Replicas:  []int{1},
    36  					},
    37  					{
    38  						Partition: 1,
    39  						Replicas:  []int{1},
    40  					},
    41  					{
    42  						Partition: 2,
    43  						Replicas:  []int{1},
    44  					},
    45  				},
    46  				ConfigEntries: config,
    47  			},
    48  			{
    49  				Topic:             topic2,
    50  				NumPartitions:     2,
    51  				ReplicationFactor: 1,
    52  				ConfigEntries:     config,
    53  			},
    54  			{
    55  				Topic:             topic3,
    56  				NumPartitions:     1,
    57  				ReplicationFactor: 1,
    58  				ConfigEntries:     config,
    59  			},
    60  		},
    61  	})
    62  
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  
    67  	defer deleteTopic(t, topic1, topic2, topic3)
    68  
    69  	expectTopics := map[string]struct{}{
    70  		topic1: {},
    71  		topic2: {},
    72  		topic3: {},
    73  	}
    74  
    75  	for topic, error := range res.Errors {
    76  		delete(expectTopics, topic)
    77  
    78  		if error != nil {
    79  			t.Errorf("%s => %s", topic, error)
    80  		}
    81  	}
    82  
    83  	for topic := range expectTopics {
    84  		t.Errorf("topic missing in response: %s", topic)
    85  	}
    86  }
    87  
    88  func TestCreateTopicsResponseV0(t *testing.T) {
    89  	item := createTopicsResponseV0{
    90  		TopicErrors: []createTopicsResponseV0TopicError{
    91  			{
    92  				Topic:     "topic",
    93  				ErrorCode: 2,
    94  			},
    95  		},
    96  	}
    97  
    98  	b := bytes.NewBuffer(nil)
    99  	w := &writeBuffer{w: b}
   100  	item.writeTo(w)
   101  
   102  	var found createTopicsResponseV0
   103  	remain, err := (&found).readFrom(bufio.NewReader(b), b.Len())
   104  	if err != nil {
   105  		t.Error(err)
   106  		t.FailNow()
   107  	}
   108  	if remain != 0 {
   109  		t.Errorf("expected 0 remain, got %v", remain)
   110  		t.FailNow()
   111  	}
   112  	if !reflect.DeepEqual(item, found) {
   113  		t.Error("expected item and found to be the same")
   114  		t.FailNow()
   115  	}
   116  }