consumer_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package sarama
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. )
  7. func TestSimpleConsumer(t *testing.T) {
  8. mb1 := NewMockBroker(t, 1)
  9. mb2 := NewMockBroker(t, 2)
  10. mdr := new(MetadataResponse)
  11. mdr.AddBroker(mb2.Addr(), int32(mb2.BrokerID()))
  12. mdr.AddTopicPartition("my_topic", 0, 2)
  13. mb1.Returns(mdr)
  14. for i := 0; i < 10; i++ {
  15. fr := new(FetchResponse)
  16. fr.AddMessage("my_topic", 0, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(i))
  17. mb2.Returns(fr)
  18. }
  19. client, err := NewClient("client_id", []string{mb1.Addr()}, &ClientConfig{MetadataRetries: 1, WaitForElection: 250 * time.Millisecond})
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. defer client.Close()
  24. consumer, err := NewConsumer(client, "my_topic", 0, "my_consumer_group", &ConsumerConfig{MaxWaitTime: 100})
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. defer consumer.Close()
  29. defer mb1.Close()
  30. defer mb2.Close()
  31. for i := 0; i < 10; i++ {
  32. event := <-consumer.Events()
  33. if event.Err != nil {
  34. t.Error(err)
  35. }
  36. if event.Offset != int64(i) {
  37. t.Error("Incorrect message offset!")
  38. }
  39. }
  40. }
  41. func TestConsumerRawOffset(t *testing.T) {
  42. mb1 := NewMockBroker(t, 1)
  43. mb2 := NewMockBroker(t, 2)
  44. mdr := new(MetadataResponse)
  45. mdr.AddBroker(mb2.Addr(), int32(mb2.BrokerID()))
  46. mdr.AddTopicPartition("my_topic", 0, 2)
  47. mb1.Returns(mdr)
  48. client, err := NewClient("client_id", []string{mb1.Addr()}, &ClientConfig{MetadataRetries: 1, WaitForElection: 250 * time.Millisecond})
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. defer client.Close()
  53. consumer, err := NewConsumer(client, "my_topic", 0, "my_consumer_group", &ConsumerConfig{OffsetMethod: OffsetMethodManual, OffsetValue: 1234, MaxWaitTime: 100})
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. defer consumer.Close()
  58. defer mb1.Close()
  59. defer mb2.Close()
  60. if consumer.offset != 1234 {
  61. t.Error("Raw offset not set correctly")
  62. }
  63. }
  64. func TestConsumerLatestOffset(t *testing.T) {
  65. mb1 := NewMockBroker(t, 1)
  66. mb2 := NewMockBroker(t, 2)
  67. mdr := new(MetadataResponse)
  68. mdr.AddBroker(mb2.Addr(), int32(mb2.BrokerID()))
  69. mdr.AddTopicPartition("my_topic", 0, 2)
  70. mb1.Returns(mdr)
  71. or := new(OffsetResponse)
  72. or.AddTopicPartition("my_topic", 0, 0x010101)
  73. mb2.Returns(or)
  74. client, err := NewClient("client_id", []string{mb1.Addr()}, &ClientConfig{MetadataRetries: 1, WaitForElection: 250 * time.Millisecond})
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. defer client.Close()
  79. consumer, err := NewConsumer(client, "my_topic", 0, "my_consumer_group", &ConsumerConfig{OffsetMethod: OffsetMethodNewest, MaxWaitTime: 100})
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. defer consumer.Close()
  84. defer mb2.Close()
  85. defer mb1.Close()
  86. if consumer.offset != 0x010101 {
  87. t.Error("Latest offset not fetched correctly")
  88. }
  89. }
  90. func ExampleConsumer() {
  91. client, err := NewClient("my_client", []string{"localhost:9092"}, &ClientConfig{MetadataRetries: 1, WaitForElection: 250 * time.Millisecond})
  92. if err != nil {
  93. panic(err)
  94. } else {
  95. fmt.Println("> connected")
  96. }
  97. defer client.Close()
  98. consumer, err := NewConsumer(client, "my_topic", 0, "my_consumer_group", &ConsumerConfig{MaxWaitTime: 200})
  99. if err != nil {
  100. panic(err)
  101. } else {
  102. fmt.Println("> consumer ready")
  103. }
  104. defer consumer.Close()
  105. msgCount := 0
  106. consumerLoop:
  107. for {
  108. select {
  109. case event := <-consumer.Events():
  110. if event.Err != nil {
  111. panic(event.Err)
  112. }
  113. msgCount += 1
  114. case <-time.After(5 * time.Second):
  115. fmt.Println("> timed out")
  116. break consumerLoop
  117. }
  118. }
  119. fmt.Println("Got", msgCount, "messages.")
  120. }