Skip to content

Instantly share code, notes, and snippets.

@mufti1
Created March 5, 2019 10:14
Show Gist options
  • Select an option

  • Save mufti1/1e3539039f193cb0b795b6d417d691d8 to your computer and use it in GitHub Desktop.

Select an option

Save mufti1/1e3539039f193cb0b795b6d417d691d8 to your computer and use it in GitHub Desktop.
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