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

lint: Enable paralleltest, fix issues #1367

Merged
merged 2 commits into from
Oct 1, 2023
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
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ linters:

# Our own extras:
- gofumpt
- nolintlint # lints nolint directives
- nolintlint # lints nolint directives
- paralleltest # requires t.Parallel on all tests
- revive

linters-settings:
Expand Down
17 changes: 12 additions & 5 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func BenchmarkBoolsReflect(b *testing.B) {
}

func TestArrayWrappers(t *testing.T) {
t.Parallel()

tests := []struct {
desc string
field Field
Expand Down Expand Up @@ -101,11 +103,16 @@ func TestArrayWrappers(t *testing.T) {
}

for _, tt := range tests {
enc := zapcore.NewMapObjectEncoder()
tt.field.Key = "k"
tt.field.AddTo(enc)
assert.Equal(t, tt.expected, enc.Fields["k"], "%s: unexpected map contents.", tt.desc)
assert.Equal(t, 1, len(enc.Fields), "%s: found extra keys in map: %v", tt.desc, enc.Fields)
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()

enc := zapcore.NewMapObjectEncoder()
tt.field.Key = "k"
tt.field.AddTo(enc)
assert.Equal(t, tt.expected, enc.Fields["k"], "unexpected map contents")
assert.Equal(t, 1, len(enc.Fields), "found extra keys in map: %v", enc.Fields)
})
}
}

Expand Down
86 changes: 70 additions & 16 deletions buffer/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,86 @@ import (
)

func TestBufferWrites(t *testing.T) {
buf := NewPool().Get()
t.Parallel()

tests := []struct {
desc string
f func()
f func(*Buffer)
want string
}{
{"AppendByte", func() { buf.AppendByte('v') }, "v"},
{"AppendString", func() { buf.AppendString("foo") }, "foo"},
{"AppendIntPositive", func() { buf.AppendInt(42) }, "42"},
{"AppendIntNegative", func() { buf.AppendInt(-42) }, "-42"},
{"AppendUint", func() { buf.AppendUint(42) }, "42"},
{"AppendBool", func() { buf.AppendBool(true) }, "true"},
{"AppendFloat64", func() { buf.AppendFloat(3.14, 64) }, "3.14"},
{
desc: "AppendByte",
f: func(buf *Buffer) { buf.AppendByte('v') },
want: "v",
},
{
desc: "AppendString",
f: func(buf *Buffer) { buf.AppendString("foo") },
want: "foo",
},
{
desc: "AppendIntPositive",
f: func(buf *Buffer) { buf.AppendInt(42) },
want: "42",
},
{
desc: "AppendIntNegative",
f: func(buf *Buffer) { buf.AppendInt(-42) },
want: "-42",
},
{
desc: "AppendUint",
f: func(buf *Buffer) { buf.AppendUint(42) },
want: "42",
},
{
desc: "AppendBool",
f: func(buf *Buffer) { buf.AppendBool(true) },
want: "true",
},
{
desc: "AppendFloat64",
f: func(buf *Buffer) { buf.AppendFloat(3.14, 64) },
want: "3.14",
},
// Intentionally introduce some floating-point error.
{"AppendFloat32", func() { buf.AppendFloat(float64(float32(3.14)), 32) }, "3.14"},
{"AppendWrite", func() { buf.Write([]byte("foo")) }, "foo"},
{"AppendTime", func() { buf.AppendTime(time.Date(2000, 1, 2, 3, 4, 5, 6, time.UTC), time.RFC3339) }, "2000-01-02T03:04:05Z"},
{"WriteByte", func() { buf.WriteByte('v') }, "v"},
{"WriteString", func() { buf.WriteString("foo") }, "foo"},
{
desc: "AppendFloat32",
f: func(buf *Buffer) { buf.AppendFloat(float64(float32(3.14)), 32) },
want: "3.14",
},
{
desc: "AppendWrite",
f: func(buf *Buffer) { buf.Write([]byte("foo")) },
want: "foo",
},
{
desc: "AppendTime",
f: func(buf *Buffer) { buf.AppendTime(time.Date(2000, 1, 2, 3, 4, 5, 6, time.UTC), time.RFC3339) },
want: "2000-01-02T03:04:05Z",
},
{
desc: "WriteByte",
f: func(buf *Buffer) { buf.WriteByte('v') },
want: "v",
},
{
desc: "WriteString",
f: func(buf *Buffer) { buf.WriteString("foo") },
want: "foo",
},
}

pool := NewPool()
for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
buf.Reset()
tt.f()
t.Parallel()

buf := pool.Get()
defer buf.Free()

tt.f(buf)
assert.Equal(t, tt.want, buf.String(), "Unexpected buffer.String().")
assert.Equal(t, tt.want, string(buf.Bytes()), "Unexpected string(buffer.Bytes()).")
assert.Equal(t, len(tt.want), buf.Len(), "Unexpected buffer length.")
Expand Down
2 changes: 2 additions & 0 deletions buffer/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
)

