github.com/rbisecke/kafka-go@v0.4.27/protocol_test.go (about)

     1  package kafka
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"fmt"
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func TestApiVersionsFormat(t *testing.T) {
    12  	for _, test := range []struct {
    13  		version ApiVersion
    14  		format  string
    15  		output  string
    16  	}{
    17  		{version: ApiVersion{1, 2, 5}, format: "%s", output: "Fetch"},
    18  		{version: ApiVersion{1, 2, 5}, format: "%d", output: "1"},
    19  		{version: ApiVersion{1, 2, 5}, format: "%-d", output: "2"},
    20  		{version: ApiVersion{1, 2, 5}, format: "%+d", output: "5"},
    21  		{version: ApiVersion{1, 2, 5}, format: "%v", output: "Fetch[v2:v5]"},
    22  		{version: ApiVersion{1, 2, 5}, format: "%-v", output: "v2"},
    23  		{version: ApiVersion{1, 2, 5}, format: "%+v", output: "v5"},
    24  		{version: ApiVersion{1, 2, 5}, format: "%#v", output: "kafka.ApiVersion{ApiKey:1 MinVersion:2 MaxVersion:5}"},
    25  	} {
    26  		t.Run(test.output, func(t *testing.T) {
    27  			if s := fmt.Sprintf(test.format, test.version); s != test.output {
    28  				t.Error("output mismatch:", s, "!=", test.output)
    29  			}
    30  		})
    31  	}
    32  }
    33  
    34  func TestProtocol(t *testing.T) {
    35  	tests := []interface{}{
    36  		int8(42),
    37  		int16(42),
    38  		int32(42),
    39  		int64(42),
    40  		"",
    41  		"Hello World!",
    42  		[]byte(nil),
    43  		[]byte("Hello World!"),
    44  
    45  		requestHeader{
    46  			Size:          26,
    47  			ApiKey:        int16(offsetCommit),
    48  			ApiVersion:    int16(v2),
    49  			CorrelationID: 42,
    50  			ClientID:      "Hello World!",
    51  		},
    52  
    53  		message{
    54  			MagicByte: 1,
    55  			Timestamp: 42,
    56  			Key:       nil,
    57  			Value:     []byte("Hello World!"),
    58  		},
    59  
    60  		topicMetadataRequestV1{"A", "B", "C"},
    61  
    62  		metadataResponseV1{
    63  			Brokers: []brokerMetadataV1{
    64  				{NodeID: 1, Host: "localhost", Port: 9001},
    65  				{NodeID: 2, Host: "localhost", Port: 9002, Rack: "rack2"},
    66  			},
    67  			ControllerID: 2,
    68  			Topics: []topicMetadataV1{
    69  				{TopicErrorCode: 0, Internal: true, Partitions: []partitionMetadataV1{{
    70  					PartitionErrorCode: 0,
    71  					PartitionID:        1,
    72  					Leader:             2,
    73  					Replicas:           []int32{1},
    74  					Isr:                []int32{1},
    75  				}}},
    76  			},
    77  		},
    78  
    79  		listOffsetRequestV1{
    80  			ReplicaID: 1,
    81  			Topics: []listOffsetRequestTopicV1{
    82  				{TopicName: "A", Partitions: []listOffsetRequestPartitionV1{
    83  					{Partition: 0, Time: -1},
    84  					{Partition: 1, Time: -1},
    85  					{Partition: 2, Time: -1},
    86  				}},
    87  				{TopicName: "B", Partitions: []listOffsetRequestPartitionV1{
    88  					{Partition: 0, Time: -2},
    89  				}},
    90  				{TopicName: "C", Partitions: []listOffsetRequestPartitionV1{
    91  					{Partition: 0, Time: 42},
    92  				}},
    93  			},
    94  		},
    95  
    96  		listOffsetResponseV1{
    97  			{TopicName: "A", PartitionOffsets: []partitionOffsetV1{
    98  				{Partition: 0, Timestamp: 42, Offset: 1},
    99  			}},
   100  			{TopicName: "B", PartitionOffsets: []partitionOffsetV1{
   101  				{Partition: 0, Timestamp: 43, Offset: 10},
   102  				{Partition: 1, Timestamp: 44, Offset: 100},
   103  			}},
   104  		},
   105  	}
   106  
   107  	for _, test := range tests {
   108  		t.Run(fmt.Sprintf("%T", test), func(t *testing.T) {
   109  			b := &bytes.Buffer{}
   110  			r := bufio.NewReader(b)
   111  			w := &writeBuffer{w: b}
   112  			w.write(test)
   113  
   114  			if size := int(sizeof(test)); size != b.Len() {
   115  				t.Error("invalid size:", size, "!=", b.Len())
   116  			}
   117  
   118  			v := reflect.New(reflect.TypeOf(test))
   119  			n := b.Len()
   120  
   121  			n, err := read(r, n, v.Interface())
   122  			if err != nil {
   123  				t.Fatal(err)
   124  			}
   125  			if n != 0 {
   126  				t.Errorf("%d unread bytes", n)
   127  			}
   128  
   129  			if !reflect.DeepEqual(test, v.Elem().Interface()) {
   130  				t.Error("values don't match:")
   131  				t.Logf("expected: %#v", test)
   132  				t.Logf("found:    %#v", v.Elem().Interface())
   133  			}
   134  		})
   135  	}
   136  }