forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leavegroup_test.go
216 lines (184 loc) · 4.81 KB
/
leavegroup_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package kafka
import (
"bufio"
"bytes"
"context"
"errors"
"reflect"
"testing"
"time"
)
func TestClientLeaveGroup(t *testing.T) {
// In order to get to a leave group call we need to first
// join a group then sync the group.
topic := makeTopic()
client, shutdown := newLocalClient()
client.Timeout = time.Minute
// Although at higher api versions ClientID is nullable
// for some reason the SyncGroup API call errors
// when ClientID is null.
// The Java Kafka Consumer generates a ClientID if one is not
// present or if the provided ClientID is empty.
client.Transport.(*Transport).ClientID = "test-client"
defer shutdown()
err := clientCreateTopic(client, topic, 3)
if err != nil {
t.Fatal(err)
}
groupID := makeGroupID()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
respc, err := waitForCoordinatorIndefinitely(ctx, client, &FindCoordinatorRequest{
Addr: client.Addr,
Key: groupID,
KeyType: CoordinatorKeyTypeConsumer,
})
if err != nil {
t.Fatal(err)
}
if respc.Error != nil {
t.Fatal(err)
}
groupInstanceID := "group-instance-id"
userData := "user-data"
var rrGroupBalancer RoundRobinGroupBalancer
req := &JoinGroupRequest{
GroupID: groupID,
GroupInstanceID: groupInstanceID,
ProtocolType: "consumer",
SessionTimeout: time.Minute,
RebalanceTimeout: time.Minute,
Protocols: []GroupProtocol{
{
Name: rrGroupBalancer.ProtocolName(),
Metadata: GroupProtocolSubscription{
Topics: []string{topic},
UserData: []byte(userData),
OwnedPartitions: map[string][]int{
topic: {0, 1, 2},
},
},
},
},
}
var resp *JoinGroupResponse
for {
resp, err = client.JoinGroup(ctx, req)
if err != nil {
t.Fatal(err)
}
if errors.Is(resp.Error, MemberIDRequired) {
req.MemberID = resp.MemberID
time.Sleep(time.Second)
continue
}
if resp.Error != nil {
t.Fatal(resp.Error)
}
break
}
if resp.MemberID != resp.LeaderID {
t.Fatal("expected to be group leader")
}
groupMembers := make([]GroupMember, 0, len(resp.Members))
groupUserDataLookup := make(map[string]GroupMember)
for _, member := range resp.Members {
gm := GroupMember{
ID: member.ID,
Topics: member.Metadata.Topics,
UserData: member.Metadata.UserData,
}
groupMembers = append(groupMembers, gm)
groupUserDataLookup[member.ID] = gm
}
metaResp, err := client.Metadata(ctx, &MetadataRequest{
Topics: []string{topic},
})
if err != nil {
t.Fatal(err)
}
assignments := rrGroupBalancer.AssignGroups(groupMembers, metaResp.Topics[0].Partitions)
sgRequest := &SyncGroupRequest{
GroupID: groupID,
GenerationID: resp.GenerationID,
MemberID: resp.MemberID,
GroupInstanceID: groupInstanceID,
ProtocolType: "consumer",
ProtocolName: rrGroupBalancer.ProtocolName(),
}
for member, assignment := range assignments {
sgRequest.Assignments = append(sgRequest.Assignments, SyncGroupRequestAssignment{
MemberID: member,
Assignment: GroupProtocolAssignment{
AssignedPartitions: assignment,
UserData: groupUserDataLookup[member].UserData,
},
})
}
sgResp, err := client.SyncGroup(ctx, sgRequest)
if err != nil {
t.Fatal(err)
}
if sgResp.Error != nil {
t.Fatal(sgResp.Error)
}
expectedAssignment := GroupProtocolAssignment{
AssignedPartitions: map[string][]int{
topic: {0, 1, 2},
},
UserData: []byte(userData),
}
if !reflect.DeepEqual(sgResp.Assignment, expectedAssignment) {
t.Fatalf("\nexpected assignment to be \n%#v \ngot\n%#v", expectedAssignment, sgResp.Assignment)
}
lgResp, err := client.LeaveGroup(ctx, &LeaveGroupRequest{
GroupID: groupID,
Members: []LeaveGroupRequestMember{
{
ID: resp.MemberID,
GroupInstanceID: groupInstanceID,
},
},
})
if err != nil {
t.Fatal(err)
}
if lgResp.Error != nil {
t.Fatal(err)
}
if len(lgResp.Members) != 1 {
t.Fatalf("expected 1 member in response, got %#v", lgResp.Members)
}
member := lgResp.Members[0]
if member.Error != nil {
t.Fatalf("unexpected member error %v", member.Error)
}
if member.GroupInstanceID != groupInstanceID {
t.Fatalf("expected group instance id to be %s got %s", groupInstanceID, member.GroupInstanceID)
}
if member.ID != resp.MemberID {
t.Fatalf("expected member id to be %s got %s", resp.MemberID, member.ID)
}
}
func TestLeaveGroupResponseV0(t *testing.T) {
item := leaveGroupResponseV0{
ErrorCode: 2,
}
b := bytes.NewBuffer(nil)
w := &writeBuffer{w: b}
item.writeTo(w)
var found leaveGroupResponseV0
remain, err := (&found).readFrom(bufio.NewReader(b), b.Len())
if err != nil {
t.Error(err)
t.FailNow()
}
if remain != 0 {
t.Errorf("expected 0 remain, got %v", remain)
t.FailNow()
}
if !reflect.DeepEqual(item, found) {
t.Error("expected item and found to be the same")
t.FailNow()
}
}