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

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"reflect"
     8  	"sort"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestClientDescribeGroups(t *testing.T) {
    14  	if os.Getenv("KAFKA_VERSION") == "2.3.1" {
    15  		// There's a bug in 2.3.1 that causes the MemberMetadata to be in the wrong format and thus
    16  		// leads to an error when decoding the DescribeGroupsResponse.
    17  		//
    18  		// See https://issues.apache.org/jira/browse/KAFKA-9150 for details.
    19  		t.Skip("Skipping because kafka version is 2.3.1")
    20  	}
    21  
    22  	client, shutdown := newLocalClient()
    23  	defer shutdown()
    24  
    25  	topic := makeTopic()
    26  	gid := fmt.Sprintf("%s-test-group", topic)
    27  
    28  	createTopic(t, topic, 2)
    29  	defer deleteTopic(t, topic)
    30  
    31  	w := newTestWriter(WriterConfig{
    32  		Topic: topic,
    33  	})
    34  
    35  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    36  	defer cancel()
    37  
    38  	err := w.WriteMessages(
    39  		ctx,
    40  		Message{
    41  			Key:   []byte("key"),
    42  			Value: []byte("value"),
    43  		},
    44  	)
    45  
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	r := NewReader(ReaderConfig{
    51  		Brokers:  []string{"localhost:9092"},
    52  		Topic:    topic,
    53  		GroupID:  gid,
    54  		MinBytes: 10,
    55  		MaxBytes: 1000,
    56  	})
    57  	_, err = r.ReadMessage(ctx)
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	resp, err := client.DescribeGroups(
    63  		ctx,
    64  		&DescribeGroupsRequest{
    65  			GroupIDs: []string{gid},
    66  		},
    67  	)
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	if len(resp.Groups) != 1 {
    72  		t.Fatal(
    73  			"Unexpected number of groups returned",
    74  			"expected", 1,
    75  			"got", len(resp.Groups),
    76  		)
    77  	}
    78  	g := resp.Groups[0]
    79  	if g.Error != nil {
    80  		t.Error(
    81  			"Wrong error in group response",
    82  			"expected", nil,
    83  			"got", g.Error,
    84  		)
    85  	}
    86  
    87  	if g.GroupID != gid {
    88  		t.Error(
    89  			"Wrong groupID",
    90  			"expected", gid,
    91  			"got", g.GroupID,
    92  		)
    93  	}
    94  
    95  	if len(g.Members) != 1 {
    96  		t.Fatal(
    97  			"Wrong group members length",
    98  			"expected", 1,
    99  			"got", len(g.Members),
   100  		)
   101  	}
   102  	if len(g.Members[0].MemberAssignments.Topics) != 1 {
   103  		t.Fatal(
   104  			"Wrong topics length",
   105  			"expected", 1,
   106  			"got", len(g.Members[0].MemberAssignments.Topics),
   107  		)
   108  	}
   109  	mt := g.Members[0].MemberAssignments.Topics[0]
   110  	if mt.Topic != topic {
   111  		t.Error(
   112  			"Wrong member assignment topic",
   113  			"expected", topic,
   114  			"got", mt.Topic,
   115  		)
   116  	}
   117  
   118  	// Partitions can be in any order, sort them
   119  	sort.Slice(mt.Partitions, func(a, b int) bool {
   120  		return mt.Partitions[a] < mt.Partitions[b]
   121  	})
   122  
   123  	if !reflect.DeepEqual([]int{0, 1}, mt.Partitions) {
   124  		t.Error(
   125  			"Wrong member assignment partitions",
   126  			"expected", []int{0, 1},
   127  			"got", mt.Partitions,
   128  		)
   129  	}
   130  }