consumer_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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", nil)
  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. config := NewConsumerConfig()
  54. config.OffsetMethod = OffsetMethodManual
  55. config.OffsetValue = 1234
  56. consumer, err := NewConsumer(client, "my_topic", 0, "my_consumer_group", config)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. defer consumer.Close()
  61. defer mb1.Close()
  62. defer mb2.Close()
  63. if consumer.offset != 1234 {
  64. t.Error("Raw offset not set correctly")
  65. }
  66. }
  67. func TestConsumerLatestOffset(t *testing.T) {
  68. mb1 := NewMockBroker(t, 1)
  69. mb2 := NewMockBroker(t, 2)
  70. mdr := new(MetadataResponse)
  71. mdr.AddBroker(mb2.Addr(), int32(mb2.BrokerID()))
  72. mdr.AddTopicPartition("my_topic", 0, 2)
  73. mb1.Returns(mdr)
  74. or := new(OffsetResponse)
  75. or.AddTopicPartition("my_topic", 0, 0x010101)
  76. mb2.Returns(or)
  77. client, err := NewClient("client_id", []string{mb1.Addr()}, &ClientConfig{MetadataRetries: 1, WaitForElection: 250 * time.Millisecond})
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. defer client.Close()
  82. config := NewConsumerConfig()
  83. config.OffsetMethod = OffsetMethodNewest
  84. consumer, err := NewConsumer(client, "my_topic", 0, "my_consumer_group", config)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. defer consumer.Close()
  89. defer mb2.Close()
  90. defer mb1.Close()
  91. if consumer.offset != 0x010101 {
  92. t.Error("Latest offset not fetched correctly")
  93. }
  94. }
  95. func ExampleConsumer() {
  96. client, err := NewClient("my_client", []string{"localhost:9092"}, &ClientConfig{MetadataRetries: 1, WaitForElection: 250 * time.Millisecond})
  97. if err != nil {
  98. panic(err)
  99. } else {
  100. fmt.Println("> connected")
  101. }
  102. defer client.Close()
  103. consumer, err := NewConsumer(client, "my_topic", 0, "my_consumer_group", &ConsumerConfig{MaxWaitTime: 200})
  104. if err != nil {
  105. panic(err)
  106. } else {
  107. fmt.Println("> consumer ready")
  108. }
  109. defer consumer.Close()
  110. msgCount := 0
  111. consumerLoop:
  112. for {
  113. select {
  114. case event := <-consumer.Events():
  115. if event.Err != nil {
  116. panic(event.Err)
  117. }
  118. msgCount++
  119. case <-time.After(5 * time.Second):
  120. fmt.Println("> timed out")
  121. break consumerLoop
  122. }
  123. }
  124. fmt.Println("Got", msgCount, "messages.")
  125. }