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 217 218 219 220 221 222 223 224 225 226 227 228 229
|
//go:build !functional
package sarama
import (
"errors"
"reflect"
"testing"
)
var (
joinGroupResponseV0_NoError = []byte{
0x00, 0x00, // No error
0x00, 0x01, 0x02, 0x03, // Generation ID
0, 8, 'p', 'r', 'o', 't', 'o', 'c', 'o', 'l', // Protocol name chosen
0, 3, 'f', 'o', 'o', // Leader ID
0, 3, 'b', 'a', 'r', // Member ID
0, 0, 0, 0, // No member info
}
joinGroupResponseV0_WithError = []byte{
0, 23, // Error: inconsistent group protocol
0x00, 0x00, 0x00, 0x00, // Generation ID
0, 0, // Protocol name chosen
0, 0, // Leader ID
0, 0, // Member ID
0, 0, 0, 0, // No member info
}
joinGroupResponseV0_Leader = []byte{
0x00, 0x00, // No error
0x00, 0x01, 0x02, 0x03, // Generation ID
0, 8, 'p', 'r', 'o', 't', 'o', 'c', 'o', 'l', // Protocol name chosen
0, 3, 'f', 'o', 'o', // Leader ID
0, 3, 'f', 'o', 'o', // Member ID == Leader ID
0, 0, 0, 1, // 1 member
0, 3, 'f', 'o', 'o', // Member ID
0, 0, 0, 3, 0x01, 0x02, 0x03, // Member metadata
}
joinGroupResponseV1 = []byte{
0x00, 0x00, // No error
0x00, 0x01, 0x02, 0x03, // Generation ID
0, 8, 'p', 'r', 'o', 't', 'o', 'c', 'o', 'l', // Protocol name chosen
0, 3, 'f', 'o', 'o', // Leader ID
0, 3, 'b', 'a', 'r', // Member ID
0, 0, 0, 0, // No member info
}
joinGroupResponseV2 = []byte{
0, 0, 0, 100,
0x00, 0x00, // No error
0x00, 0x01, 0x02, 0x03, // Generation ID
0, 8, 'p', 'r', 'o', 't', 'o', 'c', 'o', 'l', // Protocol name chosen
0, 3, 'f', 'o', 'o', // Leader ID
0, 3, 'b', 'a', 'r', // Member ID
0, 0, 0, 0, // No member info
}
)
func TestJoinGroupResponseV0(t *testing.T) {
var response *JoinGroupResponse
response = new(JoinGroupResponse)
testVersionDecodable(t, "no error", response, joinGroupResponseV0_NoError, 0)
if !errors.Is(response.Err, ErrNoError) {
t.Error("Decoding Err failed: no error expected but found", response.Err)
}
if response.GenerationId != 66051 {
t.Error("Decoding GenerationId failed, found:", response.GenerationId)
}
if response.LeaderId != "foo" {
t.Error("Decoding LeaderId failed, found:", response.LeaderId)
}
if response.MemberId != "bar" {
t.Error("Decoding MemberId failed, found:", response.MemberId)
}
if len(response.Members) != 0 {
t.Error("Decoding Members failed, found:", response.Members)
}
response = new(JoinGroupResponse)
testVersionDecodable(t, "with error", response, joinGroupResponseV0_WithError, 0)
if !errors.Is(response.Err, ErrInconsistentGroupProtocol) {
t.Error("Decoding Err failed: ErrInconsistentGroupProtocol expected but found", response.Err)
}
if response.GenerationId != 0 {
t.Error("Decoding GenerationId failed, found:", response.GenerationId)
}
if response.LeaderId != "" {
t.Error("Decoding LeaderId failed, found:", response.LeaderId)
}
if response.MemberId != "" {
t.Error("Decoding MemberId failed, found:", response.MemberId)
}
if len(response.Members) != 0 {
t.Error("Decoding Members failed, found:", response.Members)
}
response = new(JoinGroupResponse)
testVersionDecodable(t, "with error", response, joinGroupResponseV0_Leader, 0)
if !errors.Is(response.Err, ErrNoError) {
t.Error("Decoding Err failed: ErrNoError expected but found", response.Err)
}
if response.GenerationId != 66051 {
t.Error("Decoding GenerationId failed, found:", response.GenerationId)
}
if response.LeaderId != "foo" {
t.Error("Decoding LeaderId failed, found:", response.LeaderId)
}
if response.MemberId != "foo" {
t.Error("Decoding MemberId failed, found:", response.MemberId)
}
if len(response.Members) != 1 {
t.Error("Decoding Members failed, found:", response.Members)
}
if response.Members[0].MemberId != "foo" {
t.Error("Decoding MemberId failed, found:", response.Members[0].MemberId)
}
if !reflect.DeepEqual(response.Members[0].Metadata, []byte{0x01, 0x02, 0x03}) {
t.Error("Decoding foo member failed, found:", response.Members[0].Metadata)
}
}
func TestJoinGroupResponseV1(t *testing.T) {
response := new(JoinGroupResponse)
testVersionDecodable(t, "no error", response, joinGroupResponseV1, 1)
if !errors.Is(response.Err, ErrNoError) {
t.Error("Decoding Err failed: no error expected but found", response.Err)
}
if response.GenerationId != 66051 {
t.Error("Decoding GenerationId failed, found:", response.GenerationId)
}
if response.GroupProtocol != "protocol" {
t.Error("Decoding GroupProtocol failed, found:", response.GroupProtocol)
}
if response.LeaderId != "foo" {
t.Error("Decoding LeaderId failed, found:", response.LeaderId)
}
if response.MemberId != "bar" {
t.Error("Decoding MemberId failed, found:", response.MemberId)
}
if response.Version != 1 {
t.Error("Decoding Version failed, found:", response.Version)
}
if len(response.Members) != 0 {
t.Error("Decoding Members failed, found:", response.Members)
}
}
func TestJoinGroupResponseV2(t *testing.T) {
response := new(JoinGroupResponse)
testVersionDecodable(t, "no error", response, joinGroupResponseV2, 2)
if response.ThrottleTime != 100 {
t.Error("Decoding ThrottleTime failed, found:", response.ThrottleTime)
}
if !errors.Is(response.Err, ErrNoError) {
t.Error("Decoding Err failed: no error expected but found", response.Err)
}
if response.GenerationId != 66051 {
t.Error("Decoding GenerationId failed, found:", response.GenerationId)
}
if response.GroupProtocol != "protocol" {
t.Error("Decoding GroupProtocol failed, found:", response.GroupProtocol)
}
if response.LeaderId != "foo" {
t.Error("Decoding LeaderId failed, found:", response.LeaderId)
}
if response.MemberId != "bar" {
t.Error("Decoding MemberId failed, found:", response.MemberId)
}
if response.Version != 2 {
t.Error("Decoding Version failed, found:", response.Version)
}
if len(response.Members) != 0 {
t.Error("Decoding Members failed, found:", response.Members)
}
}
var (
joinGroupResponseV5 = []byte{
0, 0, 0, 100, // ThrottleTimeMs
0x00, 0x00, // No error
0x00, 0x01, 0x02, 0x03, // Generation ID
0, 8, 'p', 'r', 'o', 't', 'o', 'c', 'o', 'l', // Protocol name chosen
0, 3, 'f', 'o', 'o', // Leader ID
0, 3, 'b', 'a', 'r', // Member ID
0, 0, 0, 1, // One member info
0, 3, 'm', 'i', 'd', // memberId
0, 3, 'g', 'i', 'd', // GroupInstanceId
0, 0, 0, 3, 1, 2, 3, // Metadata
}
)
func TestJoinGroupResponse3plus(t *testing.T) {
groupInstanceId := "gid"
tests := []struct {
CaseName string
Version int16
MessageBytes []byte
Message *JoinGroupResponse
}{
{
"v5",
5,
joinGroupResponseV5,
&JoinGroupResponse{
Version: 5,
ThrottleTime: 100,
Err: ErrNoError,
GenerationId: 0x00010203,
GroupProtocol: "protocol",
LeaderId: "foo",
MemberId: "bar",
Members: []GroupMember{
{"mid", &groupInstanceId, []byte{1, 2, 3}},
},
},
},
}
for _, c := range tests {
response := new(JoinGroupResponse)
testVersionDecodable(t, c.CaseName, response, c.MessageBytes, c.Version)
if !reflect.DeepEqual(c.Message, response) {
t.Errorf("case %s decode failed, expected:%+v got %+v", c.CaseName, c.Message, response)
}
testEncodable(t, c.CaseName, c.Message, c.MessageBytes)
}
}
|