-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathio_test.go
363 lines (294 loc) · 7.07 KB
/
io_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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package sonic
import (
"errors"
"github.com/talostrading/sonic/internal"
"log"
"runtime"
"testing"
"time"
"github.com/talostrading/sonic/sonicerrors"
)
func TestPost(t *testing.T) {
ioc := MustIO()
defer ioc.Close()
var xs [1000]bool
for i := 0; i < len(xs); i++ {
// We need to copy i otherwise xs[i] will panic because i == len(xs)
// when the loop is done.
j := i
ioc.Post(func() {
xs[j] = true
})
}
if ioc.Posted() != 1000 {
t.Fatal("expected 1000 posted events")
}
if p := ioc.Pending(); p != 1000 {
t.Fatalf("not accounting for pending operations correctly expected=%d given=%d", 1000, p)
}
err := ioc.RunPending()
if err != nil {
t.Fatal(err)
}
for i, x := range xs {
if !x {
t.Fatalf("handler %d not set", i)
}
}
if p := ioc.Pending(); p != 0 {
t.Fatalf("not accounting for pending operations correctly expected=%d given=%d", 0, p)
}
if g := ioc.Posted(); g != 0 {
t.Fatalf("expected 0 posted events but got %d", g)
}
}
func TestEmptyPoll(t *testing.T) {
ioc := MustIO()
defer ioc.Close()
if err := ioc.Poll(); !errors.Is(err, sonicerrors.ErrTimeout) {
t.Fatalf("expected timeout as not operations are scheduled")
}
}
func TestRunOneFor(t *testing.T) {
ioc := MustIO()
defer ioc.Close()
start := time.Now()
expected := time.Millisecond
if err := ioc.RunOneFor(expected); !errors.Is(err, sonicerrors.ErrTimeout) {
t.Fatalf(
"expected timeout as no operations are scheduled received=%v",
err)
}
end := time.Now()
if given := end.Sub(start); given.Milliseconds() < expected.Milliseconds() {
t.Fatalf(
"invalid timeout ioc.RunOneFor(...) expected=%v given=%v",
expected,
given,
)
}
}
func TestRightNumberOfPolledEvents(t *testing.T) {
ioc := MustIO()
defer ioc.Close()
timer, err := NewTimer(ioc)
if err != nil {
t.Fatal(err)
}
dur := 500 * time.Millisecond
err = timer.ScheduleOnce(dur, func() {})
if err != nil {
t.Fatal(err)
}
start := time.Now()
npolled := 0
for {
if time.Now().Sub(start) > 2*dur {
break
}
n, err := ioc.PollOne()
if err != nil && !errors.Is(err, sonicerrors.ErrTimeout) {
t.Fatal(err)
}
if n > npolled {
npolled = n
}
}
if npolled != 1 {
t.Fatalf("expected to poll 1 operation, but polled %d", npolled)
}
}
func TestPollOneAfterClose(t *testing.T) {
ioc := MustIO()
if ioc.Closed() {
t.Fatal("ioc should not be closed")
}
n, err := ioc.PollOne()
if err != nil && !errors.Is(err, sonicerrors.ErrTimeout) {
t.Fatal(err)
}
if n != 0 {
t.Fatalf("polled %d but should not have polled any", n)
}
err = ioc.Close()
if err != nil {
t.Fatal(err)
}
if !ioc.Closed() {
t.Fatal("ioc should be closed")
}
n, err = ioc.PollOne()
if err == nil || errors.Is(err, sonicerrors.ErrTimeout) || n != 0 {
t.Fatalf("the poll should have failed after close n=%d err=%v", n, err)
}
}
func TestRunOneForAfterClose(t *testing.T) {
ioc := MustIO()
if ioc.Closed() {
t.Fatal("ioc should not be closed")
}
err := ioc.RunOneFor(time.Millisecond)
if err != nil && !errors.Is(err, sonicerrors.ErrTimeout) {
t.Fatal(err)
}
err = ioc.Close()
if err != nil {
t.Fatal(err)
}
if !ioc.Closed() {
t.Fatal("ioc should be closed")
}
err = ioc.RunOneFor(time.Millisecond)
if err == nil || errors.Is(err, sonicerrors.ErrTimeout) {
t.Fatalf("the poll should have failed after close err=%v", err)
}
}
func TestPollNothing(t *testing.T) {
ioc := MustIO()
defer ioc.Close()
n, err := ioc.PollOne()
if n != 0 && !errors.Is(err, sonicerrors.ErrTimeout) {
t.Fatalf("wrong n=%d and err=%v", n, err)
}
}
func TestSleep(t *testing.T) {
ioc := MustIO()
defer ioc.Close()
timer, err := NewTimer(ioc)
if err != nil {
panic(err)
}
scheduled := time.Now()
err = timer.ScheduleOnce(10*time.Millisecond, func() {
time.Sleep(10 * time.Millisecond)
err = timer.ScheduleOnce(10*time.Millisecond, func() {
time.Sleep(10 * time.Millisecond)
})
if err != nil {
t.Fatal(err)
}
})
if err != nil {
t.Fatal(err)
}
ioc.RunPending()
dur := time.Since(scheduled)
log.Printf("slept for a total of %s, went over by %s", dur, dur-time.Duration(40)*time.Millisecond)
}
func TestRunWarmInvalidBusyCycles(t *testing.T) {
ioc := MustIO()
defer ioc.Close()
if err := ioc.RunWarm(0, 2*time.Millisecond); err == nil {
t.Fatal("should have errored: invalid busy-cycles")
}
}
func TestRunWarmInvalidTimeout(t *testing.T) {
ioc := MustIO()
defer ioc.Close()
if err := ioc.RunWarm(10, time.Microsecond); err == nil {
t.Fatal("should have errored: invalid timeout")
}
}
func TestRunWarm(t *testing.T) {
// This test will always pass until we have some mechanism to measure the reactor's cycle duration.
// TODO revisit after introducing CycleDur() in reactor.
ioc := MustIO()
defer ioc.Close()
ticker, err := NewTimer(ioc)
if err != nil {
t.Fatal(err)
}
defer ticker.Close()
i := 0
ticker.ScheduleRepeating(time.Millisecond, func() {
i++
if i >= 10 {
ioc.Close()
}
})
// The ticker triggers every 1ms for 10 times, so we should run through the whole warm period and then yield
// until the ticker triggers, for 10 times. Each yield be resumed by the ticker.
if err := ioc.RunWarm(10, 2*time.Millisecond); err != nil {
if i < 10 {
// something happened before `Close()`ing the reactor.
t.Fatal(err)
}
}
}
func TestIOPending(t *testing.T) {
ioc := MustIO()
defer ioc.Close()
var slots []*internal.Slot
for i := 0; i < 4096; i++ {
if ioc.pending.static[i] != nil {
t.Fatal("expected static pending element to be nil")
}
slots = append(slots, &internal.Slot{Fd: i})
ioc.Register(slots[len(slots)-1])
if ioc.pending.static[i] == nil {
t.Fatal("expected static pending element to be non-nil")
}
}
if len(ioc.pending.dynamic) != 0 {
t.Fatal("pending dynamic should have length 0")
}
for i := 4096; i < 8192; i++ {
slots = append(slots, &internal.Slot{Fd: i})
ioc.Register(slots[len(slots)-1])
}
if len(ioc.pending.dynamic) != 4096 {
t.Fatal("pending dynamic should have 4096 entries")
}
for i := 0; i < 8192; i++ {
ioc.Deregister(slots[i])
if i < 4096 {
if ioc.pending.static[i] != nil {
t.Fatal("pending static element should be nil")
}
}
}
if len(ioc.pending.dynamic) != 0 {
t.Fatal("pending dynamic should have length 0")
}
for _, slot := range ioc.pending.static {
if slot != nil {
t.Fatal("static slot should be nil")
}
}
}
func TestSetUnsetRead(t *testing.T) {
ioc := MustIO()
defer ioc.Close()
pipe, err := internal.NewPipe()
if err != nil {
t.Fatal(err)
}
if pipe.ReadFd() != pipe.Slot().Fd {
t.Fatal("pipe must be identified by its read end file descriptor")
}
for i := 0; i < 10; i++ {
if err := ioc.SetRead(pipe.Slot()); err != nil {
t.Fatal(err)
}
}
for i := 0; i < 100; i++ {
if err := ioc.UnsetRead(pipe.Slot()); err != nil {
t.Fatal(err)
}
}
for i := 0; i < 100; i++ {
if err := ioc.UnsetReadWrite(pipe.Slot()); err != nil {
t.Fatal(err)
}
}
}
func BenchmarkPollOne(b *testing.B) {
ioc := MustIO()
defer ioc.Close()
runtime.GOMAXPROCS(1)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
for i := 0; i < b.N; i++ {
ioc.PollOne()
}
}