-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpromise_test.go
249 lines (238 loc) · 5.69 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright (c) 2022, [email protected]
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package genfuncs_test
import (
"context"
"fmt"
"github.com/nwillc/genfuncs"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestPromise_Wait(t *testing.T) {
type args struct {
action func(context.Context) *genfuncs.Result[int]
}
tests := []struct {
name string
args args
want int
wantOk bool
wantErrorMsg string
}{
{
name: "nil action",
args: args{},
wantOk: false,
wantErrorMsg: genfuncs.PromiseNoActionErrorMsg,
},
{
name: "deliver action",
args: args{
action: func(_ context.Context) *genfuncs.Result[int] {
return genfuncs.NewResult(1)
},
},
wantOk: true,
want: 1,
},
{
name: "deliver slow action",
args: args{
action: func(_ context.Context) *genfuncs.Result[int] {
time.Sleep(400 * time.Millisecond)
return genfuncs.NewResult(2)
},
},
wantOk: true,
want: 2,
},
{
name: "deliver error",
args: args{
action: func(_ context.Context) *genfuncs.Result[int] {
return genfuncs.NewError[int](genfuncs.NoSuchElement)
},
},
wantOk: false,
wantErrorMsg: genfuncs.NoSuchElement.Error(),
},
{
name: "action panic message",
args: args{
action: func(_ context.Context) *genfuncs.Result[int] {
panic("sky is falling")
},
},
wantOk: false,
wantErrorMsg: genfuncs.PromisePanicErrorMsg,
},
{
name: "action panic error",
args: args{
action: func(_ context.Context) *genfuncs.Result[int] {
panic(genfuncs.NoSuchElement)
},
},
wantOk: false,
wantErrorMsg: genfuncs.NoSuchElement.Error(),
},
{
name: "action panic nil",
args: args{
action: func(_ context.Context) *genfuncs.Result[int] {
panic(nil)
},
},
wantOk: false,
wantErrorMsg: genfuncs.PromisePanicErrorMsg,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
future := genfuncs.NewPromise[int](context.Background(), tt.args.action)
result := future.Wait()
assert.Equal(t, tt.wantOk, result.Ok())
if !tt.wantOk {
assert.Contains(t, result.Error().Error(), tt.wantErrorMsg)
return
}
assert.Equal(t, tt.want, result.OrEmpty())
})
}
}
func TestPromise_OnError(t *testing.T) {
var errorCount int
errorAdd := func(err error) { errorCount++ }
type args struct {
aPanic bool
f1 *genfuncs.Result[int]
}
tests := []struct {
name string
args args
wantOk bool
errorCount int
errorMsg string
}{
{
name: "no error",
args: args{
f1: genfuncs.NewResult(1),
},
wantOk: true,
errorCount: 0,
},
{
name: "error",
args: args{
f1: genfuncs.NewError[int](genfuncs.NoSuchElement),
},
wantOk: false,
errorCount: 1,
errorMsg: genfuncs.NoSuchElement.Error(),
},
{
name: "panic",
args: args{
aPanic: true,
},
wantOk: false,
errorCount: 1,
errorMsg: genfuncs.PromisePanicErrorMsg,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
errorCount = 0
p1 := genfuncs.NewPromise(context.Background(), func(_ context.Context) *genfuncs.Result[int] {
if tt.args.aPanic {
panic(tt.name)
}
return tt.args.f1
})
p2 := p1.OnError(errorAdd)
result := p2.Wait()
assert.Equal(t, tt.wantOk, result.Ok())
assert.Equal(t, tt.errorCount, errorCount)
if !tt.wantOk {
assert.Contains(t, result.Error().Error(), tt.errorMsg)
}
})
}
}
func TestPromise_OnSuccess_OnError(t *testing.T) {
count := 0
p := genfuncs.NewPromise[bool](
context.Background(),
func(_ context.Context) *genfuncs.Result[bool] {
return genfuncs.NewResult(true)
}).
OnSuccess(func(_ bool) {
count++
}).
OnError(func(e error) {
count++
})
r := p.Wait()
assert.True(t, r.Ok())
assert.True(t, r.OrEmpty())
assert.Equal(t, 1, count)
}
func TestPromise_MultiWait(t *testing.T) {
count := 0
p := genfuncs.NewPromise[bool](
context.Background(),
func(_ context.Context) *genfuncs.Result[bool] {
return genfuncs.NewResult(true)
}).
OnSuccess(func(_ bool) {
count++
})
r := p.Wait()
assert.True(t, r.Ok())
assert.True(t, r.OrEmpty())
assert.Equal(t, 1, count)
// Safe to call wait again.
r = p.Wait()
assert.True(t, r.Ok())
assert.True(t, r.OrEmpty())
assert.Equal(t, 1, count)
}
func TestPromise_Cancel(t *testing.T) {
waitForCancel := 500 * time.Millisecond
cancelAction := func(ctx context.Context) *genfuncs.Result[bool] {
select {
case <-ctx.Done():
return genfuncs.NewError[bool](fmt.Errorf("cancelled"))
case <-time.After(waitForCancel):
return genfuncs.NewResult(true)
}
}
ctx := context.Background()
p := genfuncs.NewPromise(ctx, cancelAction)
r := p.Wait()
assert.True(t, r.Ok())
ctx = context.Background()
p = genfuncs.NewPromise(ctx, cancelAction)
go func() {
time.Sleep(waitForCancel / 2)
p.Cancel()
}()
r = p.Wait()
assert.False(t, r.Ok())
assert.Contains(t, r.Error().Error(), "cancel")
}