Skip to content

Commit dc92f86

Browse files
committed
slog: fix race failures
The race detector messes with tests that measure allocations, so disable those tests when it is running. Fixes golang/go#54978. Change-Id: I5e8e720f6a91f11257bcbb267927a236b2dfdafc Reviewed-on: https://go-review.googlesource.com/c/exp/+/430075 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]>
1 parent 5c715a9 commit dc92f86

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

slog/attr_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,24 @@ func TestAttrNoAlloc(t *testing.T) {
103103
_ = x
104104
}
105105

106-
func TestAnyLevel(t *testing.T) {
106+
func TestAnyLevelAlloc(t *testing.T) {
107107
// Because typical Levels are small integers,
108108
// they are zero-alloc.
109109
var a Attr
110110
x := DebugLevel + 100
111111
wantAllocs(t, 0, func() { a = Any("k", x) })
112+
_ = a
113+
}
114+
115+
func TestAnyLevel(t *testing.T) {
116+
x := DebugLevel + 100
117+
a := Any("k", x)
112118
v := a.Value()
113119
if _, ok := v.(Level); !ok {
114120
t.Errorf("wanted Level, got %T", v)
115121
}
116122
}
117123

118-
func wantAllocs(t *testing.T, want int, f func()) {
119-
t.Helper()
120-
got := int(testing.AllocsPerRun(5, f))
121-
if got != want {
122-
t.Errorf("got %d allocs, want %d", got, want)
123-
}
124-
}
125-
126124
//////////////// Benchmark for accessing Attr values
127125

128126
// The "As" form is the slowest.

slog/norace_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build !race
6+
7+
package slog
8+
9+
import "testing"
10+
11+
func wantAllocs(t *testing.T, want int, f func()) {
12+
t.Helper()
13+
got := int(testing.AllocsPerRun(5, f))
14+
if got != want {
15+
t.Errorf("got %d allocs, want %d", got, want)
16+
}
17+
}

slog/race_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build race
6+
7+
package slog
8+
9+
import "testing"
10+
11+
func wantAllocs(t *testing.T, want int, f func()) {
12+
t.Log("skipping allocation tests with race detector")
13+
}

0 commit comments

Comments
 (0)