| 26 | } |
| 27 | |
| 28 | func TestPanicErrorUnwrap(t *testing.T) { |
| 29 | t.Parallel() |
| 30 | |
| 31 | testCases := []struct { |
| 32 | name string |
| 33 | panicValue any |
| 34 | wrappedErrorType bool |
| 35 | }{ |
| 36 | { |
| 37 | name: "panicError wraps non-error type", |
| 38 | panicValue: &panicError{value: "string value"}, |
| 39 | wrappedErrorType: false, |
| 40 | }, |
| 41 | { |
| 42 | name: "panicError wraps error type", |
| 43 | panicValue: &panicError{value: new(errValue)}, |
| 44 | wrappedErrorType: false, |
| 45 | }, |
| 46 | } |
| 47 | |
| 48 | for _, tc := range testCases { |
| 49 | tc := tc |
| 50 | |
| 51 | t.Run(tc.name, func(t *testing.T) { |
| 52 | t.Parallel() |
| 53 | |
| 54 | var recovered any |
| 55 | |
| 56 | group := &Group[string, any]{} |
| 57 | |
| 58 | func() { |
| 59 | defer func() { |
| 60 | recovered = recover() |
| 61 | t.Logf("after panic(%#v) in group.Do, recovered %#v", tc.panicValue, recovered) |
| 62 | }() |
| 63 | |
| 64 | _, _, _ = group.Do(tc.name, func() (any, error) { |
| 65 | panic(tc.panicValue) |
| 66 | }) |
| 67 | }() |
| 68 | |
| 69 | if recovered == nil { |
| 70 | t.Fatal("expected a non-nil panic value") |
| 71 | } |
| 72 | |
| 73 | err, ok := recovered.(error) |
| 74 | if !ok { |
| 75 | t.Fatalf("recovered non-error type: %T", recovered) |
| 76 | } |
| 77 | |
| 78 | if !errors.Is(err, new(errValue)) && tc.wrappedErrorType { |
| 79 | t.Errorf("unexpected wrapped error type %T; want %T", err, new(errValue)) |
| 80 | } |
| 81 | }) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestDo(t *testing.T) { |