client_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package sarama
  2. import (
  3. "io"
  4. "testing"
  5. )
  6. func safeClose(t *testing.T, c io.Closer) {
  7. err := c.Close()
  8. if err != nil {
  9. t.Error(err)
  10. }
  11. }
  12. func TestSimpleClient(t *testing.T) {
  13. seedBroker := newMockBroker(t, 1)
  14. seedBroker.Returns(new(MetadataResponse))
  15. client, err := NewClient([]string{seedBroker.Addr()}, nil)
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. seedBroker.Close()
  20. safeClose(t, client)
  21. }
  22. func TestCachedPartitions(t *testing.T) {
  23. seedBroker := newMockBroker(t, 1)
  24. replicas := []int32{3, 1, 5}
  25. isr := []int32{5, 1}
  26. metadataResponse := new(MetadataResponse)
  27. metadataResponse.AddBroker("localhost:12345", 2)
  28. metadataResponse.AddTopicPartition("my_topic", 0, 2, replicas, isr, ErrNoError)
  29. metadataResponse.AddTopicPartition("my_topic", 1, 2, replicas, isr, ErrLeaderNotAvailable)
  30. seedBroker.Returns(metadataResponse)
  31. config := NewConfig()
  32. config.Metadata.Retry.Max = 0
  33. c, err := NewClient([]string{seedBroker.Addr()}, config)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. client := c.(*client)
  38. // Verify they aren't cached the same
  39. allP := client.cachedPartitionsResults["my_topic"][allPartitions]
  40. writeP := client.cachedPartitionsResults["my_topic"][writablePartitions]
  41. if len(allP) == len(writeP) {
  42. t.Fatal("Invalid lengths!")
  43. }
  44. tmp := client.cachedPartitionsResults["my_topic"]
  45. // Verify we actually use the cache at all!
  46. tmp[allPartitions] = []int32{1, 2, 3, 4}
  47. client.cachedPartitionsResults["my_topic"] = tmp
  48. if 4 != len(client.cachedPartitions("my_topic", allPartitions)) {
  49. t.Fatal("Not using the cache!")
  50. }
  51. seedBroker.Close()
  52. safeClose(t, client)
  53. }
  54. func TestClientDoesntCachePartitionsForTopicsWithErrors(t *testing.T) {
  55. seedBroker := newMockBroker(t, 1)
  56. replicas := []int32{seedBroker.BrokerID()}
  57. metadataResponse := new(MetadataResponse)
  58. metadataResponse.AddBroker(seedBroker.Addr(), seedBroker.BrokerID())
  59. metadataResponse.AddTopicPartition("my_topic", 1, replicas[0], replicas, replicas, ErrNoError)
  60. metadataResponse.AddTopicPartition("my_topic", 2, replicas[0], replicas, replicas, ErrNoError)
  61. seedBroker.Returns(metadataResponse)
  62. config := NewConfig()
  63. config.Metadata.Retry.Max = 0
  64. client, err := NewClient([]string{seedBroker.Addr()}, config)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. metadataResponse = new(MetadataResponse)
  69. metadataResponse.AddTopic("unknown", ErrUnknownTopicOrPartition)
  70. seedBroker.Returns(metadataResponse)
  71. partitions, err := client.Partitions("unknown")
  72. if err != ErrUnknownTopicOrPartition {
  73. t.Error("Expected ErrUnknownTopicOrPartition, found", err)
  74. }
  75. if partitions != nil {
  76. t.Errorf("Should return nil as partition list, found %v", partitions)
  77. }
  78. // Should still use the cache of a known topic
  79. partitions, err = client.Partitions("my_topic")
  80. if err != nil {
  81. t.Errorf("Expected no error, found %v", err)
  82. }
  83. metadataResponse = new(MetadataResponse)
  84. metadataResponse.AddTopic("unknown", ErrUnknownTopicOrPartition)
  85. seedBroker.Returns(metadataResponse)
  86. // Should not use cache for unknown topic
  87. partitions, err = client.Partitions("unknown")
  88. if err != ErrUnknownTopicOrPartition {
  89. t.Error("Expected ErrUnknownTopicOrPartition, found", err)
  90. }
  91. if partitions != nil {
  92. t.Errorf("Should return nil as partition list, found %v", partitions)
  93. }
  94. seedBroker.Close()
  95. safeClose(t, client)
  96. }
  97. func TestClientSeedBrokers(t *testing.T) {
  98. seedBroker := newMockBroker(t, 1)
  99. metadataResponse := new(MetadataResponse)
  100. metadataResponse.AddBroker("localhost:12345", 2)
  101. seedBroker.Returns(metadataResponse)
  102. client, err := NewClient([]string{seedBroker.Addr()}, nil)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. seedBroker.Close()
  107. safeClose(t, client)
  108. }
  109. func TestClientMetadata(t *testing.T) {
  110. seedBroker := newMockBroker(t, 1)
  111. leader := newMockBroker(t, 5)
  112. replicas := []int32{3, 1, 5}
  113. isr := []int32{5, 1}
  114. metadataResponse := new(MetadataResponse)
  115. metadataResponse.AddBroker(leader.Addr(), leader.BrokerID())
  116. metadataResponse.AddTopicPartition("my_topic", 0, leader.BrokerID(), replicas, isr, ErrNoError)
  117. metadataResponse.AddTopicPartition("my_topic", 1, leader.BrokerID(), replicas, isr, ErrLeaderNotAvailable)
  118. seedBroker.Returns(metadataResponse)
  119. config := NewConfig()
  120. config.Metadata.Retry.Max = 0
  121. client, err := NewClient([]string{seedBroker.Addr()}, config)
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. topics, err := client.Topics()
  126. if err != nil {
  127. t.Error(err)
  128. } else if len(topics) != 1 || topics[0] != "my_topic" {
  129. t.Error("Client returned incorrect topics:", topics)
  130. }
  131. parts, err := client.Partitions("my_topic")
  132. if err != nil {
  133. t.Error(err)
  134. } else if len(parts) != 2 || parts[0] != 0 || parts[1] != 1 {
  135. t.Error("Client returned incorrect partitions for my_topic:", parts)
  136. }
  137. parts, err = client.WritablePartitions("my_topic")
  138. if err != nil {
  139. t.Error(err)
  140. } else if len(parts) != 1 || parts[0] != 0 {
  141. t.Error("Client returned incorrect writable partitions for my_topic:", parts)
  142. }
  143. tst, err := client.Leader("my_topic", 0)
  144. if err != nil {
  145. t.Error(err)
  146. } else if tst.ID() != 5 {
  147. t.Error("Leader for my_topic had incorrect ID.")
  148. }
  149. replicas, err = client.Replicas("my_topic", 0)
  150. if err != nil {
  151. t.Error(err)
  152. } else if replicas[0] != 1 {
  153. t.Error("Incorrect (or unsorted) replica")
  154. } else if replicas[1] != 3 {
  155. t.Error("Incorrect (or unsorted) replica")
  156. } else if replicas[2] != 5 {
  157. t.Error("Incorrect (or unsorted) replica")
  158. }
  159. leader.Close()
  160. seedBroker.Close()
  161. safeClose(t, client)
  162. }
  163. func TestClientRefreshBehaviour(t *testing.T) {
  164. seedBroker := newMockBroker(t, 1)
  165. leader := newMockBroker(t, 5)
  166. metadataResponse1 := new(MetadataResponse)
  167. metadataResponse1.AddBroker(leader.Addr(), leader.BrokerID())
  168. seedBroker.Returns(metadataResponse1)
  169. metadataResponse2 := new(MetadataResponse)
  170. metadataResponse2.AddTopicPartition("my_topic", 0xb, leader.BrokerID(), nil, nil, ErrNoError)
  171. seedBroker.Returns(metadataResponse2)
  172. c, err := NewClient([]string{seedBroker.Addr()}, nil)
  173. if err != nil {
  174. t.Fatal(err)
  175. }
  176. client := c.(*client)
  177. parts, err := client.Partitions("my_topic")
  178. if err != nil {
  179. t.Error(err)
  180. } else if len(parts) != 1 || parts[0] != 0xb {
  181. t.Error("Client returned incorrect partitions for my_topic:", parts)
  182. }
  183. tst, err := client.Leader("my_topic", 0xb)
  184. if err != nil {
  185. t.Error(err)
  186. } else if tst.ID() != 5 {
  187. t.Error("Leader for my_topic had incorrect ID.")
  188. }
  189. leader.Close()
  190. seedBroker.Close()
  191. safeClose(t, client)
  192. }