func TestBuffers(t *testing.T) {
t.Parallel()

const dummyData = "dummy data"
p := NewPool()

Expand Down
2 changes: 2 additions & 0 deletions clock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func (c constantClock) NewTicker(d time.Duration) *time.Ticker {
}

func TestWithClock(t *testing.T) {
t.Parallel()

date := time.Date(2077, 1, 23, 10, 15, 13, 441, time.UTC)
clock := constantClock(date)
withLogger(t, DebugLevel, []Option{WithClock(clock)}, func(log *Logger, logs *observer.ObservedLogs) {
Expand Down
17 changes: 17 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
)

func TestConfig(t *testing.T) {
t.Parallel()

tests := []struct {
desc string
cfg Config
Expand All @@ -57,7 +59,10 @@ func TestConfig(t *testing.T) {
}

for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()

logOut := filepath.Join(t.TempDir(), "test.log")

tt.cfg.OutputPaths = []string{logOut}
Expand Down Expand Up @@ -86,6 +91,8 @@ func TestConfig(t *testing.T) {
}

func TestConfigWithInvalidPaths(t *testing.T) {
t.Parallel()

tests := []struct {
desc string
output string
Expand All @@ -97,7 +104,10 @@ func TestConfigWithInvalidPaths(t *testing.T) {
}

for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()

cfg := NewProductionConfig()
cfg.OutputPaths = []string{tt.output}
cfg.ErrorOutputPaths = []string{tt.errOutput}
Expand All @@ -108,6 +118,8 @@ func TestConfigWithInvalidPaths(t *testing.T) {
}

func TestConfigWithMissingAttributes(t *testing.T) {
t.Parallel()

tests := []struct {
desc string
cfg Config
Expand Down Expand Up @@ -135,7 +147,10 @@ func TestConfigWithMissingAttributes(t *testing.T) {
}

for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()

cfg := tt.cfg
_, err := cfg.Build()
assert.EqualError(t, err, tt.expectErr)
Expand All @@ -159,6 +174,8 @@ func makeSamplerCountingHook() (h func(zapcore.Entry, zapcore.SamplingDecision),
}

func TestConfigWithSamplingHook(t *testing.T) {
t.Parallel()

shook, dcount, scount := makeSamplerCountingHook()
cfg := Config{
Level: NewAtomicLevelAt(InfoLevel),
Expand Down
11 changes: 11 additions & 0 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,20 @@ import (
)

func TestRegisterDefaultEncoders(t *testing.T) {
t.Parallel()

testEncodersRegistered(t, "console", "json")
}

//nolint:paralleltest // modifies global registry
func TestRegisterEncoder(t *testing.T) {
testEncoders(func() {
assert.NoError(t, RegisterEncoder("foo", newNilEncoder), "expected to be able to register the encoder foo")
testEncodersRegistered(t, "foo")
})
}

//nolint:paralleltest // modifies global registry
func TestDuplicateRegisterEncoder(t *testing.T) {
testEncoders(func() {
assert.NoError(t, RegisterEncoder("foo", newNilEncoder), "expected to be able to register the encoder foo")
Expand All @@ -47,9 +51,12 @@ func TestDuplicateRegisterEncoder(t *testing.T) {
}

func TestRegisterEncoderNoName(t *testing.T) {
t.Parallel()

assert.Equal(t, errNoEncoderNameSpecified, RegisterEncoder("", newNilEncoder), "expected an error when registering an encoder with no name")
}

//nolint:paralleltest // modifies global registry
func TestNewEncoder(t *testing.T) {
testEncoders(func() {
assert.NoError(t, RegisterEncoder("foo", newNilEncoder), "expected to be able to register the encoder foo")
Expand All @@ -60,11 +67,15 @@ func TestNewEncoder(t *testing.T) {
}

func TestNewEncoderNotRegistered(t *testing.T) {
t.Parallel()

_, err := newEncoder("foo", zapcore.EncoderConfig{})
assert.Error(t, err, "expected an error when trying to create an encoder of an unregistered name")
}

func TestNewEncoderNoName(t *testing.T) {
t.Parallel()

_, err := newEncoder("", zapcore.EncoderConfig{})
assert.Equal(t, errNoEncoderNameSpecified, err, "expected an error when creating an encoder with no name")
}
Expand Down
34 changes: 25 additions & 9 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
)

func TestErrorConstructors(t *testing.T) {
t.Parallel()

fail := errors.New("fail")

tests := []struct {
Expand All @@ -48,14 +50,21 @@ func TestErrorConstructors(t *testing.T) {
}

for _, tt := range tests {
if !assert.Equal(t, tt.expect, tt.field, "Unexpected output from convenience field constructor %s.", tt.name) {
t.Logf("type expected: %T\nGot: %T", tt.expect.Interface, tt.field.Interface)
}
assertCanBeReused(t, tt.field)
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

if !assert.Equal(t, tt.expect, tt.field, "Unexpected output from convenience field constructor %s.", tt.name) {
t.Logf("type expected: %T\nGot: %T", tt.expect.Interface, tt.field.Interface)
}
assertCanBeReused(t, tt.field)
})
}
}

func TestErrorArrayConstructor(t *testing.T) {
t.Parallel()

tests := []struct {
desc string
field Field
Expand All @@ -70,15 +79,22 @@ func TestErrorArrayConstructor(t *testing.T) {
}

for _, tt := range tests {
enc := zapcore.NewMapObjectEncoder()
tt.field.Key = "k"
tt.field.AddTo(enc)
assert.Equal(t, tt.expected, enc.Fields["k"], "%s: unexpected map contents.", tt.desc)
assert.Equal(t, 1, len(enc.Fields), "%s: found extra keys in map: %v", tt.desc, enc.Fields)
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()

enc := zapcore.NewMapObjectEncoder()
tt.field.Key = "k"
tt.field.AddTo(enc)
assert.Equal(t, tt.expected, enc.Fields["k"], "%s: unexpected map contents.", tt.desc)
assert.Equal(t, 1, len(enc.Fields), "%s: found extra keys in map: %v", tt.desc, enc.Fields)
})
}
}

func TestErrorsArraysHandleRichErrors(t *testing.T) {
t.Parallel()

errs := []error{fmt.Errorf("egad")}

enc := zapcore.NewMapObjectEncoder()
Expand Down
15 changes: 11 additions & 4 deletions exp/zapfield/zapfield_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type (
)

func TestFieldConstructors(t *testing.T) {
t.Parallel()

var (
key = MyKey("test key")
value = MyValue("test value")
Expand All @@ -55,10 +57,15 @@ func TestFieldConstructors(t *testing.T) {
}

for _, tt := range tests {
if !assert.Equal(t, tt.expect, tt.field, "Unexpected output from convenience field constructor %s.", tt.name) {
t.Logf("type expected: %T\nGot: %T", tt.expect.Interface, tt.field.Interface)
}
assertCanBeReused(t, tt.field)
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

if !assert.Equal(t, tt.expect, tt.field, "Unexpected output from convenience field constructor %s.", tt.name) {
t.Logf("type expected: %T\nGot: %T", tt.expect.Interface, tt.field.Interface)
}
assertCanBeReused(t, tt.field)
})
}
}

Expand Down
Loading