File: join_group_request_test.go

package info (click to toggle)
golang-github-ibm-sarama 1.45.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,964 kB
  • sloc: makefile: 35; sh: 19
file content (143 lines) | stat: -rw-r--r-- 4,733 bytes parent folder | download
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
//go:build !functional

package sarama

import (
	"reflect"
	"testing"
)

var (
	joinGroupRequestV0_NoProtocols = []byte{
		0, 9, 'T', 'e', 's', 't', 'G', 'r', 'o', 'u', 'p', // Group ID
		0, 0, 0, 100, // Session timeout
		0, 0, // Member ID
		0, 8, 'c', 'o', 'n', 's', 'u', 'm', 'e', 'r', // Protocol Type
		0, 0, 0, 0, // 0 protocol groups
	}

	joinGroupRequestV0_OneProtocol = []byte{
		0, 9, 'T', 'e', 's', 't', 'G', 'r', 'o', 'u', 'p', // Group ID
		0, 0, 0, 100, // Session timeout
		0, 11, 'O', 'n', 'e', 'P', 'r', 'o', 't', 'o', 'c', 'o', 'l', // Member ID
		0, 8, 'c', 'o', 'n', 's', 'u', 'm', 'e', 'r', // Protocol Type
		0, 0, 0, 1, // 1 group protocol
		0, 3, 'o', 'n', 'e', // Protocol name
		0, 0, 0, 3, 0x01, 0x02, 0x03, // protocol metadata
	}

	joinGroupRequestV1 = []byte{
		0, 9, 'T', 'e', 's', 't', 'G', 'r', 'o', 'u', 'p', // Group ID
		0, 0, 0, 100, // Session timeout
		0, 0, 0, 200, // Rebalance timeout
		0, 11, 'O', 'n', 'e', 'P', 'r', 'o', 't', 'o', 'c', 'o', 'l', // Member ID
		0, 8, 'c', 'o', 'n', 's', 'u', 'm', 'e', 'r', // Protocol Type
		0, 0, 0, 1, // 1 group protocol
		0, 3, 'o', 'n', 'e', // Protocol name
		0, 0, 0, 3, 0x01, 0x02, 0x03, // protocol metadata
	}
)

func TestJoinGroupRequest(t *testing.T) {
	request := new(JoinGroupRequest)
	request.GroupId = "TestGroup"
	request.SessionTimeout = 100
	request.ProtocolType = "consumer"
	testRequest(t, "V0: no protocols", request, joinGroupRequestV0_NoProtocols)
}

func TestJoinGroupRequestV0_OneProtocol(t *testing.T) {
	request := new(JoinGroupRequest)
	request.GroupId = "TestGroup"
	request.SessionTimeout = 100
	request.MemberId = "OneProtocol"
	request.ProtocolType = "consumer"
	request.AddGroupProtocol("one", []byte{0x01, 0x02, 0x03})
	packet := testRequestEncode(t, "V0: one protocol", request, joinGroupRequestV0_OneProtocol)
	request.GroupProtocols = make(map[string][]byte)
	request.GroupProtocols["one"] = []byte{0x01, 0x02, 0x03}
	testRequestDecode(t, "V0: one protocol", request, packet)
}

func TestJoinGroupRequestDeprecatedEncode(t *testing.T) {
	request := new(JoinGroupRequest)
	request.GroupId = "TestGroup"
	request.SessionTimeout = 100
	request.MemberId = "OneProtocol"
	request.ProtocolType = "consumer"
	request.GroupProtocols = make(map[string][]byte)
	request.GroupProtocols["one"] = []byte{0x01, 0x02, 0x03}
	packet := testRequestEncode(t, "V0: one protocol", request, joinGroupRequestV0_OneProtocol)
	request.AddGroupProtocol("one", []byte{0x01, 0x02, 0x03})
	testRequestDecode(t, "V0: one protocol", request, packet)
}

func TestJoinGroupRequestV1(t *testing.T) {
	request := new(JoinGroupRequest)
	request.Version = 1
	request.GroupId = "TestGroup"
	request.SessionTimeout = 100
	request.RebalanceTimeout = 200
	request.MemberId = "OneProtocol"
	request.ProtocolType = "consumer"
	request.AddGroupProtocol("one", []byte{0x01, 0x02, 0x03})
	packet := testRequestEncode(t, "V1", request, joinGroupRequestV1)
	request.GroupProtocols = make(map[string][]byte)
	request.GroupProtocols["one"] = []byte{0x01, 0x02, 0x03}
	testRequestDecode(t, "V1", request, packet)
}

var (
	joinGroupRequestV5 = []byte{
		0, 9, 'T', 'e', 's', 't', 'G', 'r', 'o', 'u', 'p', // Group ID
		0, 0, 0, 100, // Session timeout
		0, 0, 0, 200, // Rebalance timeout
		0, 11, 'O', 'n', 'e', 'P', 'r', 'o', 't', 'o', 'c', 'o', 'l', // Member ID
		0, 3, 'g', 'i', 'd', // GroupInstanceId
		0, 8, 'c', 'o', 'n', 's', 'u', 'm', 'e', 'r', // Protocol Type
		0, 0, 0, 1, // 1 group protocol
		0, 3, 'o', 'n', 'e', // Protocol name
		0, 0, 0, 3, 0x01, 0x02, 0x03, // protocol metadata
	}
)

func TestJoinGroupRequestV3plus(t *testing.T) {
	groupInstanceId := "gid"
	tests := []struct {
		CaseName     string
		Version      int16
		MessageBytes []byte
		Message      *JoinGroupRequest
	}{
		{
			"v5",
			5,
			joinGroupRequestV5,
			&JoinGroupRequest{
				Version:          5,
				GroupId:          "TestGroup",
				SessionTimeout:   100,
				RebalanceTimeout: 200,
				MemberId:         "OneProtocol",
				GroupInstanceId:  &groupInstanceId,
				ProtocolType:     "consumer",
				GroupProtocols: map[string][]byte{
					"one": {1, 2, 3},
				},
				OrderedGroupProtocols: []*GroupProtocol{
					{Name: "one", Metadata: []byte{1, 2, 3}},
				},
			},
		},
	}
	for _, c := range tests {
		request := new(JoinGroupRequest)
		testVersionDecodable(t, c.CaseName, request, c.MessageBytes, c.Version)
		if !reflect.DeepEqual(c.Message, request) {
			t.Errorf("case %s decode failed, expected:%+v got %+v", c.CaseName, c.Message, request)
		}
		// This is to avoid error check "cannot specify both GroupProtocols and OrderedGroupProtocols on JoinGroupRequest"
		c.Message.GroupProtocols = nil
		testEncodable(t, c.CaseName, c.Message, c.MessageBytes)
	}
}