-
Notifications
You must be signed in to change notification settings - Fork 0
/
future_test.go
executable file
·131 lines (101 loc) · 2.12 KB
/
future_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
package eventual2go
import (
"errors"
"testing"
"time"
)
func TestFutureBasicCompletion(t *testing.T) {
cp := NewCompleter[bool]()
f := cp.Future()
if f.Completed() {
t.Error("Complete to early")
}
c := f.AsChan()
cp.Complete(true)
//
if !f.Completed() {
t.Error("not completed")
}
if !(<-c) {
t.Error("Completed with wrong args")
}
if !f.Result(){
t.Error("Completed with wrong args")
}
}
func TestTimeoutCompletion(t *testing.T) {
cp := NewTimeoutCompleter[bool](1 * time.Microsecond)
f := cp.Future()
time.Sleep(10 * time.Millisecond)
if !f.Completed() {
t.Error("Timeout didnt complete")
}
c := make(chan error, 1)
defer close(c)
f.Err(testcompletererr(c))
if <-c != ErrTimeout {
t.Error("Completed with wrong error")
}
}
func TestFutureChainCompletion(t *testing.T) {
cp := NewCompleter[bool]()
f := cp.Future()
c := f.AsChan()
cp.Complete(true)
if !f.Completed() {
t.Error("not completed")
}
if !(<-c) {
t.Error("Completed with wrong args")
}
}
func TestFutureErrCompletion(t *testing.T) {
cp := NewCompleter[bool]()
f := cp.Future()
c1 := make(chan error, 1)
defer close(c1)
f.Err(testcompletererr(c1))
cp.CompleteError(errors.New("testerror"))
if !f.Completed() {
t.Error("not completed")
}
c2 := make(chan error, 1)
defer close(c2)
f.Err(testcompletererr(c2))
if (<-c1).Error() != "testerror" {
t.Error("Completed with wrong err")
}
if (<-c2).Error() != "testerror" {
t.Error("Completed with wrong err")
}
}
func TestFutureMultiCompletion(t *testing.T) {
cp := NewCompleter[Data]()
f := cp.Future()
c1 := make(chan interface{})
defer close(c1)
f.Then(testcompleter(c1))
c2 := make(chan interface{})
defer close(c2)
f.Then(testcompleter(c2))
if len(f.fcs) != 2 {
t.Fatal("fcs lenth not 2, is", len(f.fcs))
}
cp.Complete(true)
if !(<-c1).(bool) {
t.Error("Completed with wrong args")
}
if !(<-c2).(bool) {
t.Error("Completed with wrong args")
}
}
func testcompleter(c chan interface{}) CompletionHandler[Data] {
return func(d Data) {
c <- d
}
}
func testcompletererr(c chan error) ErrorHandler {
return func(e error) {
c <- e
}
}