-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathepoll_test.go
170 lines (150 loc) · 3.32 KB
/
epoll_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
package epoller
import (
"errors"
"io"
"log"
"net"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestPoller(t *testing.T) {
// connections
num := 10
// msg per connection
msgPerConn := 10
poller, err := NewPoller(0)
require.NoError(t, err)
// start server
ln, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
defer ln.Close()
go func() {
for {
conn, err := ln.Accept()
if err != nil {
return
}
poller.Add(conn)
}
}()
// create num connections and send msgPerConn messages per connection
for i := 0; i < num; i++ {
go func() {
conn, err := net.Dial("tcp", ln.Addr().String())
if err != nil {
t.Error(err)
return
}
time.Sleep(time.Second)
for i := 0; i < msgPerConn; i++ {
n, err := conn.Write([]byte("hello world"))
if err != nil {
t.Error(err)
}
if n != len("hello world") {
t.Errorf("expect to write %d bytes but got %d bytes", len("hello world"), n)
}
}
conn.Close()
}()
}
// read those num * msgPerConn messages, and each message (hello world) contains 11 bytes.
done := make(chan struct{})
errs := make(chan error)
var total int
var count int
expected := num * msgPerConn * len("hello world")
go func(errs chan error) {
for {
conns, err := poller.Wait(128)
if err != nil {
t.Log(err)
errs <- err // fatal errors (i.e t.Fatal()) must be reported in the main test goroutine
return
}
if len(conns) == 0 {
continue
}
count++
buf := make([]byte, 1024)
for _, conn := range conns {
n, err := conn.Read(buf)
if err != nil {
if err == io.EOF || errors.Is(err, net.ErrClosed) {
poller.Remove(conn)
conn.Close()
} else {
t.Error(err)
}
}
total += n
}
if total == expected {
break
}
}
t.Logf("read all %d bytes, count: %d", total, count)
close(done)
}(errs)
select {
case <-done:
case <-time.After(2 * time.Second):
case err := <-errs:
t.Fatal(err)
}
if total != expected {
t.Fatalf("epoller does not work. expect %d bytes but got %d bytes", expected, total)
}
}
type netPoller struct {
Poller Poller
WriteReq chan uint64
}
func TestPoller_growstack(t *testing.T) {
var nps []netPoller
for i := 0; i < 2; i++ {
poller, err := NewPoller(128)
if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
// the following line cause goroutine stack grow and copy local variables to new allocated stack and switch to new stack
// but runtime.adjustpointers will check whether pointers bigger than runtime.minLegalPointer(4096) or throw a panic
// fatal error: invalid pointer found on stack (runtime/stack.go:[email protected])
// since NewEpoller return A pointer created by CreateIoCompletionPort may less than 4096
np := netPoller{
Poller: poller,
WriteReq: make(chan uint64, 1000000),
}
nps = append(nps, np)
}
poller := nps[0].Poller
// start server
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatal(err)
}
defer ln.Close()
go func() {
for {
conn, err := ln.Accept()
if err != nil {
return
}
poller.Add(conn)
}
}()
conn, err := net.Dial("tcp", ln.Addr().String())
if err != nil {
t.Error(err)
return
}
time.Sleep(200 * time.Millisecond)
for i := 0; i < 100; i++ {
conn.Write([]byte("hello world"))
}
conn.Close()
}