github.com/yandex/pandora@v0.5.32/lib/errutil/errutil_test.go (about) 1 package errutil 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "testing" 8 9 pkgerrors "github.com/pkg/errors" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestIscoreutilIsCtxErroror(t *testing.T) { 15 canceledContext, cancel := context.WithCancel(context.Background()) 16 cancel() 17 18 t.Run("nil error", func(t *testing.T) { 19 assert.True(t, IsCtxError(context.Background(), nil)) 20 assert.True(t, IsCtxError(canceledContext, nil)) 21 }) 22 23 t.Run("context error", func(t *testing.T) { 24 assert.False(t, IsCtxError(context.Background(), context.Canceled)) 25 assert.True(t, IsCtxError(canceledContext, context.Canceled)) 26 }) 27 28 t.Run("caused by context error", func(t *testing.T) { 29 assert.False(t, IsCtxError(context.Background(), pkgerrors.Wrap(context.Canceled, "new err"))) 30 assert.True(t, IsCtxError(canceledContext, pkgerrors.Wrap(context.Canceled, "new err"))) 31 }) 32 33 t.Run("default error wrapping has defferent result", func(t *testing.T) { 34 assert.False(t, IsCtxError(context.Background(), fmt.Errorf("new err %w", context.Canceled))) 35 assert.False(t, IsCtxError(canceledContext, fmt.Errorf("new err %w", context.Canceled))) 36 }) 37 38 t.Run("usual error", func(t *testing.T) { 39 err := errors.New("new err") 40 assert.False(t, IsCtxError(canceledContext, err)) 41 assert.False(t, IsCtxError(context.Background(), err)) 42 }) 43 } 44 45 func TestJoin(t *testing.T) { 46 type args struct { 47 } 48 err1 := errors.New("error message") 49 err2 := errors.New("error message 2") 50 tests := []struct { 51 name string 52 err1 error 53 err2 error 54 wantMessage string 55 wantErr error 56 wantNil bool 57 }{ 58 { 59 name: "nil result", 60 err1: nil, 61 err2: nil, 62 wantMessage: "", 63 wantNil: true, 64 }, 65 { 66 name: "first error only", 67 err1: err1, 68 err2: nil, 69 wantMessage: "error message", 70 wantNil: false, 71 }, 72 { 73 name: "second error only", 74 err1: nil, 75 err2: err2, 76 wantMessage: "error message 2", 77 wantNil: false, 78 }, 79 { 80 name: "two errors", 81 err1: err1, 82 err2: err2, 83 wantMessage: "2 errors occurred:\n\t* error message\n\t* error message 2\n\n", 84 wantNil: false, 85 }, 86 } 87 for _, tt := range tests { 88 t.Run(tt.name, func(t *testing.T) { 89 err := Join(tt.err1, tt.err2) 90 if tt.wantNil { 91 require.NoError(t, err) 92 return 93 } 94 require.Equal(t, tt.wantMessage, err.Error()) 95 }) 96 } 97 } 98 99 func TestIsCtxError(t *testing.T) { 100 canceledCtx, cancel := context.WithCancel(context.Background()) 101 cancel() 102 103 tests := []struct { 104 name string 105 err error 106 wantCanceledCtx bool 107 wantNotCanceledCtx bool 108 }{ 109 { 110 name: "nil error", 111 err: nil, 112 wantCanceledCtx: true, 113 wantNotCanceledCtx: true, 114 }, 115 { 116 name: "context error", 117 err: context.Canceled, 118 wantCanceledCtx: true, 119 wantNotCanceledCtx: false, 120 }, 121 { 122 name: "caused by context error", 123 err: pkgerrors.Wrap(context.Canceled, "new err"), 124 wantCanceledCtx: true, 125 wantNotCanceledCtx: false, 126 }, 127 { 128 name: "default error wrapping has defferent result", 129 err: fmt.Errorf("new err 2 %w", context.Canceled), 130 wantCanceledCtx: false, 131 wantNotCanceledCtx: false, 132 }, 133 { 134 name: "usual error", 135 err: errors.New("new err"), 136 wantCanceledCtx: false, 137 wantNotCanceledCtx: false, 138 }, 139 } 140 for _, tt := range tests { 141 t.Run(tt.name, func(t *testing.T) { 142 canceledResult := IsCtxError(canceledCtx, tt.err) 143 require.Equal(t, tt.wantCanceledCtx, canceledResult) 144 145 notCanceledResult := IsCtxError(context.Background(), tt.err) 146 require.Equal(t, tt.wantNotCanceledCtx, notCanceledResult) 147 }) 148 } 149 }