From 1f4182b85842351fae21b07eb8aee5c18bbaafc4 Mon Sep 17 00:00:00 2001 From: Michael Bittorf Date: Mon, 10 Apr 2017 23:26:02 +0200 Subject: [PATCH] Use TesTLogger in TestProducerPublish --- producer_test.go | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/producer_test.go b/producer_test.go index 9be29006..c8d6c955 100755 --- a/producer_test.go +++ b/producer_test.go @@ -13,8 +13,22 @@ import ( "sync/atomic" "testing" "time" + "strings" ) +type TestLogger struct { + t *testing.T +} +func (tl TestLogger) Error(s string) { tl.t.Fatal(s) } +func (tl TestLogger) Info(s string) { } +func (tl TestLogger) Output(c int, s string) error { + if strings.Contains(s, "ERR") { + tl.t.Fatalf("error string: %s", s) + } + return nil +} +func NewTestLogger(t *testing.T) *TestLogger { return &TestLogger{t} } + type ConsumerHandler struct { t *testing.T q *Consumer @@ -87,22 +101,26 @@ func TestProducerPublish(t *testing.T) { topicName := "publish" + strconv.Itoa(int(time.Now().Unix())) msgCount := 10 + testLogger := NewTestLogger(t) + config := NewConfig() - w, _ := NewProducer("127.0.0.1:4150", config) - w.SetLogger(nullLogger, LogLevelInfo) - defer w.Stop() + func() { + w, _ := NewProducer("127.0.0.1:4150", config) + w.SetLogger(testLogger, LogLevelInfo) + defer w.Stop() + + for i := 0; i < msgCount; i++ { + err := w.Publish(topicName, []byte("publish_test_case")) + if err != nil { + t.Fatalf("error %s", err) + } + } - for i := 0; i < msgCount; i++ { - err := w.Publish(topicName, []byte("publish_test_case")) + err := w.Publish(topicName, []byte("bad_test_case")) if err != nil { t.Fatalf("error %s", err) } - } - - err := w.Publish(topicName, []byte("bad_test_case")) - if err != nil { - t.Fatalf("error %s", err) - } + }() readMessages(topicName, t, msgCount) }