Created
March 5, 2019 10:14
-
-
Save mufti1/1e3539039f193cb0b795b6d417d691d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package consumer_test | |
| import ( | |
| "os" | |
| "testing" | |
| "time" | |
| "github.com/Shopify/sarama" | |
| "github.com/Shopify/sarama/mocks" | |
| "github.com/mufti1/kafka-example/consumer" | |
| ) | |
| func TestConsume(t *testing.T) { | |
| consumers := mocks.NewConsumer(t, nil) | |
| defer func() { | |
| if err := consumers.Close(); err != nil { | |
| t.Error(err) | |
| } | |
| }() | |
| consumers.SetTopicMetadata(map[string][]int32{ | |
| "test_topic": {0}, | |
| }) | |
| kafka := &consumer.KafkaConsumer{ | |
| Consumer: consumers, | |
| } | |
| consumers.ExpectConsumePartition("test_topic", 0, sarama.OffsetNewest).YieldMessage(&sarama.ConsumerMessage{Value: []byte("hello world")}) | |
| signals := make(chan os.Signal, 1) | |
| go kafka.Consume([]string{"test_topic"}, signals) | |
| timeout := time.After(2 * time.Second) | |
| for { | |
| select { | |
| case <-timeout: | |
| signals <- os.Interrupt | |
| return | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment