join_group_response_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package sarama
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. var (
  7. joinGroupResponseV0_NoError = []byte{
  8. 0x00, 0x00, // No error
  9. 0x00, 0x01, 0x02, 0x03, // Generation ID
  10. 0, 8, 'p', 'r', 'o', 't', 'o', 'c', 'o', 'l', // Protocol name chosen
  11. 0, 3, 'f', 'o', 'o', // Leader ID
  12. 0, 3, 'b', 'a', 'r', // Member ID
  13. 0, 0, 0, 0, // No member info
  14. }
  15. joinGroupResponseV0_WithError = []byte{
  16. 0, 23, // Error: inconsistent group protocol
  17. 0x00, 0x00, 0x00, 0x00, // Generation ID
  18. 0, 0, // Protocol name chosen
  19. 0, 0, // Leader ID
  20. 0, 0, // Member ID
  21. 0, 0, 0, 0, // No member info
  22. }
  23. joinGroupResponseV0_Leader = []byte{
  24. 0x00, 0x00, // No error
  25. 0x00, 0x01, 0x02, 0x03, // Generation ID
  26. 0, 8, 'p', 'r', 'o', 't', 'o', 'c', 'o', 'l', // Protocol name chosen
  27. 0, 3, 'f', 'o', 'o', // Leader ID
  28. 0, 3, 'f', 'o', 'o', // Member ID == Leader ID
  29. 0, 0, 0, 1, // 1 member
  30. 0, 3, 'f', 'o', 'o', // Member ID
  31. 0, 0, 0, 3, 0x01, 0x02, 0x03, // Member metadata
  32. }
  33. joinGroupResponseV1 = []byte{
  34. 0x00, 0x00, // No error
  35. 0x00, 0x01, 0x02, 0x03, // Generation ID
  36. 0, 8, 'p', 'r', 'o', 't', 'o', 'c', 'o', 'l', // Protocol name chosen
  37. 0, 3, 'f', 'o', 'o', // Leader ID
  38. 0, 3, 'b', 'a', 'r', // Member ID
  39. 0, 0, 0, 0, // No member info
  40. }
  41. joinGroupResponseV2 = []byte{
  42. 0, 0, 0, 100,
  43. 0x00, 0x00, // No error
  44. 0x00, 0x01, 0x02, 0x03, // Generation ID
  45. 0, 8, 'p', 'r', 'o', 't', 'o', 'c', 'o', 'l', // Protocol name chosen
  46. 0, 3, 'f', 'o', 'o', // Leader ID
  47. 0, 3, 'b', 'a', 'r', // Member ID
  48. 0, 0, 0, 0, // No member info
  49. }
  50. )
  51. func TestJoinGroupResponseV0(t *testing.T) {
  52. var response *JoinGroupResponse
  53. response = new(JoinGroupResponse)
  54. testVersionDecodable(t, "no error", response, joinGroupResponseV0_NoError, 0)
  55. if response.Err != ErrNoError {
  56. t.Error("Decoding Err failed: no error expected but found", response.Err)
  57. }
  58. if response.GenerationId != 66051 {
  59. t.Error("Decoding GenerationId failed, found:", response.GenerationId)
  60. }
  61. if response.LeaderId != "foo" {
  62. t.Error("Decoding LeaderId failed, found:", response.LeaderId)
  63. }
  64. if response.MemberId != "bar" {
  65. t.Error("Decoding MemberId failed, found:", response.MemberId)
  66. }
  67. if len(response.Members) != 0 {
  68. t.Error("Decoding Members failed, found:", response.Members)
  69. }
  70. response = new(JoinGroupResponse)
  71. testVersionDecodable(t, "with error", response, joinGroupResponseV0_WithError, 0)
  72. if response.Err != ErrInconsistentGroupProtocol {
  73. t.Error("Decoding Err failed: ErrInconsistentGroupProtocol expected but found", response.Err)
  74. }
  75. if response.GenerationId != 0 {
  76. t.Error("Decoding GenerationId failed, found:", response.GenerationId)
  77. }
  78. if response.LeaderId != "" {
  79. t.Error("Decoding LeaderId failed, found:", response.LeaderId)
  80. }
  81. if response.MemberId != "" {
  82. t.Error("Decoding MemberId failed, found:", response.MemberId)
  83. }
  84. if len(response.Members) != 0 {
  85. t.Error("Decoding Members failed, found:", response.Members)
  86. }
  87. response = new(JoinGroupResponse)
  88. testVersionDecodable(t, "with error", response, joinGroupResponseV0_Leader, 0)
  89. if response.Err != ErrNoError {
  90. t.Error("Decoding Err failed: ErrNoError expected but found", response.Err)
  91. }
  92. if response.GenerationId != 66051 {
  93. t.Error("Decoding GenerationId failed, found:", response.GenerationId)
  94. }
  95. if response.LeaderId != "foo" {
  96. t.Error("Decoding LeaderId failed, found:", response.LeaderId)
  97. }
  98. if response.MemberId != "foo" {
  99. t.Error("Decoding MemberId failed, found:", response.MemberId)
  100. }
  101. if len(response.Members) != 1 {
  102. t.Error("Decoding Members failed, found:", response.Members)
  103. }
  104. if !reflect.DeepEqual(response.Members["foo"], []byte{0x01, 0x02, 0x03}) {
  105. t.Error("Decoding foo member failed, found:", response.Members["foo"])
  106. }
  107. }
  108. func TestJoinGroupResponseV1(t *testing.T) {
  109. response := new(JoinGroupResponse)
  110. testVersionDecodable(t, "no error", response, joinGroupResponseV1, 1)
  111. if response.Err != ErrNoError {
  112. t.Error("Decoding Err failed: no error expected but found", response.Err)
  113. }
  114. if response.GenerationId != 66051 {
  115. t.Error("Decoding GenerationId failed, found:", response.GenerationId)
  116. }
  117. if response.GroupProtocol != "protocol" {
  118. t.Error("Decoding GroupProtocol failed, found:", response.GroupProtocol)
  119. }
  120. if response.LeaderId != "foo" {
  121. t.Error("Decoding LeaderId failed, found:", response.LeaderId)
  122. }
  123. if response.MemberId != "bar" {
  124. t.Error("Decoding MemberId failed, found:", response.MemberId)
  125. }
  126. if response.Version != 1 {
  127. t.Error("Decoding Version failed, found:", response.Version)
  128. }
  129. if len(response.Members) != 0 {
  130. t.Error("Decoding Members failed, found:", response.Members)
  131. }
  132. }
  133. func TestJoinGroupResponseV2(t *testing.T) {
  134. response := new(JoinGroupResponse)
  135. testVersionDecodable(t, "no error", response, joinGroupResponseV2, 2)
  136. if response.ThrottleTime != 100 {
  137. t.Error("Decoding ThrottleTime failed, found:", response.ThrottleTime)
  138. }
  139. if response.Err != ErrNoError {
  140. t.Error("Decoding Err failed: no error expected but found", response.Err)
  141. }
  142. if response.GenerationId != 66051 {
  143. t.Error("Decoding GenerationId failed, found:", response.GenerationId)
  144. }
  145. if response.GroupProtocol != "protocol" {
  146. t.Error("Decoding GroupProtocol failed, found:", response.GroupProtocol)
  147. }
  148. if response.LeaderId != "foo" {
  149. t.Error("Decoding LeaderId failed, found:", response.LeaderId)
  150. }
  151. if response.MemberId != "bar" {
  152. t.Error("Decoding MemberId failed, found:", response.MemberId)
  153. }
  154. if response.Version != 2 {
  155. t.Error("Decoding Version failed, found:", response.Version)
  156. }
  157. if len(response.Members) != 0 {
  158. t.Error("Decoding Members failed, found:", response.Members)
  159. }
  160. }