-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfutures_test.go
More file actions
184 lines (171 loc) · 4.81 KB
/
Copy pathfutures_test.go
File metadata and controls
184 lines (171 loc) · 4.81 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
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
package resonate
import (
"errors"
"strings"
"testing"
)
func TestFuture_Remote_Await_Resolved(t *testing.T) {
ctx := testContext("root", nil)
rec := resolvedPromise(t, "p-1", 42)
f := &Future{id: "p-1", ctx: ctx, kind: futureRemote, record: &rec}
var got int
if err := f.Await(&got); err != nil {
t.Fatal(err)
}
if got != 42 {
t.Fatalf("expected 42, got %d", got)
}
}
func TestFuture_Remote_Await_Resolved_DiscardInto(t *testing.T) {
ctx := testContext("root", nil)
rec := resolvedPromise(t, "p-1", 42)
f := &Future{id: "p-1", ctx: ctx, kind: futureRemote, record: &rec}
if err := f.Await(nil); err != nil {
t.Fatal(err)
}
}
func TestFuture_Remote_Await_Rejected(t *testing.T) {
ctx := testContext("root", nil)
rec := rejectedPromise(t, "p-1", "boom")
f := &Future{id: "p-1", ctx: ctx, kind: futureRemote, record: &rec}
var got int
err := f.Await(&got)
if err == nil {
t.Fatal("expected error")
}
var app *ApplicationError
if !errors.As(err, &app) {
t.Fatalf("expected *ApplicationError, got %T: %v", err, err)
}
if app.Message != "boom" {
t.Fatalf("expected 'boom', got %q", app.Message)
}
}
func TestFuture_Remote_Await_Pending_PanicsAndRegistersTodo(t *testing.T) {
ctx := testContext("root", nil)
rec := pendingPromise("p-pending")
f := &Future{id: "p-pending", ctx: ctx, kind: futureRemote, record: &rec}
assertPanicsWithSuspend(t, func() {
_ = f.Await(nil)
})
todos := ctx.drainRemoteTodos()
if len(todos) != 1 || todos[0] != "p-pending" {
t.Fatalf("expected spawnedRemote=[p-pending], got %v", todos)
}
}
func TestFuture_Local_Await_PreSettledResolved(t *testing.T) {
ctx := testContext("root", nil)
rec := resolvedPromise(t, "loc", "hello")
f := &Future{id: "loc", ctx: ctx, kind: futureLocal, record: &rec}
var got string
if err := f.Await(&got); err != nil {
t.Fatal(err)
}
if got != "hello" {
t.Fatalf("expected 'hello', got %q", got)
}
}
func TestFuture_Local_Await_PreSettledRejected(t *testing.T) {
ctx := testContext("root", nil)
rec := rejectedPromise(t, "loc", "kaboom")
f := &Future{id: "loc", ctx: ctx, kind: futureLocal, record: &rec}
err := f.Await(nil)
if err == nil || err.Error() != "application error: kaboom" {
t.Fatalf("expected application error kaboom, got %v", err)
}
}
func TestFuture_Local_Await_GoroutineCompletes(t *testing.T) {
ctx := testContext("root", nil)
ch := make(chan localResult, 1)
f := &Future{id: "loc", ctx: ctx, kind: futureLocal, result: ch}
go func() {
rec := resolvedPromise(t, "loc", 99)
f.record = &rec
ch <- localResult{}
}()
var got int
if err := f.Await(&got); err != nil {
t.Fatal(err)
}
if got != 99 {
t.Fatalf("expected 99, got %d", got)
}
}
func TestFuture_Local_Await_GoroutineSuspended_RePanics(t *testing.T) {
ctx := testContext("root", nil)
ch := make(chan localResult, 1)
f := &Future{id: "loc", ctx: ctx, kind: futureLocal, result: ch}
ch <- localResult{suspended: true}
assertPanicsWithSuspend(t, func() {
_ = f.Await(nil)
})
}
func TestFuture_Local_Await_GoroutineError(t *testing.T) {
ctx := testContext("root", nil)
ch := make(chan localResult, 1)
f := &Future{id: "loc", ctx: ctx, kind: futureLocal, result: ch}
ch <- localResult{err: errors.New("infra: out of memory")}
err := f.Await(nil)
if err == nil || !strings.Contains(err.Error(), "out of memory") {
t.Fatalf("expected infra error, got %v", err)
}
}
func TestFuture_Local_DoubleAwait_ReturnsSameValue(t *testing.T) {
ctx := testContext("root", nil)
rec := resolvedPromise(t, "loc", 1)
f := &Future{id: "loc", ctx: ctx, kind: futureLocal, record: &rec}
var v int
if err := f.Await(&v); err != nil {
t.Fatal(err)
}
if v != 1 {
t.Fatalf("first Await: expected 1, got %d", v)
}
v = 0
if err := f.Await(&v); err != nil {
t.Fatalf("second Await returned error: %v", err)
}
if v != 1 {
t.Fatalf("second Await: expected 1, got %d", v)
}
}
func TestFuture_Local_DoubleAwait_GoroutineReadOnce(t *testing.T) {
ctx := testContext("root", nil)
ch := make(chan localResult, 1)
f := &Future{id: "loc", ctx: ctx, kind: futureLocal, result: ch}
go func() {
rec := resolvedPromise(t, "loc", 7)
f.record = &rec
ch <- localResult{}
}()
var v int
if err := f.Await(&v); err != nil {
t.Fatal(err)
}
if v != 7 {
t.Fatalf("first Await: expected 7, got %d", v)
}
v = 0
if err := f.Await(&v); err != nil {
t.Fatalf("second Await returned error: %v", err)
}
if v != 7 {
t.Fatalf("second Await: expected 7, got %d", v)
}
}
func TestFuture_Remote_DoubleAwait_Resolved_ReturnsSameValue(t *testing.T) {
ctx := testContext("root", nil)
rec := resolvedPromise(t, "p", 3)
f := &Future{id: "p", ctx: ctx, kind: futureRemote, record: &rec}
var v int
if err := f.Await(&v); err != nil {
t.Fatal(err)
}
v = 0
if err := f.Await(&v); err != nil {
t.Fatalf("second Await returned error: %v", err)
}
if v != 3 {
t.Fatalf("expected 3, got %d", v)
}
}