-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventloop_test.go
More file actions
287 lines (248 loc) · 5.36 KB
/
eventloop_test.go
File metadata and controls
287 lines (248 loc) · 5.36 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package ramune_test
import (
"errors"
"testing"
"time"
"github.com/i2y/ramune"
)
func TestSetTimeout(t *testing.T) {
r := newOrSkip(t)
defer r.Close()
if err := r.Exec(`
globalThis.fired = false;
setTimeout(function() { globalThis.fired = true; }, 10);
`); err != nil {
t.Fatal(err)
}
// Before running the event loop, the callback hasn't fired.
v, err := r.Eval("globalThis.fired")
if err != nil {
t.Fatal(err)
}
b, _ := v.Bool()
v.Close()
if b {
t.Fatal("callback should not have fired yet")
}
// Run the event loop.
if err := r.RunEventLoop(); err != nil {
t.Fatal(err)
}
v2, err := r.Eval("globalThis.fired")
if err != nil {
t.Fatal(err)
}
defer v2.Close()
b2, _ := v2.Bool()
if !b2 {
t.Fatal("callback should have fired after RunEventLoop")
}
}
func TestSetInterval(t *testing.T) {
r := newOrSkip(t)
defer r.Close()
if err := r.Exec(`
globalThis.count = 0;
var id = setInterval(function() {
globalThis.count++;
if (globalThis.count >= 3) clearInterval(id);
}, 10);
`); err != nil {
t.Fatal(err)
}
if err := r.RunEventLoop(); err != nil {
t.Fatal(err)
}
v, err := r.Eval("globalThis.count")
if err != nil {
t.Fatal(err)
}
defer v.Close()
n, _ := v.Float64()
if n != 3.0 {
t.Fatalf("got count=%f, want 3", n)
}
}
func TestClearTimeout(t *testing.T) {
r := newOrSkip(t)
defer r.Close()
if err := r.Exec(`
globalThis.fired = false;
var id = setTimeout(function() { globalThis.fired = true; }, 10);
clearTimeout(id);
`); err != nil {
t.Fatal(err)
}
// Event loop should exit immediately (no pending timers).
if err := r.RunEventLoop(); err != nil {
t.Fatal(err)
}
v, err := r.Eval("globalThis.fired")
if err != nil {
t.Fatal(err)
}
defer v.Close()
b, _ := v.Bool()
if b {
t.Fatal("cleared timeout should not fire")
}
}
func TestTick(t *testing.T) {
r := newOrSkip(t)
defer r.Close()
if err := r.Exec(`
globalThis.order = [];
setTimeout(function() { globalThis.order.push('a'); }, 0);
setTimeout(function() { globalThis.order.push('b'); }, 50);
`); err != nil {
t.Fatal(err)
}
// First tick should fire the immediate timer.
pending, err := r.Tick()
if err != nil {
t.Fatal(err)
}
if !pending {
t.Fatal("should still have pending timers")
}
v, err := r.Eval("JSON.stringify(globalThis.order)")
if err != nil {
t.Fatal(err)
}
s, _ := v.GoString()
v.Close()
if s != `["a"]` {
t.Fatalf("after first tick got %s, want [\"a\"]", s)
}
// Run the rest.
if err := r.RunEventLoop(); err != nil {
t.Fatal(err)
}
v2, err := r.Eval("JSON.stringify(globalThis.order)")
if err != nil {
t.Fatal(err)
}
defer v2.Close()
s2, _ := v2.GoString()
if s2 != `["a","b"]` {
t.Fatalf("after RunEventLoop got %s, want [\"a\",\"b\"]", s2)
}
}
func TestEvalAsync(t *testing.T) {
r := newOrSkip(t)
defer r.Close()
// EvalAsync with a setTimeout-based Promise.
v, err := r.EvalAsync(`
new Promise(function(resolve) {
setTimeout(function() { resolve(42); }, 10);
})
`)
if err != nil {
t.Fatal(err)
}
defer v.Close()
f, err := v.Float64()
if err != nil {
t.Fatal(err)
}
if f != 42.0 {
t.Fatalf("got %f, want 42", f)
}
}
func TestEvalAsyncReject(t *testing.T) {
r := newOrSkip(t)
defer r.Close()
_, err := r.EvalAsync(`
new Promise(function(resolve, reject) {
setTimeout(function() { reject(new Error('boom')); }, 10);
})
`)
if err == nil {
t.Fatal("expected error from rejected promise")
}
var jsErr *ramune.JSError
if !errors.As(err, &jsErr) {
t.Fatalf("expected *JSError, got %T: %v", err, err)
}
if jsErr.Message == "" {
t.Fatal("expected non-empty error message")
}
}
func TestEvalAsyncSyncValue(t *testing.T) {
r := newOrSkip(t)
defer r.Close()
// EvalAsync should also work with non-Promise values.
v, err := r.EvalAsync(`1 + 2`)
if err != nil {
t.Fatal(err)
}
defer v.Close()
f, err := v.Float64()
if err != nil {
t.Fatal(err)
}
if f != 3.0 {
t.Fatalf("got %f, want 3", f)
}
}
func TestSetImmediate(t *testing.T) {
r := newOrSkip(t)
defer r.Close()
if err := r.Exec(`
globalThis.order = [];
setTimeout(function() { globalThis.order.push('timeout'); }, 0);
setImmediate(function() { globalThis.order.push('immediate'); });
`); err != nil {
t.Fatal(err)
}
if err := r.RunEventLoop(); err != nil {
t.Fatal(err)
}
v, err := r.Eval("JSON.stringify(globalThis.order)")
if err != nil {
t.Fatal(err)
}
defer v.Close()
s, _ := v.GoString()
// setImmediate should fire before setTimeout(0) since immediates
// are processed first in each tick.
if s != `["immediate","timeout"]` {
t.Fatalf("got %s, want [\"immediate\",\"timeout\"]", s)
}
}
func TestEventLoopTimeout(t *testing.T) {
r := newOrSkip(t)
defer r.Close()
// setInterval without clearInterval should hit the timeout.
if err := r.Exec(`setInterval(function() {}, 10)`); err != nil {
t.Fatal(err)
}
err := r.RunEventLoopFor(100 * time.Millisecond)
if err == nil {
t.Fatal("expected timeout error")
}
}
func TestEvalAsyncChainedPromises(t *testing.T) {
r := newOrSkip(t)
defer r.Close()
v, err := r.EvalAsync(`
new Promise(function(resolve) {
setTimeout(function() { resolve(10); }, 10);
}).then(function(x) {
return x * 2;
}).then(function(x) {
return x + 1;
})
`)
if err != nil {
t.Fatal(err)
}
defer v.Close()
f, err := v.Float64()
if err != nil {
t.Fatal(err)
}
if f != 21.0 {
t.Fatalf("got %f, want 21 (10*2+1)", f)
}
}