Skip to content

Commit 63ca69c

Browse files
committed
singleflight: Fix TestPanicErrorUnwrap
TestPanicErrorUnwrap does not test what it seems to test - "wrappedErrorType" is always false (should be true in the second case) - The values are already wrapped in the private error (they shouldn't) - "errors.Is(err, new(T))" is undefined for zero-sized "T"s (and false for non-zero-sized). - "tc := tc" is not needed since Go 1.22 Fixes: - Use a (local) sentinel value instead of a type, as in TestDoErr. - Replace all "new(errValue)" by direct references to the unique sentinel "errValue". - Replace diagnostic, since the error type we get is internal, so we can't directly expect that - it should only wrap our sentinel. Signed-off-by: Oliver Eikemeier <[email protected]>
1 parent 7fad2c9 commit 63ca69c

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

singleflight/singleflight_test.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,29 @@ import (
1919
"time"
2020
)
2121

22-
type errValue struct{}
23-
24-
func (err *errValue) Error() string {
25-
return "error value"
26-
}
27-
2822
func TestPanicErrorUnwrap(t *testing.T) {
2923
t.Parallel()
3024

25+
errValue := errors.New("error value")
26+
3127
testCases := []struct {
3228
name string
3329
panicValue interface{}
3430
wrappedErrorType bool
3531
}{
3632
{
3733
name: "panicError wraps non-error type",
38-
panicValue: &panicError{value: "string value"},
34+
panicValue: "string value",
3935
wrappedErrorType: false,
4036
},
4137
{
4238
name: "panicError wraps error type",
43-
panicValue: &panicError{value: new(errValue)},
44-
wrappedErrorType: false,
39+
panicValue: errValue,
40+
wrappedErrorType: true,
4541
},
4642
}
4743

4844
for _, tc := range testCases {
49-
tc := tc
50-
5145
t.Run(tc.name, func(t *testing.T) {
5246
t.Parallel()
5347

@@ -75,8 +69,8 @@ func TestPanicErrorUnwrap(t *testing.T) {
7569
t.Fatalf("recovered non-error type: %T", recovered)
7670
}
7771

78-
if !errors.Is(err, new(errValue)) && tc.wrappedErrorType {
79-
t.Errorf("unexpected wrapped error type %T; want %T", err, new(errValue))
72+
if !errors.Is(err, errValue) && tc.wrappedErrorType {
73+
t.Errorf("unexpected wrapped error \"%v\"; want \"%v\"", err, errValue)
8074
}
8175
})
8276
}

0 commit comments

Comments
 (0)