client_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 TestDefaultClientConfigValidates(t *testing.T) {
  13. config := NewClientConfig()
  14. if err := config.Validate(); err != nil {
  15. t.Error(err)
  16. }
  17. }
  18. func TestSimpleClient(t *testing.T) {
  19. mb := NewMockBroker(t, 1)
  20. mb.Returns(new(MetadataResponse))
  21. client, err := NewClient("client_id", []string{mb.Addr()}, nil)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. defer safeClose(t, client)
  26. defer mb.Close()
  27. }
  28. func TestCachedPartitions(t *testing.T) {
  29. mb1 := NewMockBroker(t, 1)
  30. mb5 := NewMockBroker(t, 5)
  31. replicas := []int32{3, 1, 5}
  32. isr := []int32{5, 1}
  33. mdr := new(MetadataResponse)
  34. mdr.AddBroker(mb5.Addr(), mb5.BrokerID())
  35. mdr.AddTopicPartition("my_topic", 0, mb5.BrokerID(), replicas, isr, NoError)
  36. mdr.AddTopicPartition("my_topic", 1, mb5.BrokerID(), replicas, isr, LeaderNotAvailable)
  37. mb1.Returns(mdr)
  38. config := NewClientConfig()
  39. config.MetadataRetries = 0
  40. client, err := NewClient("client_id", []string{mb1.Addr()}, config)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. defer safeClose(t, client)
  45. defer mb1.Close()
  46. defer mb5.Close()
  47. // Verify they aren't cached the same
  48. allP := client.cachedPartitionsResults["my_topic"][allPartitions]
  49. writeP := client.cachedPartitionsResults["my_topic"][writablePartitions]
  50. if len(allP) == len(writeP) {
  51. t.Fatal("Invalid lengths!")
  52. }
  53. tmp := client.cachedPartitionsResults["my_topic"]
  54. // Verify we actually use the cache at all!
  55. tmp[allPartitions] = []int32{1, 2, 3, 4}
  56. client.cachedPartitionsResults["my_topic"] = tmp
  57. if 4 != len(client.cachedPartitions("my_topic", allPartitions)) {
  58. t.Fatal("Not using the cache!")
  59. }
  60. }
  61. func TestClientSeedBrokers(t *testing.T) {
  62. mb1 := NewMockBroker(t, 1)
  63. mb2 := NewMockBroker(t, 2)
  64. mdr := new(MetadataResponse)
  65. mdr.AddBroker(mb2.Addr(), mb2.BrokerID())
  66. mb1.Returns(mdr)
  67. client, err := NewClient("client_id", []string{mb1.Addr()}, nil)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. defer safeClose(t, client)
  72. defer mb1.Close()
  73. defer mb2.Close()
  74. }
  75. func TestClientMetadata(t *testing.T) {
  76. mb1 := NewMockBroker(t, 1)
  77. mb5 := NewMockBroker(t, 5)
  78. replicas := []int32{3, 1, 5}
  79. isr := []int32{5, 1}
  80. mdr := new(MetadataResponse)
  81. mdr.AddBroker(mb5.Addr(), mb5.BrokerID())
  82. mdr.AddTopicPartition("my_topic", 0, mb5.BrokerID(), replicas, isr, NoError)
  83. mdr.AddTopicPartition("my_topic", 1, mb5.BrokerID(), replicas, isr, LeaderNotAvailable)
  84. mb1.Returns(mdr)
  85. config := NewClientConfig()
  86. config.MetadataRetries = 0
  87. client, err := NewClient("client_id", []string{mb1.Addr()}, config)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. defer safeClose(t, client)
  92. defer mb1.Close()
  93. defer mb5.Close()
  94. topics, err := client.Topics()
  95. if err != nil {
  96. t.Error(err)
  97. } else if len(topics) != 1 || topics[0] != "my_topic" {
  98. t.Error("Client returned incorrect topics:", topics)
  99. }
  100. parts, err := client.Partitions("my_topic")
  101. if err != nil {
  102. t.Error(err)
  103. } else if len(parts) != 2 || parts[0] != 0 || parts[1] != 1 {
  104. t.Error("Client returned incorrect partitions for my_topic:", parts)
  105. }
  106. parts, err = client.WritablePartitions("my_topic")
  107. if err != nil {
  108. t.Error(err)
  109. } else if len(parts) != 1 || parts[0] != 0 {
  110. t.Error("Client returned incorrect writable partitions for my_topic:", parts)
  111. }
  112. tst, err := client.Leader("my_topic", 0)
  113. if err != nil {
  114. t.Error(err)
  115. } else if tst.ID() != 5 {
  116. t.Error("Leader for my_topic had incorrect ID.")
  117. }
  118. replicas, err = client.Replicas("my_topic", 0)
  119. if err != nil {
  120. t.Error(err)
  121. } else if replicas[0] != 1 {
  122. t.Error("Incorrect (or unsorted) replica")
  123. } else if replicas[1] != 3 {
  124. t.Error("Incorrect (or unsorted) replica")
  125. } else if replicas[2] != 5 {
  126. t.Error("Incorrect (or unsorted) replica")
  127. }
  128. isr, err = client.ReplicasInSync("my_topic", 0)
  129. if err != nil {
  130. t.Error(err)
  131. } else if isr[0] != 1 {
  132. t.Error("Incorrect (or unsorted) isr")
  133. } else if isr[1] != 5 {
  134. t.Error("Incorrect (or unsorted) isr")
  135. }
  136. }
  137. func TestClientRefreshBehaviour(t *testing.T) {
  138. mb1 := NewMockBroker(t, 1)
  139. mb5 := NewMockBroker(t, 5)
  140. mdr := new(MetadataResponse)
  141. mdr.AddBroker(mb5.Addr(), mb5.BrokerID())
  142. mb1.Returns(mdr)
  143. mdr2 := new(MetadataResponse)
  144. mdr2.AddTopicPartition("my_topic", 0xb, mb5.BrokerID(), nil, nil, NoError)
  145. mb1.Returns(mdr2)
  146. client, err := NewClient("clientID", []string{mb1.Addr()}, nil)
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. parts, err := client.Partitions("my_topic")
  151. if err != nil {
  152. t.Error(err)
  153. } else if len(parts) != 1 || parts[0] != 0xb {
  154. t.Error("Client returned incorrect partitions for my_topic:", parts)
  155. }
  156. tst, err := client.Leader("my_topic", 0xb)
  157. if err != nil {
  158. t.Error(err)
  159. } else if tst.ID() != 5 {
  160. t.Error("Leader for my_topic had incorrect ID.")
  161. }
  162. client.disconnectBroker(tst)
  163. mb5.Close()
  164. mb1.Close()
  165. safeClose(t, client)
  166. }