client_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 TestClientSeedBrokers(t *testing.T) {
  29. mb1 := NewMockBroker(t, 1)
  30. mb2 := NewMockBroker(t, 2)
  31. mdr := new(MetadataResponse)
  32. mdr.AddBroker(mb2.Addr(), mb2.BrokerID())
  33. mb1.Returns(mdr)
  34. client, err := NewClient("client_id", []string{mb1.Addr()}, nil)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. defer safeClose(t, client)
  39. defer mb1.Close()
  40. defer mb2.Close()
  41. }
  42. func TestClientMetadata(t *testing.T) {
  43. mb1 := NewMockBroker(t, 1)
  44. mb5 := NewMockBroker(t, 5)
  45. replicas := []int32{3, 1, 5}
  46. isr := []int32{5, 1}
  47. mdr := new(MetadataResponse)
  48. mdr.AddBroker(mb5.Addr(), mb5.BrokerID())
  49. mdr.AddTopicPartition("my_topic", 0, mb5.BrokerID(), replicas, isr, NoError)
  50. mdr.AddTopicPartition("my_topic", 1, mb5.BrokerID(), replicas, isr, LeaderNotAvailable)
  51. mb1.Returns(mdr)
  52. config := NewClientConfig()
  53. config.MetadataRetries = 0
  54. client, err := NewClient("client_id", []string{mb1.Addr()}, config)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. defer safeClose(t, client)
  59. defer mb1.Close()
  60. defer mb5.Close()
  61. topics, err := client.Topics()
  62. if err != nil {
  63. t.Error(err)
  64. } else if len(topics) != 1 || topics[0] != "my_topic" {
  65. t.Error("Client returned incorrect topics:", topics)
  66. }
  67. parts, err := client.Partitions("my_topic")
  68. if err != nil {
  69. t.Error(err)
  70. } else if len(parts) != 2 || parts[0] != 0 || parts[1] != 1 {
  71. t.Error("Client returned incorrect partitions for my_topic:", parts)
  72. }
  73. parts, err = client.WritablePartitions("my_topic")
  74. if err != nil {
  75. t.Error(err)
  76. } else if len(parts) != 1 || parts[0] != 0 {
  77. t.Error("Client returned incorrect writable partitions for my_topic:", parts)
  78. }
  79. tst, err := client.Leader("my_topic", 0)
  80. if err != nil {
  81. t.Error(err)
  82. } else if tst.ID() != 5 {
  83. t.Error("Leader for my_topic had incorrect ID.")
  84. }
  85. replicas, err = client.Replicas("my_topic", 0)
  86. if err != nil {
  87. t.Error(err)
  88. } else if replicas[0] != 1 {
  89. t.Error("Incorrect (or unsorted) replica")
  90. } else if replicas[1] != 3 {
  91. t.Error("Incorrect (or unsorted) replica")
  92. } else if replicas[2] != 5 {
  93. t.Error("Incorrect (or unsorted) replica")
  94. }
  95. isr, err = client.ReplicasInSync("my_topic", 0)
  96. if err != nil {
  97. t.Error(err)
  98. } else if isr[0] != 1 {
  99. t.Error("Incorrect (or unsorted) isr")
  100. } else if isr[1] != 5 {
  101. t.Error("Incorrect (or unsorted) isr")
  102. }
  103. }
  104. func TestClientRefreshBehaviour(t *testing.T) {
  105. mb1 := NewMockBroker(t, 1)
  106. mb5 := NewMockBroker(t, 5)
  107. mdr := new(MetadataResponse)
  108. mdr.AddBroker(mb5.Addr(), mb5.BrokerID())
  109. mb1.Returns(mdr)
  110. mdr2 := new(MetadataResponse)
  111. mdr2.AddTopicPartition("my_topic", 0xb, mb5.BrokerID(), nil, nil, NoError)
  112. mb5.Returns(mdr2)
  113. client, err := NewClient("clientID", []string{mb1.Addr()}, nil)
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. parts, err := client.Partitions("my_topic")
  118. if err != nil {
  119. t.Error(err)
  120. } else if len(parts) != 1 || parts[0] != 0xb {
  121. t.Error("Client returned incorrect partitions for my_topic:", parts)
  122. }
  123. tst, err := client.Leader("my_topic", 0xb)
  124. if err != nil {
  125. t.Error(err)
  126. } else if tst.ID() != 5 {
  127. t.Error("Leader for my_topic had incorrect ID.")
  128. }
  129. client.disconnectBroker(tst)
  130. mb5.Close()
  131. mb1.Close()
  132. safeClose(t, client)
  133. }