client_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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("clientID", "localhost", mockBroker.Port())
  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("clientID", "localhost", mockBroker.Port())
  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, 0x07, '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("clientID", "localhost", mockBroker.Port())
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. parts, err := client.partitions("myTopic")
  67. if err != nil {
  68. t.Error(err)
  69. } else if len(parts) != 1 || parts[0] != 0 {
  70. t.Error("Client returned incorrect partitions for myTopic:", parts)
  71. }
  72. tst, err := client.leader("myTopic", 0)
  73. if err != nil {
  74. t.Error(err)
  75. } else if tst.ID() != 5 {
  76. t.Error("Leader for myTopic had incorrect ID.")
  77. }
  78. client.Close()
  79. }
  80. func TestClientRefreshBehaviour(t *testing.T) {
  81. responses := make(chan []byte, 3)
  82. mockBroker := NewMockBroker(t, responses)
  83. mockExtra := NewMockBroker(t, make(chan []byte))
  84. defer mockBroker.Close()
  85. defer mockExtra.Close()
  86. // return the extra mock as another available broker
  87. response := []byte{
  88. 0x00, 0x00, 0x00, 0x01,
  89. 0x00, 0x00, 0x00, 0xaa,
  90. 0x00, 0x09, 'l', 'o', 'c', 'a', 'l', 'h', 'o', 's', 't',
  91. 0x00, 0x00, 0x00, 0x00,
  92. 0x00, 0x00, 0x00, 0x00}
  93. binary.BigEndian.PutUint32(response[19:], uint32(mockExtra.Port()))
  94. responses <- response
  95. responses <- []byte{
  96. 0x00, 0x00, 0x00, 0x00,
  97. 0x00, 0x00, 0x00, 0x01,
  98. 0x00, 0x00,
  99. 0x00, 0x07, 'm', 'y', 'T', 'o', 'p', 'i', 'c',
  100. 0x00, 0x00, 0x00, 0x01,
  101. 0x00, 0x05,
  102. 0x00, 0x00, 0x00, 0x0e,
  103. 0xFF, 0xFF, 0xFF, 0xFF,
  104. 0x00, 0x00, 0x00, 0x00,
  105. 0x00, 0x00, 0x00, 0x00}
  106. responses <- []byte{
  107. 0x00, 0x00, 0x00, 0x00,
  108. 0x00, 0x00, 0x00, 0x01,
  109. 0x00, 0x00,
  110. 0x00, 0x07, 'm', 'y', 'T', 'o', 'p', 'i', 'c',
  111. 0x00, 0x00, 0x00, 0x01,
  112. 0x00, 0x00,
  113. 0x00, 0x00, 0x00, 0x0b,
  114. 0x00, 0x00, 0x00, 0xaa,
  115. 0x00, 0x00, 0x00, 0x00,
  116. 0x00, 0x00, 0x00, 0x00}
  117. client, err := NewClient("clientID", "localhost", mockBroker.Port())
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. parts, err := client.partitions("myTopic")
  122. if err != nil {
  123. t.Error(err)
  124. } else if len(parts) != 1 || parts[0] != 0xb {
  125. t.Error("Client returned incorrect partitions for myTopic:", parts)
  126. }
  127. tst, err := client.leader("myTopic", 0xb)
  128. if err != nil {
  129. t.Error(err)
  130. } else if tst.ID() != 0xaa {
  131. t.Error("Leader for myTopic had incorrect ID.")
  132. }
  133. client.disconnectBroker(tst)
  134. client.Close()
  135. }