client_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. client, err := NewClient([]string{seedBroker.Addr()}, config)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. // Verify they aren't cached the same
  38. allP := client.cachedPartitionsResults["my_topic"][allPartitions]
  39. writeP := client.cachedPartitionsResults["my_topic"][writablePartitions]
  40. if len(allP) == len(writeP) {
  41. t.Fatal("Invalid lengths!")
  42. }
  43. tmp := client.cachedPartitionsResults["my_topic"]
  44. // Verify we actually use the cache at all!
  45. tmp[allPartitions] = []int32{1, 2, 3, 4}
  46. client.cachedPartitionsResults["my_topic"] = tmp
  47. if 4 != len(client.cachedPartitions("my_topic", allPartitions)) {
  48. t.Fatal("Not using the cache!")
  49. }
  50. seedBroker.Close()
  51. safeClose(t, client)
  52. }
  53. func TestClientSeedBrokers(t *testing.T) {
  54. seedBroker := newMockBroker(t, 1)
  55. metadataResponse := new(MetadataResponse)
  56. metadataResponse.AddBroker("localhost:12345", 2)
  57. seedBroker.Returns(metadataResponse)
  58. client, err := NewClient([]string{seedBroker.Addr()}, nil)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. seedBroker.Close()
  63. safeClose(t, client)
  64. }
  65. func TestClientMetadata(t *testing.T) {
  66. seedBroker := newMockBroker(t, 1)
  67. leader := newMockBroker(t, 5)
  68. replicas := []int32{3, 1, 5}
  69. isr := []int32{5, 1}
  70. metadataResponse := new(MetadataResponse)
  71. metadataResponse.AddBroker(leader.Addr(), leader.BrokerID())
  72. metadataResponse.AddTopicPartition("my_topic", 0, leader.BrokerID(), replicas, isr, ErrNoError)
  73. metadataResponse.AddTopicPartition("my_topic", 1, leader.BrokerID(), replicas, isr, ErrLeaderNotAvailable)
  74. seedBroker.Returns(metadataResponse)
  75. config := NewConfig()
  76. config.Metadata.Retry.Max = 0
  77. client, err := NewClient([]string{seedBroker.Addr()}, config)
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. topics, err := client.Topics()
  82. if err != nil {
  83. t.Error(err)
  84. } else if len(topics) != 1 || topics[0] != "my_topic" {
  85. t.Error("Client returned incorrect topics:", topics)
  86. }
  87. parts, err := client.Partitions("my_topic")
  88. if err != nil {
  89. t.Error(err)
  90. } else if len(parts) != 2 || parts[0] != 0 || parts[1] != 1 {
  91. t.Error("Client returned incorrect partitions for my_topic:", parts)
  92. }
  93. parts, err = client.WritablePartitions("my_topic")
  94. if err != nil {
  95. t.Error(err)
  96. } else if len(parts) != 1 || parts[0] != 0 {
  97. t.Error("Client returned incorrect writable partitions for my_topic:", parts)
  98. }
  99. tst, err := client.Leader("my_topic", 0)
  100. if err != nil {
  101. t.Error(err)
  102. } else if tst.ID() != 5 {
  103. t.Error("Leader for my_topic had incorrect ID.")
  104. }
  105. replicas, err = client.Replicas("my_topic", 0)
  106. if err != nil {
  107. t.Error(err)
  108. } else if replicas[0] != 1 {
  109. t.Error("Incorrect (or unsorted) replica")
  110. } else if replicas[1] != 3 {
  111. t.Error("Incorrect (or unsorted) replica")
  112. } else if replicas[2] != 5 {
  113. t.Error("Incorrect (or unsorted) replica")
  114. }
  115. isr, err = client.ReplicasInSync("my_topic", 0)
  116. if err != nil {
  117. t.Error(err)
  118. } else if isr[0] != 1 {
  119. t.Error("Incorrect (or unsorted) isr")
  120. } else if isr[1] != 5 {
  121. t.Error("Incorrect (or unsorted) isr")
  122. }
  123. leader.Close()
  124. seedBroker.Close()
  125. safeClose(t, client)
  126. }
  127. func TestClientRefreshBehaviour(t *testing.T) {
  128. seedBroker := newMockBroker(t, 1)
  129. leader := newMockBroker(t, 5)
  130. metadataResponse1 := new(MetadataResponse)
  131. metadataResponse1.AddBroker(leader.Addr(), leader.BrokerID())
  132. seedBroker.Returns(metadataResponse1)
  133. metadataResponse2 := new(MetadataResponse)
  134. metadataResponse2.AddTopicPartition("my_topic", 0xb, leader.BrokerID(), nil, nil, ErrNoError)
  135. seedBroker.Returns(metadataResponse2)
  136. client, err := NewClient([]string{seedBroker.Addr()}, nil)
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. parts, err := client.Partitions("my_topic")
  141. if err != nil {
  142. t.Error(err)
  143. } else if len(parts) != 1 || parts[0] != 0xb {
  144. t.Error("Client returned incorrect partitions for my_topic:", parts)
  145. }
  146. tst, err := client.Leader("my_topic", 0xb)
  147. if err != nil {
  148. t.Error(err)
  149. } else if tst.ID() != 5 {
  150. t.Error("Leader for my_topic had incorrect ID.")
  151. }
  152. client.disconnectBroker(tst)
  153. leader.Close()
  154. seedBroker.Close()
  155. safeClose(t, client)
  156. }