client_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package sarama
  2. import (
  3. "encoding/binary"
  4. "testing"
  5. )
  6. func TestSimpleClient(t *testing.T) {
  7. responses := make(chan []byte, 1)
  8. mockBroker := NewMockBroker(t, responses)
  9. defer mockBroker.Close()
  10. // Only one response needed, an empty metadata response
  11. responses <- []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
  12. client, err := NewClient("client_id", []string{mockBroker.Addr()}, nil)
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. client.Close()
  17. }
  18. func TestClientExtraBrokers(t *testing.T) {
  19. responses := make(chan []byte, 1)
  20. mockBroker := NewMockBroker(t, responses)
  21. mockExtra := NewMockBroker(t, make(chan []byte))
  22. defer mockBroker.Close()
  23. defer mockExtra.Close()
  24. // return the extra mock as another available broker
  25. response := []byte{
  26. 0x00, 0x00, 0x00, 0x01,
  27. 0x00, 0x00, 0x00, 0x01,
  28. 0x00, 0x09, 'l', 'o', 'c', 'a', 'l', 'h', 'o', 's', 't',
  29. 0x00, 0x00, 0x00, 0x00,
  30. 0x00, 0x00, 0x00, 0x00}
  31. binary.BigEndian.PutUint32(response[19:], uint32(mockExtra.Port()))
  32. responses <- response
  33. client, err := NewClient("client_id", []string{mockBroker.Addr()}, nil)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. client.Close()
  38. }
  39. func TestClientMetadata(t *testing.T) {
  40. responses := make(chan []byte, 1)
  41. mockBroker := NewMockBroker(t, responses)
  42. mockExtra := NewMockBroker(t, make(chan []byte))
  43. defer mockBroker.Close()
  44. defer mockExtra.Close()
  45. // return the extra mock as another available broker
  46. response := []byte{
  47. 0x00, 0x00, 0x00, 0x01,
  48. 0x00, 0x00, 0x00, 0x05,
  49. 0x00, 0x09, 'l', 'o', 'c', 'a', 'l', 'h', 'o', 's', 't',
  50. 0x00, 0x00, 0x00, 0x00,
  51. 0x00, 0x00, 0x00, 0x01,
  52. 0x00, 0x00,
  53. 0x00, 0x08, 'm', 'y', '_', 't', 'o', 'p', 'i', 'c',
  54. 0x00, 0x00, 0x00, 0x01,
  55. 0x00, 0x00,
  56. 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x05,
  58. 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00}
  60. binary.BigEndian.PutUint32(response[19:], uint32(mockExtra.Port()))
  61. responses <- response
  62. client, err := NewClient("client_id", []string{mockBroker.Addr()}, nil)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. defer client.Close()
  67. topics, err := client.Topics()
  68. if err != nil {
  69. t.Error(err)
  70. } else if len(topics) != 1 || topics[0] != "my_topic" {
  71. t.Error("Client returned incorrect topics:", topics)
  72. }
  73. parts, err := client.Partitions("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 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. }
  86. func TestClientRefreshBehaviour(t *testing.T) {
  87. responses := make(chan []byte, 1)
  88. extraResponses := make(chan []byte, 2)
  89. mockBroker := NewMockBroker(t, responses)
  90. mockExtra := NewMockBroker(t, extraResponses)
  91. defer mockBroker.Close()
  92. defer mockExtra.Close()
  93. // return the extra mock as another available broker
  94. response := []byte{
  95. 0x00, 0x00, 0x00, 0x01,
  96. 0x00, 0x00, 0x00, 0xaa,
  97. 0x00, 0x09, 'l', 'o', 'c', 'a', 'l', 'h', 'o', 's', 't',
  98. 0x00, 0x00, 0x00, 0x00,
  99. 0x00, 0x00, 0x00, 0x00}
  100. binary.BigEndian.PutUint32(response[19:], uint32(mockExtra.Port()))
  101. responses <- response
  102. extraResponses <- []byte{
  103. 0x00, 0x00, 0x00, 0x00,
  104. 0x00, 0x00, 0x00, 0x01,
  105. 0x00, 0x00,
  106. 0x00, 0x08, 'm', 'y', '_', 't', 'o', 'p', 'i', 'c',
  107. 0x00, 0x00, 0x00, 0x01,
  108. 0x00, 0x05,
  109. 0x00, 0x00, 0x00, 0x0e,
  110. 0xFF, 0xFF, 0xFF, 0xFF,
  111. 0x00, 0x00, 0x00, 0x00,
  112. 0x00, 0x00, 0x00, 0x00}
  113. extraResponses <- []byte{
  114. 0x00, 0x00, 0x00, 0x00,
  115. 0x00, 0x00, 0x00, 0x01,
  116. 0x00, 0x00,
  117. 0x00, 0x08, 'm', 'y', '_', 't', 'o', 'p', 'i', 'c',
  118. 0x00, 0x00, 0x00, 0x01,
  119. 0x00, 0x00,
  120. 0x00, 0x00, 0x00, 0x0b,
  121. 0x00, 0x00, 0x00, 0xaa,
  122. 0x00, 0x00, 0x00, 0x00,
  123. 0x00, 0x00, 0x00, 0x00}
  124. client, err := NewClient("clientID", []string{mockBroker.Addr()}, &ClientConfig{MetadataRetries: 1})
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. defer client.Close()
  129. parts, err := client.Partitions("my_topic")
  130. if err != nil {
  131. t.Error(err)
  132. } else if len(parts) != 1 || parts[0] != 0xb {
  133. t.Error("Client returned incorrect partitions for my_topic:", parts)
  134. }
  135. tst, err := client.Leader("my_topic", 0xb)
  136. if err != nil {
  137. t.Error(err)
  138. } else if tst.ID() != 0xaa {
  139. t.Error("Leader for my_topic had incorrect ID.")
  140. }
  141. client.disconnectBroker(tst)
  142. }