github.com/rbisecke/kafka-go@v0.4.27/listgroups_test.go (about) 1 package kafka 2 3 import ( 4 "bufio" 5 "bytes" 6 "context" 7 "fmt" 8 "reflect" 9 "testing" 10 "time" 11 ) 12 13 func TestListGroupsResponseV1(t *testing.T) { 14 item := listGroupsResponseV1{ 15 ErrorCode: 2, 16 Groups: []listGroupsResponseGroupV1{ 17 { 18 GroupID: "a", 19 ProtocolType: "b", 20 }, 21 }, 22 } 23 24 b := bytes.NewBuffer(nil) 25 w := &writeBuffer{w: b} 26 item.writeTo(w) 27 28 var found listGroupsResponseV1 29 remain, err := (&found).readFrom(bufio.NewReader(b), b.Len()) 30 if err != nil { 31 t.Error(err) 32 t.FailNow() 33 } 34 if remain != 0 { 35 t.Errorf("expected 0 remain, got %v", remain) 36 t.FailNow() 37 } 38 if !reflect.DeepEqual(item, found) { 39 t.Error("expected item and found to be the same") 40 t.FailNow() 41 } 42 } 43 44 func TestClientListGroups(t *testing.T) { 45 client, shutdown := newLocalClient() 46 defer shutdown() 47 48 topic := makeTopic() 49 gid := fmt.Sprintf("%s-test-group", topic) 50 51 createTopic(t, topic, 1) 52 defer deleteTopic(t, topic) 53 54 w := newTestWriter(WriterConfig{ 55 Topic: topic, 56 }) 57 58 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 59 defer cancel() 60 61 err := w.WriteMessages( 62 ctx, 63 Message{ 64 Key: []byte("key"), 65 Value: []byte("value"), 66 }, 67 ) 68 if err != nil { 69 t.Fatal(err) 70 } 71 72 r := NewReader(ReaderConfig{ 73 Brokers: []string{"localhost:9092"}, 74 Topic: topic, 75 GroupID: gid, 76 MinBytes: 10, 77 MaxBytes: 1000, 78 }) 79 _, err = r.ReadMessage(ctx) 80 if err != nil { 81 t.Fatal(err) 82 } 83 84 resp, err := client.ListGroups( 85 ctx, 86 &ListGroupsRequest{}, 87 ) 88 if err != nil { 89 t.Fatal(err) 90 } 91 if resp.Error != nil { 92 t.Error( 93 "Unexpected error in response", 94 "expected", nil, 95 "got", resp.Error, 96 ) 97 } 98 hasGroup := false 99 for _, group := range resp.Groups { 100 if group.GroupID == gid { 101 hasGroup = true 102 break 103 } 104 } 105 106 if !hasGroup { 107 t.Error("Group not found in list") 108 } 109 }