-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuture_test.go
More file actions
124 lines (106 loc) · 2.61 KB
/
future_test.go
File metadata and controls
124 lines (106 loc) · 2.61 KB
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
package svcinit
import (
"context"
"errors"
"testing"
"testing/synctest"
"time"
"gotest.tools/v3/assert"
)
func TestFuture_alreadyResolved(t *testing.T) {
// When an already-resolved Future is resolved it should panic with ErrAlreadyResolved.
defer func() {
if recover() != ErrAlreadyResolved {
t.Fatal("expected Future to panic with ErrAlreadyResolved")
}
}()
fut := NewFuture[string]()
fut.Resolve("Hello World!")
fut.Resolve("Hello World!")
}
func TestFuture_notResolved(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
fut := NewFuture[string]()
_, err := fut.Value(WithoutFutureWait())
assert.ErrorIs(t, err, ErrNotResolved)
})
}
func TestFuture_Value(t *testing.T) {
for name, tt := range map[string]struct {
v string
err error
}{
"with value": {"Hello World!", nil},
"with error": {"", errors.New("error")},
} {
t.Run(name, func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
fut := NewFuture[string]()
if tt.err != nil {
fut.ResolveError(tt.err)
} else {
fut.Resolve(tt.v)
}
v, err := fut.Value()
assert.Equal(t, tt.err, err)
assert.Equal(t, tt.v, v)
})
})
}
}
func TestFuture_ValueCtx(t *testing.T) {
for name, tt := range map[string]struct {
v string
err error
}{
"with value": {"Hello World!", nil},
"with error": {"", errors.New("error")},
} {
t.Run(name, func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
fut := NewFuture[string]()
if tt.err != nil {
fut.ResolveError(tt.err)
} else {
fut.Resolve(tt.v)
}
v, err := fut.Value(WithFutureCtx(context.Background()))
assert.Equal(t, tt.err, err)
assert.Equal(t, tt.v, v)
})
})
}
t.Run("with canceled context", func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
timeout := time.After(time.Second)
done := make(chan bool)
go func() {
ctx, cancel := context.WithCancel(context.Background())
cancel()
fut := NewFuture[string]()
_, err := fut.Value(WithFutureCtx(ctx))
assert.ErrorIs(t, err, context.Canceled)
done <- true
}()
select {
case <-timeout:
t.Fatal("ValueCtx() future should not have blocked")
case <-done:
}
})
})
}
func TestLatch_alreadyResolved(t *testing.T) {
// When an already-resolved Latch is resolved it should panic with ErrAlreadyResolved.
defer func() {
if recover() != ErrAlreadyResolved {
t.Fatal("expected latch to panic with ErrAlreadyResolved")
}
}()
var counter int
inc := func() { counter++ }
l := new(latch)
l.resolve(inc)
l.resolve(inc)
assert.Equal(t, 1, counter, "expect resolve func to be called at most once")
}