-
Notifications
You must be signed in to change notification settings - Fork 1
/
epoll_linux_test.go
148 lines (139 loc) · 2.96 KB
/
epoll_linux_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
// ©Hayabusa Cloud Co., Ltd. 2022. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//go:build linux
package sox
import (
"testing"
"time"
)
func TestEpoll(t *testing.T) {
ep, err := newPoller(16)
if err != nil {
t.Errorf("new epoll: %v", err)
return
}
defer ep.Close()
efd1, err := NewEventfd()
if err != nil {
t.Errorf("new event fd: %v", err)
return
}
defer efd1.Close()
err = efd1.WriteUint(5)
if err != nil {
t.Errorf("event fd write: %v", err)
return
}
efd2, err := NewEventfd()
if err != nil {
t.Errorf("new event fd: %v", err)
return
}
defer efd2.Close()
err = efd2.WriteUint(10)
if err != nil {
t.Errorf("event fd write: %v", err)
return
}
d := time.Millisecond * 200
events, err := ep.wait(d)
if err != nil {
t.Errorf("epoll wait: %v", err)
return
}
if len(events) != 0 {
t.Errorf("epoll wait expected event num=%d but got %v", 0, events)
return
}
err = ep.add(efd1.Fd(), pollerEventIn)
if err != nil {
t.Errorf("epoll add fd=%d: %v", efd1.Fd(), err)
return
}
events, err = ep.wait(d)
if err != nil {
t.Errorf("epoll wait: %v", err)
return
}
if len(events) != 1 {
t.Errorf("epoll wait expected event num=%d but got %v", 1, events)
return
}
if int(events[0].Fd) != efd1.Fd() || events[0].Events != pollerEventIn {
t.Errorf("epoll event expected fd=%d events=%d but got %v", efd1.Fd(), pollerEventIn, events[0])
return
}
err = ep.add(efd2.Fd(), pollerEventIn)
if err != nil {
t.Errorf("epoll add fd=%d: %v", efd2.Fd(), err)
return
}
events, err = ep.wait(d)
if err != nil {
t.Errorf("epoll wait: %v", err)
return
}
if len(events) != 1 {
t.Errorf("epoll wait expected event num=%d but got %v", 1, events)
return
}
if int(events[0].Fd) != efd2.Fd() || events[0].Events != pollerEventIn {
t.Errorf("epoll event expected fd=%d events=%d but got %v", efd2.Fd(), pollerEventIn, events[0])
return
}
err = efd1.WriteUint(5)
if err != nil {
t.Errorf("event fd write: %v", err)
return
}
err = efd2.WriteUint(10)
if err != nil {
t.Errorf("event fd write: %v", err)
return
}
events, err = ep.wait(d)
if err != nil {
t.Errorf("epoll wait: %v", err)
return
}
if len(events) != 2 {
t.Errorf("epoll wait expected event num=%d but got %v", 2, events)
return
}
err = ep.del(efd1.Fd())
if err != nil {
t.Errorf("epoll del fd=%d: %v", efd1.Fd(), err)
return
}
err = ep.del(efd2.Fd())
if err != nil {
t.Errorf("epoll del fd=%d: %v", efd2.Fd(), err)
return
}
err = efd1.WriteUint(5)
if err != nil {
t.Errorf("event fd write: %v", err)
return
}
err = efd2.WriteUint(10)
if err != nil {
t.Errorf("event fd write: %v", err)
return
}
for {
events, err = ep.wait(d)
if err == ErrInterruptedSyscall {
continue
}
if err != nil {
t.Errorf("epoll wait: %v", err)
return
}
break
}
if len(events) != 0 {
t.Errorf("epoll wait expected event num=%d but got %v", 0, events)
return
}
}