Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate test logger in favour of new Zap test logger #104

Merged
merged 4 commits into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ sudo: required
language: go

go:
- "1.10.1"
- "1.11.1"

services:
- docker
Expand Down
3 changes: 3 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions script/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ set -e

cd "$(dirname "$0")/.."

os=$(uname -s | awk '{print tolower($0)}')

if [ -n "$CI" ] || ! command -v dep >/dev/null 2>&1; then
os=$(uname -s | awk '{print tolower($0)}')
curl -L "https://github.com/golang/dep/releases/download/v0.5.0/dep-$os-amd64" >"$GOPATH/bin/dep"
chmod +x "$GOPATH/bin/dep"
fi

if [ -n "$CI" ] || ! command -v shellcheck >/dev/null 2>&1; then
os=$(uname -s | awk '{print tolower($0)}')
if [ "$os" = "darwin" ]; then
echo >&2 "You need to install shellcheck before continuing."
fi
Expand All @@ -28,11 +28,21 @@ if [ -n "$CI" ] || ! command -v shellcheck >/dev/null 2>&1; then
fi

if [ -n "$CI" ] || ! command -v shfmt >/dev/null 2>&1; then
os=$(uname -s | awk '{print tolower($0)}')
curl -L "https://github.com/mvdan/sh/releases/download/v2.5.0/shfmt_v2.5.0_${os}_amd64" >"$GOPATH/bin/shfmt"

chmod +x "$GOPATH/bin/shfmt"
fi

go get -u github.com/alecthomas/gometalinter
gometalinter --install
if [ -n "$CI" ] || ! command -v gometalinter >/dev/null 2>&1; then
if [ "$os" = "darwin" ]; then
echo >&2 "You need to install gometalinter before continuing."
fi

tmp=$(mktemp -d)
curl -Ls "https://github.com/alecthomas/gometalinter/releases/download/v2.0.11/gometalinter-2.0.11-$os-amd64.tar.gz" |
tar xzf - --strip 1 -C "$tmp"

mkdir -p "$GOPATH/bin"
cp "${tmp}/"* "$GOPATH/bin/"
chmod +x "$GOPATH/bin/"*
fi
7 changes: 3 additions & 4 deletions streamclient/kafkaclient/eventhandlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package kafkaclient
import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/confluentinc/confluent-kafka-go/kafka"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/stretchr/testify/assert"
"go.uber.org/zap/zaptest"
)

