client_test.go 4.2 KB

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