-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr_runner_test.go
132 lines (104 loc) · 2.65 KB
/
err_runner_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
package kytsya
import (
"errors"
"sync/atomic"
"testing"
"time"
)
var (
testError = errors.New("houston, we have a problem")
)
func TestErroutineOk(t *testing.T) {
var counter uint32
err := Erroutine[struct{}]().Spawn(func() Result[struct{}] {
atomic.AddUint32(&counter, 1)
return Result[struct{}]{}
}).Wait()
if err.Err != nil {
t.Fatal("expect nil error")
}
if atomic.LoadUint32(&counter) != 1 {
t.Fatal("expect incremented counter")
}
}
func TestErroutineError(t *testing.T) {
var counter uint32
err := Erroutine[struct{}]().Spawn(func() Result[struct{}] {
atomic.AddUint32(&counter, 1)
return Result[struct{}]{
Err: testError,
}
}).Wait()
if err.Err == nil {
t.Fatal("expect not nil error")
}
if err.Err.Error() != testError.Error() {
t.Fatal("expect test error in response")
}
if atomic.LoadUint32(&counter) != 1 {
t.Fatal("expect incremented counter")
}
}
func TestErroutineWithRecover(t *testing.T) {
err := Erroutine[struct{}]().WithRecover().Spawn(func() Result[struct{}] {
panic(testError)
}).Wait()
if err.Err == nil {
t.Fatal("expect non nil error")
}
if !errors.Is(err.Err, ErrRecoveredFromPanic) {
t.Fatal("expect recovered test error")
}
}
func TestErroutineWithTimeout(t *testing.T) {
err := Erroutine[struct{}]().WithTimeout(time.Second).Spawn(func() Result[struct{}] {
time.Sleep(time.Second * 2)
return Result[struct{}]{}
}).Wait()
if err.Err == nil {
t.Fatal("expect non nil error")
}
if !errors.Is(err.Err, ErrTimeout) {
t.Fatal("expect timeout error")
}
}
func TestWaitAsync(t *testing.T) {
resCh := Erroutine[uint32]().Spawn(func() Result[uint32] {
return Result[uint32]{Data: 1}
}).WaitAsync()
res := <-resCh
if res.Data != 1 {
t.Fatal("expected async data got from channel")
}
}
func TestWaitAsync_Timeout(t *testing.T) {
resCh := Erroutine[uint32]().WithTimeout(time.Second).
Spawn(func() Result[uint32] {
time.Sleep(time.Second * 2)
return Result[uint32]{Data: 1}
}).WaitAsync()
res := <-resCh
if !errors.Is(res.Err, ErrTimeout) {
t.Fatal("goroutine expect timeout")
}
}
func TestWaitAsync_Timeout_OK(t *testing.T) {
resCh := Erroutine[uint32]().WithTimeout(time.Second).
Spawn(func() Result[uint32] {
time.Sleep(time.Millisecond * 50)
return Result[uint32]{Data: 1}
}).WaitAsync()
res := <-resCh
if res.Data != 1 {
t.Fatal("expected async data got from channel")
}
}
func TestWaitWithErrorWithTimeout(t *testing.T) {
err := errors.New("something went wrong")
res := Erroutine[int]().WithTimeout(time.Second).Spawn(func() Result[int] {
return Result[int]{Err: err}
}).Wait()
if !errors.Is(res.Err, err) {
t.Fatal("expect an error")
}
}