github.com/hoveychen/kafka-go@v0.4.42/protocol/fetch/fetch_test.go (about)

     1  package fetch_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/hoveychen/kafka-go/protocol"
     8  	"github.com/hoveychen/kafka-go/protocol/fetch"
     9  	"github.com/hoveychen/kafka-go/protocol/prototest"
    10  )
    11  
    12  const (
    13  	v0  = 0
    14  	v11 = 11
    15  )
    16  
    17  func TestFetchRequest(t *testing.T) {
    18  	prototest.TestRequest(t, v0, &fetch.Request{
    19  		ReplicaID:   -1,
    20  		MaxWaitTime: 500,
    21  		MinBytes:    1024,
    22  		Topics: []fetch.RequestTopic{
    23  			{
    24  				Topic: "topic-1",
    25  				Partitions: []fetch.RequestPartition{
    26  					{
    27  						Partition:         1,
    28  						FetchOffset:       2,
    29  						PartitionMaxBytes: 1024,
    30  					},
    31  				},
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestFetchResponse(t *testing.T) {
    38  	t0 := time.Now().Truncate(time.Millisecond)
    39  	t1 := t0.Add(1 * time.Millisecond)
    40  	t2 := t0.Add(2 * time.Millisecond)
    41  
    42  	prototest.TestResponse(t, v0, &fetch.Response{
    43  		Topics: []fetch.ResponseTopic{
    44  			{
    45  				Topic: "topic-1",
    46  				Partitions: []fetch.ResponsePartition{
    47  					{
    48  						Partition:     1,
    49  						HighWatermark: 1000,
    50  						RecordSet: protocol.RecordSet{
    51  							Version: 1,
    52  							Records: protocol.NewRecordReader(
    53  								protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0")},
    54  								protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")},
    55  								protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")},
    56  							),
    57  						},
    58  					},
    59  				},
    60  			},
    61  		},
    62  	})
    63  
    64  	headers := []protocol.Header{
    65  		{Key: "key-1", Value: []byte("value-1")},
    66  		{Key: "key-2", Value: []byte("value-2")},
    67  		{Key: "key-3", Value: []byte("value-3")},
    68  	}
    69  
    70  	prototest.TestResponse(t, v11, &fetch.Response{
    71  		Topics: []fetch.ResponseTopic{
    72  			{
    73  				Topic: "topic-1",
    74  				Partitions: []fetch.ResponsePartition{
    75  					{
    76  						Partition:     1,
    77  						HighWatermark: 1000,
    78  						RecordSet: protocol.RecordSet{
    79  							Version: 2,
    80  							Records: protocol.NewRecordReader(
    81  								protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0"), Headers: headers},
    82  								protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")},
    83  								protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")},
    84  							),
    85  						},
    86  					},
    87  				},
    88  			},
    89  		},
    90  	})
    91  }
    92  
    93  func BenchmarkFetchResponse(b *testing.B) {
    94  	t0 := time.Now().Truncate(time.Millisecond)
    95  	t1 := t0.Add(1 * time.Millisecond)
    96  	t2 := t0.Add(2 * time.Millisecond)
    97  
    98  	prototest.BenchmarkResponse(b, v0, &fetch.Response{
    99  		Topics: []fetch.ResponseTopic{
   100  			{
   101  				Topic: "topic-1",
   102  				Partitions: []fetch.ResponsePartition{
   103  					{
   104  						Partition:     1,
   105  						HighWatermark: 1000,
   106  						RecordSet: protocol.RecordSet{
   107  							Version: 1,
   108  							Records: protocol.NewRecordReader(
   109  								protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0")},
   110  								protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")},
   111  								protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")},
   112  							),
   113  						},
   114  					},
   115  				},
   116  			},
   117  		},
   118  	})
   119  
   120  	headers := []protocol.Header{
   121  		{Key: "key-1", Value: []byte("value-1")},
   122  		{Key: "key-2", Value: []byte("value-2")},
   123  		{Key: "key-3", Value: []byte("value-3")},
   124  	}
   125  
   126  	prototest.BenchmarkResponse(b, v11, &fetch.Response{
   127  		Topics: []fetch.ResponseTopic{
   128  			{
   129  				Topic: "topic-1",
   130  				Partitions: []fetch.ResponsePartition{
   131  					{
   132  						Partition:     1,
   133  						HighWatermark: 1000,
   134  						RecordSet: protocol.RecordSet{
   135  							Version: 2,
   136  							Records: protocol.NewRecordReader(
   137  								protocol.Record{Offset: 0, Time: t0, Key: nil, Value: prototest.String("msg-0"), Headers: headers},
   138  								protocol.Record{Offset: 1, Time: t1, Key: nil, Value: prototest.String("msg-1")},
   139  								protocol.Record{Offset: 2, Time: t2, Key: prototest.Bytes([]byte{1}), Value: prototest.String("msg-2")},
   140  							),
   141  						},
   142  					},
   143  				},
   144  			},
   145  		},
   146  	})
   147  }