Skip to content

Commit

Permalink
Merge pull request #627 from Shopify/remove-testing-package
Browse files Browse the repository at this point in the history
Don't import `testing` in the main lib
  • Loading branch information
eapache committed Mar 10, 2016
2 parents 241c8f8 + 6888ecc commit 8717226
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
11 changes: 5 additions & 6 deletions mockbroker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"reflect"
"strconv"
"sync"
"testing"
"time"

"github.com/davecgh/go-spew/spew"
Expand Down Expand Up @@ -52,7 +51,7 @@ type MockBroker struct {
stopper chan none
expectations chan encoder
listener net.Listener
t *testing.T
t TestReporter
latency time.Duration
handler requestHandlerFunc
history []RequestResponse
Expand Down Expand Up @@ -255,16 +254,16 @@ func (b *MockBroker) serverError(err error) {
b.t.Errorf(err.Error())
}

// NewMockBroker launches a fake Kafka broker. It takes a *testing.T as provided by the
// NewMockBroker launches a fake Kafka broker. It takes a TestReporter as provided by the
// test framework and a channel of responses to use. If an error occurs it is
// simply logged to the *testing.T and the broker exits.
func NewMockBroker(t *testing.T, brokerID int32) *MockBroker {
// simply logged to the TestReporter and the broker exits.
func NewMockBroker(t TestReporter, brokerID int32) *MockBroker {
return NewMockBrokerAddr(t, brokerID, "localhost:0")
}

// NewMockBrokerAddr behaves like newMockBroker but listens on the address you give
// it rather than just some ephemeral port.
func NewMockBrokerAddr(t *testing.T, brokerID int32, addr string) *MockBroker {
func NewMockBrokerAddr(t TestReporter, brokerID int32, addr string) *MockBroker {
var err error

broker := &MockBroker{
Expand Down
38 changes: 23 additions & 15 deletions mockresponses.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ package sarama

import (
"fmt"
"testing"
)

// TestReporter has methods matching go's testing.T to avoid importing
// `testing` in the main part of the library.
type TestReporter interface {
Error(...interface{})
Errorf(string, ...interface{})
Fatal(...interface{})
Fatalf(string, ...interface{})
}

// MockResponse is a response builder interface it defines one method that
// allows generating a response based on a request body. MockResponses are used
// to program behavior of MockBroker in tests.
Expand Down Expand Up @@ -62,10 +70,10 @@ func (mc *MockSequence) For(reqBody decoder) (res encoder) {
type MockMetadataResponse struct {
leaders map[string]map[int32]int32
brokers map[string]int32
t *testing.T
t TestReporter
}

func NewMockMetadataResponse(t *testing.T) *MockMetadataResponse {
func NewMockMetadataResponse(t TestReporter) *MockMetadataResponse {
return &MockMetadataResponse{
leaders: make(map[string]map[int32]int32),
brokers: make(map[string]int32),
Expand Down Expand Up @@ -113,10 +121,10 @@ func (mmr *MockMetadataResponse) For(reqBody decoder) encoder {
// MockOffsetResponse is an `OffsetResponse` builder.
type MockOffsetResponse struct {
offsets map[string]map[int32]map[int64]int64
t *testing.T
t TestReporter
}

func NewMockOffsetResponse(t *testing.T) *MockOffsetResponse {
func NewMockOffsetResponse(t TestReporter) *MockOffsetResponse {
return &MockOffsetResponse{
offsets: make(map[string]map[int32]map[int64]int64),
t: t,
Expand Down Expand Up @@ -170,11 +178,11 @@ func (mor *MockOffsetResponse) getOffset(topic string, partition int32, time int
type MockFetchResponse struct {
messages map[string]map[int32]map[int64]Encoder
highWaterMarks map[string]map[int32]int64
t *testing.T
t TestReporter
batchSize int
}

func NewMockFetchResponse(t *testing.T, batchSize int) *MockFetchResponse {
func NewMockFetchResponse(t TestReporter, batchSize int) *MockFetchResponse {
return &MockFetchResponse{
messages: make(map[string]map[int32]map[int64]Encoder),
highWaterMarks: make(map[string]map[int32]int64),
Expand Down Expand Up @@ -270,10 +278,10 @@ func (mfr *MockFetchResponse) getHighWaterMark(topic string, partition int32) in
// MockConsumerMetadataResponse is a `ConsumerMetadataResponse` builder.
type MockConsumerMetadataResponse struct {
coordinators map[string]interface{}
t *testing.T
t TestReporter
}

func NewMockConsumerMetadataResponse(t *testing.T) *MockConsumerMetadataResponse {
func NewMockConsumerMetadataResponse(t TestReporter) *MockConsumerMetadataResponse {
return &MockConsumerMetadataResponse{
coordinators: make(map[string]interface{}),
t: t,
Expand Down Expand Up @@ -307,10 +315,10 @@ func (mr *MockConsumerMetadataResponse) For(reqBody decoder) encoder {
// MockOffsetCommitResponse is a `OffsetCommitResponse` builder.
type MockOffsetCommitResponse struct {
errors map[string]map[string]map[int32]KError
t *testing.T
t TestReporter
}

func NewMockOffsetCommitResponse(t *testing.T) *MockOffsetCommitResponse {
func NewMockOffsetCommitResponse(t TestReporter) *MockOffsetCommitResponse {
return &MockOffsetCommitResponse{t: t}
}

Expand Down Expand Up @@ -363,10 +371,10 @@ func (mr *MockOffsetCommitResponse) getError(group, topic string, partition int3
// MockProduceResponse is a `ProduceResponse` builder.
type MockProduceResponse struct {
errors map[string]map[int32]KError
t *testing.T
t TestReporter
}

func NewMockProduceResponse(t *testing.T) *MockProduceResponse {
func NewMockProduceResponse(t TestReporter) *MockProduceResponse {
return &MockProduceResponse{t: t}
}

Expand Down Expand Up @@ -409,10 +417,10 @@ func (mr *MockProduceResponse) getError(topic string, partition int32) KError {
// MockOffsetFetchResponse is a `OffsetFetchResponse` builder.
type MockOffsetFetchResponse struct {
offsets map[string]map[string]map[int32]*OffsetFetchResponseBlock
t *testing.T
t TestReporter
}

func NewMockOffsetFetchResponse(t *testing.T) *MockOffsetFetchResponse {
func NewMockOffsetFetchResponse(t TestReporter) *MockOffsetFetchResponse {
return &MockOffsetFetchResponse{t: t}
}

Expand Down

0 comments on commit 8717226

Please sign in to comment.