From 63ca69c5d05f64bd102763ea27ba61b45c677e09 Mon Sep 17 00:00:00 2001 From: Oliver Eikemeier Date: Thu, 3 Jul 2025 21:31:52 +0200 Subject: [PATCH] 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 --- singleflight/singleflight_test.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/singleflight/singleflight_test.go b/singleflight/singleflight_test.go index 853ec42..5a2563b 100644 --- a/singleflight/singleflight_test.go +++ b/singleflight/singleflight_test.go @@ -19,15 +19,11 @@ import ( "time" ) -type errValue struct{} - -func (err *errValue) Error() string { - return "error value" -} - func TestPanicErrorUnwrap(t *testing.T) { t.Parallel() + errValue := errors.New("error value") + testCases := []struct { name string panicValue interface{} @@ -35,19 +31,17 @@ func TestPanicErrorUnwrap(t *testing.T) { }{ { name: "panicError wraps non-error type", - panicValue: &panicError{value: "string value"}, + panicValue: "string value", wrappedErrorType: false, }, { name: "panicError wraps error type", - panicValue: &panicError{value: new(errValue)}, - wrappedErrorType: false, + panicValue: errValue, + wrappedErrorType: true, }, } for _, tc := range testCases { - tc := tc - t.Run(tc.name, func(t *testing.T) { t.Parallel() @@ -75,8 +69,8 @@ func TestPanicErrorUnwrap(t *testing.T) { t.Fatalf("recovered non-error type: %T", recovered) } - if !errors.Is(err, new(errValue)) && tc.wrappedErrorType { - t.Errorf("unexpected wrapped error type %T; want %T", err, new(errValue)) + if !errors.Is(err, errValue) && tc.wrappedErrorType { + t.Errorf("unexpected wrapped error \"%v\"; want \"%v\"", err, errValue) } }) }