-
Notifications
You must be signed in to change notification settings - Fork 2
/
io_eventloop_test.go
155 lines (143 loc) · 3.99 KB
/
io_eventloop_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
package liblpc
import (
"github.com/gen-iot/std"
"golang.org/x/sys/unix"
"os"
"testing"
"time"
)
func init() {
Debug = true
}
func testEvtloop(evtLoop EventLoop) {
time.Sleep(time.Second)
for {
time.Sleep(time.Second)
evtLoop.RunInLoop(func() {
stdLog(time.Now().String())
})
}
}
func TestNotify(t *testing.T) {
evtLoop, err := NewEventLoop()
go testEvtloop(evtLoop)
std.AssertError(err, "NewEventLoop")
defer std.CloseIgnoreErr(evtLoop)
evtLoop.Run(nil)
}
func TestEpoll_WatcherCtl_CloseBeforeAdd(t *testing.T) {
loop, err := NewIOEvtLoop(1024)
std.AssertError(err, "new io evtloop")
defer std.CloseIgnoreErr(loop)
//
fds, err := MakeIpcSockpair(true)
std.AssertError(err, "make ipc sockpair .1")
conn := NewBufferedConnStream(loop, fds[0], func(sw StreamWriter, buf std.ReadableBuffer) {
stdLog("on read .1")
})
conn.Start()
//
fds2, err := MakeIpcSockpair(true)
std.AssertError(err, "make ipc sockpair .2")
conn2 := NewBufferedConnStream(loop, fds2[0], func(sw StreamWriter, buf std.ReadableBuffer) {
stdLog("on read .2")
})
conn2.Start()
//note! must close here due to system fd(id) reuse policy
err = unix.Close(fds[0])
std.AssertError(err, "close (before add) fds[0] err")
err = unix.Close(fds[1])
std.AssertError(err, "close (before add) fds[1] err")
loop.Run(nil)
}
func TestEpoll_WatcherCtl_CloseAfterAdd(t *testing.T) {
loop, err := NewIOEvtLoop(1024)
std.AssertError(err, "new io evtloop")
defer std.CloseIgnoreErr(loop)
//
fds, err := MakeIpcSockpair(true)
std.AssertError(err, "make ipc sockpair .1")
conn := NewBufferedConnStream(loop, fds[0], func(sw StreamWriter, buf std.ReadableBuffer) {
stdLog("on read .1")
})
conn.Start()
//
fds2, err := MakeIpcSockpair(true)
std.AssertError(err, "make ipc sockpair .2")
conn2 := NewBufferedConnStream(loop, fds2[0], func(sw StreamWriter, buf std.ReadableBuffer) {
stdLog("on read .2")
})
conn2.Start()
go func() {
time.Sleep(time.Second * 2)
// note! must close here due to system fd(id) reuse policy
err = unix.Close(fds[0])
std.AssertError(err, "close fds[0] err")
err = conn.Close()
stdLog("conn close (after add) err:", err)
err = unix.Close(fds[1])
std.AssertError(err, "close fds[1] err")
err = conn2.Close()
stdLog("conn2 close (after add) err:", err)
}()
//
loop.Run(nil)
}
func TestIOEvtLoop(t *testing.T) {
fds, e := MakeIpcSockpair(true)
std.AssertError(e, "MakeIpcSockpair")
loop, e := NewIOEvtLoop(4 * 1024)
std.AssertError(e, "NewIOEvtLoop")
defer std.CloseIgnoreErr(loop)
stream := NewConnStream(loop, int(fds[0]),
func(sw StreamWriter, data []byte, len int) {
stdLog("Server onRead , data is -> ", string(data[:len]))
sw.Write([]byte(time.Now().String()), true)
})
stream.SetOnClose(func(sw StreamWriter, err error) {
stdLog("Server onRead error -> ", err, " ,closed!")
std.CloseIgnoreErr(sw)
})
defer std.CloseIgnoreErr(stream)
stream.Start()
go func() {
for idx := 0; idx < 100; idx++ {
time.Sleep(time.Millisecond)
_, err := unix.Write(fds[1], []byte(time.Now().String()))
std.AssertError(err, "Write")
}
err := unix.Close(fds[1])
std.AssertError(err, "Close")
loop.Break()
}()
loop.Run(nil)
}
func TestSpawnIO(t *testing.T) {
stdLog("current pid = ", os.Getpid())
fds, err := MakeIpcSockpair(true)
std.AssertError(err, "MakeIpcSockpair")
loop, err := NewIOEvtLoop(2 * 1024 * 1024)
std.AssertError(err, "NewIOEvtLoop")
defer std.CloseIgnoreErr(loop)
cmd, err := Spawn("bin/child", fds[1])
std.AssertError(err, "Spawn")
stdLog("spawn success pid = ", cmd.Process.Pid)
stream := NewConnStream(loop, int(fds[0]),
func(sw StreamWriter, data []byte, len int) {
stdLog("Server onRead , data is -> ", string(data[:len]))
sw.Write([]byte(time.Now().String()), true)
})
stream.SetOnClose(func(sw StreamWriter, err error) {
stdLog("Server onRead error -> ", err, " ,closed!")
_ = sw.Close()
})
defer func() {
_ = stream.Close()
}()
stream.Start()
go func() {
err := cmd.Wait()
stdLog("child exit error -> ", err)
}()
loop.Run(nil)
}