func TestHandleError(t *testing.T) {
Expand All @@ -28,7 +27,7 @@ func TestHandleError(t *testing.T) {

err := testErr{errors.New(tt.err.String()), tt.err}
ch := make(chan error, 100)
logger := zap.NewNop()
logger := zaptest.NewLogger(t)

handleError(err, tt.ignores, ch, logger)

Expand Down
3 changes: 0 additions & 3 deletions streamclient/kafkaclient/producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/blendle/go-streamprocessor/streamutil/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

func TestIntegrationNewProducer(t *testing.T) {
Expand Down Expand Up @@ -234,8 +233,6 @@ func BenchmarkIntegrationProducer_Messages(b *testing.B) {
testutil.Integration(b)

topic := testutil.Random(b)
logger, err := zap.NewDevelopment()
require.NoError(b, err, logger)

// We use the default (production-like) config in this benchmark, to simulate
// real-world usage as best as possible.
Expand Down
24 changes: 9 additions & 15 deletions streamclient/kafkaclient/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/blendle/go-streamprocessor/streamutil/testutil"
"github.com/confluentinc/confluent-kafka-go/kafka"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
)

// TestConsumer returns a new kafka consumer to be used in test cases. It also
Expand Down Expand Up @@ -172,25 +172,22 @@ func TestOffsets(tb testing.TB, message stream.Message) []kafka.TopicPartition {
func TestConsumerConfig(tb testing.TB, topicAndGroup string, options ...streamconfig.Option) []streamconfig.Option {
var allOptions []streamconfig.Option

opts := streamconfig.ConsumerOptions(func(c *streamconfig.Consumer) {
c.Kafka = kafkaconfig.TestConsumer(tb)
c.Kafka.GroupID = topicAndGroup
c.Kafka.Topics = []string{topicAndGroup}
})

if testutil.Verbose(tb) {
logger, err := zap.NewDevelopment()
require.NoError(tb, err)

verbose := streamconfig.ConsumerOptions(func(c *streamconfig.Consumer) {
c.Logger = logger.Named("TestConsumer")
c.Kafka.Debug.CGRP = true
c.Kafka.Debug.Topic = true
})

allOptions = append(allOptions, verbose)
}

opts := streamconfig.ConsumerOptions(func(c *streamconfig.Consumer) {
c.Logger = zaptest.NewLogger(tb).Named("testConsumer")
c.Kafka = kafkaconfig.TestConsumer(tb)
c.Kafka.GroupID = topicAndGroup
c.Kafka.Topics = []string{topicAndGroup}
})

return append(append(allOptions, opts), options...)
}

Expand All @@ -200,11 +197,7 @@ func TestProducerConfig(tb testing.TB, topic string, options ...streamconfig.Opt
var allOptions []streamconfig.Option

if testutil.Verbose(tb) {
logger, err := zap.NewDevelopment()
require.NoError(tb, err)

verbose := streamconfig.ProducerOptions(func(p *streamconfig.Producer) {
p.Logger = logger.Named("TestProducer")
p.Kafka.Debug.CGRP = true
p.Kafka.Debug.Topic = true
})
Expand All @@ -213,6 +206,7 @@ func TestProducerConfig(tb testing.TB, topic string, options ...streamconfig.Opt
}

opts := streamconfig.ProducerOptions(func(p *streamconfig.Producer) {
p.Logger = zaptest.NewLogger(tb).Named("testProducer")
p.Kafka.ID = "testProducer"
p.Kafka.SessionTimeout = 1 * time.Second
p.Kafka.HeartbeatInterval = 150 * time.Millisecond
Expand Down
9 changes: 5 additions & 4 deletions streamconfig/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
"github.com/blendle/go-streamprocessor/streamconfig/kafkaconfig"
"github.com/blendle/go-streamprocessor/streamconfig/pubsubconfig"
"github.com/blendle/go-streamprocessor/streamconfig/standardstreamconfig"
"github.com/blendle/go-streamprocessor/streamutil/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
)

func TestConsumer(t *testing.T) {
Expand All @@ -26,9 +27,9 @@ func TestConsumer(t *testing.T) {
Standardstream: standardstreamconfig.Consumer{},

Global: streamconfig.Global{
Logger: testutil.Logger(t),
HandleInterrupt: false,
Name: "",
Logger: zaptest.NewLogger(t, zaptest.Level(zapcore.ErrorLevel)),
HandleInterrupt: false,
Name: "",
AllowEnvironmentBasedConfiguration: false,
},
}
Expand Down
6 changes: 3 additions & 3 deletions streamconfig/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ type Global struct {

// GlobalDefaults provide a default of global preferences.
var GlobalDefaults = Global{
HandleErrors: true,
HandleInterrupt: true,
Name: "",
HandleErrors: true,
HandleInterrupt: true,
Name: "",
AllowEnvironmentBasedConfiguration: true,
}
9 changes: 5 additions & 4 deletions streamconfig/producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
"github.com/blendle/go-streamprocessor/streamconfig/kafkaconfig"
"github.com/blendle/go-streamprocessor/streamconfig/pubsubconfig"
"github.com/blendle/go-streamprocessor/streamconfig/standardstreamconfig"
"github.com/blendle/go-streamprocessor/streamutil/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
)

func TestProducer(t *testing.T) {
Expand All @@ -26,9 +27,9 @@ func TestProducer(t *testing.T) {
Standardstream: standardstreamconfig.Producer{},

Global: streamconfig.Global{
Logger: testutil.Logger(t),
HandleInterrupt: false,
Name: "",
Logger: zaptest.NewLogger(t, zaptest.Level(zapcore.ErrorLevel)),
HandleInterrupt: false,
Name: "",
AllowEnvironmentBasedConfiguration: false,
},
}
Expand Down
10 changes: 3 additions & 7 deletions streamconfig/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/blendle/go-streamprocessor/streamconfig/kafkaconfig"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
)

func TestTestNewConsumer(t *testing.T) {
Expand All @@ -29,9 +29,7 @@ func TestTestNewConsumer_WithoutDefaults(t *testing.T) {
}

func TestTestNewConsumer_WithOptions(t *testing.T) {
logger, err := zap.NewDevelopment()
require.NoError(t, err)

logger := zaptest.NewLogger(t)
c1 := streamconfig.Consumer{Global: streamconfig.Global{Logger: logger}}
c2 := streamconfig.TestNewConsumer(t, false, streamconfig.Logger(logger))

Expand Down Expand Up @@ -81,9 +79,7 @@ func TestTestNewProducer_WithoutDefaults(t *testing.T) {
}

func TestTestNewProducer_WithOptions(t *testing.T) {
logger, err := zap.NewDevelopment()
require.NoError(t, err)

logger := zaptest.NewLogger(t)
p1 := streamconfig.Producer{Global: streamconfig.Global{Logger: logger}}
p2 := streamconfig.TestNewProducer(t, false, streamconfig.Logger(logger))

Expand Down
7 changes: 7 additions & 0 deletions streamutil/testutil/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,20 @@ func Integration(tb testing.TB) {

// Logger returns a Zap logger instance to use during testing. It returns logs
// in a user-friendly format, reporting anything above warn level.
//
// DEPRECATED: this is now supported by Zap itself, since version `1.9.0`.
//
// See: https://github.com/uber-go/zap/pull/518
//
func Logger(tb testing.TB) *zap.Logger {
cfg := zap.NewDevelopmentConfig()
cfg.Level.SetLevel(zap.ErrorLevel)

log, err := cfg.Build()
require.NoError(tb, err)

log.Error("testing.Logger is deprecated, please use zaptest.NewLogger")

return log
}

Expand Down
5 changes: 4 additions & 1 deletion streamutil/testutil/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/blendle/go-streamprocessor/streamutil/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
)

func TestIntegration_Test(t *testing.T) {
Expand All @@ -33,7 +35,8 @@ func TestExec(t *testing.T) {
}

func TestLogger(t *testing.T) {
assert.Equal(t, "*zap.Logger", reflect.TypeOf(testutil.Logger(t)).String())
assert.Equal(t, "*zap.Logger",
reflect.TypeOf(zaptest.NewLogger(t, zaptest.Level(zapcore.ErrorLevel))).String())
}

func TestVerbose_TEST_DEBUG(t *testing.T) {
Expand Down