Skip to content

Commit

Permalink
Use TesTLogger in TestProducerPublish
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bittorf committed Apr 10, 2017
1 parent b9762cd commit 1f4182b
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 1f4182b

Please sign in to comment.