github.com/hoveychen/kafka-go@v0.4.42/listoffset_test.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestClientListOffsets(t *testing.T) {
    10  	client, topic, shutdown := newLocalClientAndTopic()
    11  	defer shutdown()
    12  
    13  	now := time.Now()
    14  
    15  	_, err := client.Produce(context.Background(), &ProduceRequest{
    16  		Topic:        topic,
    17  		Partition:    0,
    18  		RequiredAcks: -1,
    19  		Records: NewRecordReader(
    20  			Record{Time: now, Value: NewBytes([]byte(`hello-1`))},
    21  			Record{Time: now, Value: NewBytes([]byte(`hello-2`))},
    22  			Record{Time: now, Value: NewBytes([]byte(`hello-3`))},
    23  		),
    24  	})
    25  
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	res, err := client.ListOffsets(context.Background(), &ListOffsetsRequest{
    31  		Topics: map[string][]OffsetRequest{
    32  			topic: {FirstOffsetOf(0), LastOffsetOf(0)},
    33  		},
    34  	})
    35  
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  
    40  	if len(res.Topics) != 1 {
    41  		t.Fatal("invalid number of topics found in list offsets response:", len(res.Topics))
    42  	}
    43  
    44  	partitions, ok := res.Topics[topic]
    45  	if !ok {
    46  		t.Fatal("missing topic in the list offsets response:", topic)
    47  	}
    48  	if len(partitions) != 1 {
    49  		t.Fatal("invalid number of partitions found in list offsets response:", len(partitions))
    50  	}
    51  	partition := partitions[0]
    52  
    53  	if partition.Partition != 0 {
    54  		t.Error("invalid partition id found in list offsets response:", partition.Partition)
    55  	}
    56  
    57  	if partition.FirstOffset != 0 {
    58  		t.Error("invalid first offset found in list offsets response:", partition.FirstOffset)
    59  	}
    60  
    61  	if partition.LastOffset != 3 {
    62  		t.Error("invalid last offset found in list offsets response:", partition.LastOffset)
    63  	}
    64  
    65  	if firstOffsetTime := partition.Offsets[partition.FirstOffset]; !firstOffsetTime.IsZero() {
    66  		t.Error("unexpected first offset time in list offsets response:", partition.Offsets)
    67  	}
    68  
    69  	if lastOffsetTime := partition.Offsets[partition.LastOffset]; !lastOffsetTime.IsZero() {
    70  		t.Error("unexpected last offset time in list offsets response:", partition.Offsets)
    71  	}
    72  
    73  	if partition.Error != nil {
    74  		t.Error("unexpected error in list offsets response:", partition.Error)
    75  	}
    76  }