consumer_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package kafka
  2. import (
  3. "encoding/binary"
  4. "sarama/mock"
  5. "testing"
  6. )
  7. func TestSimpleConsumer(t *testing.T) {
  8. masterResponses := make(chan []byte, 1)
  9. extraResponses := make(chan []byte, 2)
  10. mockBroker := mock.NewBroker(t, masterResponses)
  11. mockExtra := mock.NewBroker(t, extraResponses)
  12. defer mockBroker.Close()
  13. defer mockExtra.Close()
  14. // return the extra mock as another available broker
  15. response := []byte{
  16. 0x00, 0x00, 0x00, 0x01,
  17. 0x00, 0x00, 0x00, 0x01,
  18. 0x00, 0x09, 'l', 'o', 'c', 'a', 'l', 'h', 'o', 's', 't',
  19. 0x00, 0x00, 0x00, 0x00,
  20. 0x00, 0x00, 0x00, 0x01,
  21. 0x00, 0x00,
  22. 0x00, 0x07, 'm', 'y', 'T', 'o', 'p', 'i', 'c',
  23. 0x00, 0x00, 0x00, 0x01,
  24. 0x00, 0x00,
  25. 0x00, 0x00, 0x00, 0x00,
  26. 0x00, 0x00, 0x00, 0x01,
  27. 0x00, 0x00, 0x00, 0x00,
  28. 0x00, 0x00, 0x00, 0x00}
  29. binary.BigEndian.PutUint32(response[19:], uint32(mockExtra.Port()))
  30. masterResponses <- response
  31. extraResponses <- nil
  32. extraResponses <- []byte{
  33. 0x00, 0x00, 0x00, 0x01,
  34. 0x00, 0x07, 'm', 'y', 'T', 'o', 'p', 'i', 'c',
  35. 0x00, 0x00, 0x00, 0x01,
  36. 0x00, 0x00, 0x00, 0x00,
  37. 0x00, 0x00,
  38. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39. 0x00, 0x00, 0x00, 0x00}
  40. client, err := NewClient("clientID", "localhost", mockBroker.Port())
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. consumer, err := NewConsumer(client, "myTopic", 0, "myConsumerGroup")
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. consumer.Close()
  49. client.Close()
  50. }