This repository has been archived by the owner on Jan 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise_test.go
122 lines (119 loc) · 2.72 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
package xgraph
import (
"errors"
"sync"
"testing"
"time"
)
func timeout() func() {
finch := make(chan struct{})
go func() {
t := time.NewTimer(time.Second * 10)
select {
case <-t.C:
panic(errors.New("timeout"))
case <-finch:
}
}()
return func() { finch <- struct{}{} }
}
func TestPromise(t *testing.T) {
tests := []testCase{
{
Name: "basic",
Func: func() (bool, error) {
defer timeout()()
var run bool
var e error
NewPromise(func(s FinishHandler, f FailHandler) {
s()
}).Then(func() { run = true }, func(err error) { e = err })
return run, e
},
Expect: []interface{}{true, nil},
},
{
Name: "basic-error",
Func: func() (bool, error) {
defer timeout()()
var run bool
var e error
NewPromise(func(s FinishHandler, f FailHandler) {
f(errors.New("this is an error"))
}).Then(func() { run = true }, func(err error) { e = err })
return run, e
},
Expect: []interface{}{false, errors.New("this is an error")},
},
{
Name: "cache",
Func: func() (bool, bool, error, error) {
defer timeout()()
var run1, run2 bool
var e1, e2 error
p := NewPromise(func(s FinishHandler, f FailHandler) {
f(errors.New("this is an error"))
})
p.Then(func() { run1 = true }, func(err error) { e1 = err })
p.Then(func() { run2 = true }, func(err error) { e2 = err })
return run1, run2, e1, e2
},
Expect: []interface{}{false, false, errors.New("this is an error"), errors.New("this is an error")},
},
{
Name: "cache-error",
Func: func() (bool, bool, error, error) {
defer timeout()()
var run1, run2 bool
var e1, e2 error
p := NewPromise(func(s FinishHandler, f FailHandler) {
s()
})
p.Then(func() { run1 = true }, func(err error) { e1 = err })
p.Then(func() { run2 = true }, func(err error) { e2 = err })
return run1, run2, e1, e2
},
Expect: []interface{}{true, true, nil, nil},
},
{
Name: "cache-single-run",
Func: func() int {
defer timeout()()
runs := 0
p := NewPromise(func(s FinishHandler, f FailHandler) {
runs++
s()
})
p.Then(func() {}, func(err error) {})
p.Then(func() {}, func(err error) {})
return runs
},
Expect: []interface{}{1},
},
{
Name: "concurrent-single-start",
Func: func() int {
defer timeout()()
runs := 0
var lck sync.Mutex
lck.Lock()
p := NewPromise(func(s FinishHandler, f FailHandler) {
runs++
go func() {
lck.Lock()
defer lck.Unlock()
s()
}()
})
p.Then(func() {}, func(err error) {})
p.Then(func() {}, func(err error) {})
lck.Unlock()
return runs
},
Expect: []interface{}{1},
},
}
for _, tv := range tests {
tv.genTest(t)
}
}