forked from IBM/sarama
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More 0.9 protocol additions. Add broker accessors
- Loading branch information
Showing
9 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package sarama | ||
|
||
type GroupMemberMetadata struct { | ||
Version int16 | ||
Topics []string | ||
UserData []byte | ||
} | ||
|
||
func (m *GroupMemberMetadata) encode(pe packetEncoder) error { | ||
pe.putInt16(m.Version) | ||
|
||
if err := pe.putStringArray(m.Topics); err != nil { | ||
return err | ||
} | ||
|
||
if err := pe.putBytes(m.UserData); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *GroupMemberMetadata) decode(pd packetDecoder) (err error) { | ||
if m.Version, err = pd.getInt16(); err != nil { | ||
return | ||
} | ||
|
||
if m.Topics, err = pd.getStringArray(); err != nil { | ||
return | ||
} | ||
|
||
if m.UserData, err = pd.getBytes(); err != nil { | ||
return | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type GroupMemberAssignment struct { | ||
Version int16 | ||
Topics []GroupMemberAssignedTopic | ||
UserData []byte | ||
} | ||
|
||
type GroupMemberAssignedTopic struct { | ||
Topic string | ||
Partitions []int32 | ||
} | ||
|
||
func (m *GroupMemberAssignment) encode(pe packetEncoder) error { | ||
pe.putInt16(m.Version) | ||
|
||
if err := pe.putArrayLength(len(m.Topics)); err != nil { | ||
return err | ||
} | ||
|
||
for _, topic := range m.Topics { | ||
if err := pe.putString(topic.Topic); err != nil { | ||
return err | ||
} | ||
if err := pe.putInt32Array(topic.Partitions); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if err := pe.putBytes(m.UserData); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *GroupMemberAssignment) decode(pd packetDecoder) (err error) { | ||
if m.Version, err = pd.getInt16(); err != nil { | ||
return | ||
} | ||
|
||
var topicLen int | ||
if topicLen, err = pd.getArrayLength(); err != nil { | ||
return | ||
} | ||
|
||
m.Topics = make([]GroupMemberAssignedTopic, topicLen) | ||
for i := 0; i < topicLen; i++ { | ||
if m.Topics[i].Topic, err = pd.getString(); err != nil { | ||
return | ||
} | ||
if m.Topics[i].Partitions, err = pd.getInt32Array(); err != nil { | ||
return | ||
} | ||
} | ||
|
||
if m.UserData, err = pd.getBytes(); err != nil { | ||
return | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package sarama | ||
|
||
import ( | ||
"bytes" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
var ( | ||
groupMemberMetadata = []byte{ | ||
0, 1, // Version | ||
0, 0, 0, 2, // Topic array length | ||
0, 3, 'o', 'n', 'e', // Topic one | ||
0, 3, 't', 'w', 'o', // Topic two | ||
0, 0, 0, 3, 0x01, 0x02, 0x03, // Userdata | ||
} | ||
groupMemberAssignment = []byte{ | ||
0, 1, // Version | ||
0, 0, 0, 2, // Topic array length | ||
0, 3, 'o', 'n', 'e', // Topic one | ||
0, 0, 0, 3, // Topic one, partition array length | ||
0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 4, // 0, 2, 4 | ||
0, 3, 't', 'w', 'o', // Topic two | ||
0, 0, 0, 2, // Topic two, partition array length | ||
0, 0, 0, 1, 0, 0, 0, 3, // 1, 3 | ||
0, 0, 0, 3, 0x01, 0x02, 0x03, // Userdata | ||
} | ||
) | ||
|
||
func TestGroupMemberMetadata(t *testing.T) { | ||
meta := &GroupMemberMetadata{ | ||
Version: 1, | ||
Topics: []string{"one", "two"}, | ||
UserData: []byte{0x01, 0x02, 0x03}, | ||
} | ||
|
||
buf, err := encode(meta) | ||
if err != nil { | ||
t.Error("Failed to encode data", err) | ||
} else if !bytes.Equal(groupMemberMetadata, buf) { | ||
t.Errorf("Encoded data does not match expectation\nexpected: %v\nactual: %v", groupMemberMetadata, buf) | ||
} | ||
|
||
meta2 := new(GroupMemberMetadata) | ||
err = decode(buf, meta2) | ||
if err != nil { | ||
t.Error("Failed to decode data", err) | ||
} else if !reflect.DeepEqual(meta, meta2) { | ||
t.Errorf("Encoded data does not match expectation\nexpected: %v\nactual: %v", meta, meta2) | ||
} | ||
} | ||
|
||
func TestGroupMemberAssignment(t *testing.T) { | ||
amt := &GroupMemberAssignment{ | ||
Version: 1, | ||
Topics: []GroupMemberAssignedTopic{ | ||
{Topic: "one", Partitions: []int32{0, 2, 4}}, | ||
{Topic: "two", Partitions: []int32{1, 3}}, | ||
}, | ||
UserData: []byte{0x01, 0x02, 0x03}, | ||
} | ||
|
||
buf, err := encode(amt) | ||
if err != nil { | ||
t.Error("Failed to encode data", err) | ||
} else if !bytes.Equal(groupMemberAssignment, buf) { | ||
t.Errorf("Encoded data does not match expectation\nexpected: %v\nactual: %v", groupMemberAssignment, buf) | ||
} | ||
|
||
amt2 := new(GroupMemberAssignment) | ||
err = decode(buf, amt2) | ||
if err != nil { | ||
t.Error("Failed to decode data", err) | ||
} else if !reflect.DeepEqual(amt, amt2) { | ||
t.Errorf("Encoded data does not match expectation\nexpected: %v\nactual: %v", amt, amt2) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters