-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise_test.go
174 lines (153 loc) · 3.91 KB
/
promise_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2020 Ahmad Sameh(asmsh)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package promise
import (
"context"
"errors"
"testing"
)
// testStrError is an error implementation that's used only for testing.
// it's a string to allow comparing its values.
type testStrError string
func (t testStrError) Error() string {
return string(t)
}
func newStrError() error {
return testStrError("str_test_error")
}
// testPtrError is an error implementation that's used only for testing.
// it's a pointer-based error, to mimick most error structures in real-scenarios.
type testPtrError struct {
txt string
}
func (t *testPtrError) Error() string {
return t.txt
}
func newPtrError() error {
return &testPtrError{txt: "ptr_test_error"}
}
func TestPanicking(t *testing.T) {
panicValue := "test_panic"
t.Run("callback handling", func(t *testing.T) {
defer func() {
v := recover()
if v != nil {
t.Fatalf("got unexpected panic: %v", v)
}
}()
p := Go(func() {
panic(panicValue)
}).Recover(func(ctx context.Context, res Result[any]) Result[any] {
return nil
})
p.Wait()
})
t.Run("Res handling", func(t *testing.T) {
defer func() {
v := recover()
if v != nil {
t.Fatalf("got unexpected panic: %v", v)
}
}()
p := Go(func() {
panic(panicValue)
})
res := p.Res()
if res == nil {
t.Fatalf("Res() = %v, want: non-nil", res)
}
if perr := new(PanicError); !errors.As(res.Err(), perr) || perr.V != panicValue {
t.Fatalf("Res() got unexpected error: %v", res.Err())
}
})
t.Run("non-recovered panic 1", func(t *testing.T) {
defer func() {
v := recover()
if v == nil {
t.Fatal("expected a panic, but none happened")
}
if _, ok := v.(error); !ok {
t.Fatalf("got unexpected panic: %v", v)
}
verr := v.(error)
if perr := new(PanicError); !errors.As(verr, perr) || perr.V != panicValue {
t.Fatalf("got unexpected panic: %v", v)
}
}()
p := Go(func() {
panic(panicValue)
})
p.Wait()
})
t.Run("non-recovered panic 2", func(t *testing.T) {
defer func() {
v := recover()
if v == nil {
t.Fatal("expected a panic, but none happened")
}
if _, ok := v.(error); !ok {
t.Fatalf("got unexpected panic: %v", v)
}
verr := v.(error)
if perr := new(PanicError); !errors.As(verr, perr) || perr.V != panicValue {
t.Fatalf("got unexpected panic: %v", v)
}
}()
p := Go(func() {
panic(panicValue)
}).Then(func(ctx context.Context, res Result[any]) Result[any] {
return nil
})
p.Wait()
})
}
func TestRejection(t *testing.T) {
t.Run("callback handling", func(t *testing.T) {
defer func() {
v := recover()
if v != nil {
t.Fatalf("got unexpected panic: %v", v)
}
}()
p := GoRes(func(ctx context.Context) Result[any] {
return Err[any](newStrError())
}).Catch(func(ctx context.Context, res Result[any]) Result[any] {
// handle the error...
return nil
})
p.Wait()
})
t.Run("Res handling", func(t *testing.T) {
wantErr := newStrError()
defer func() {
v := recover()
if v != nil {
t.Fatalf("got unexpected panic: %v", v)
}
}()
p := GoRes(func(ctx context.Context) Result[any] {
return Err[any](wantErr)
})
res := p.Res()
if res == nil {
t.Errorf("Res() = nil, want: non-nil")
}
if res.Err() == nil {
t.Errorf("Res().Err() = nil, want: non-nil")
}
if err := res.Err(); !errors.Is(err, wantErr) {
t.Errorf("Res().Err() = %v, want: %v", err, wantErr)
}
})
}