-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepoll_linux_test.go
143 lines (133 loc) · 2.78 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
// +build linux
package xlistener
import (
"bufio"
"bytes"
"fmt"
"io"
"net"
"reflect"
"sort"
"strconv"
"sync"
"testing"
"time"
)
const (
DELIMITER byte = '\n'
)
func ReadLine(conn net.Conn) (string, error) {
reader := bufio.NewReader(conn)
var buffer bytes.Buffer
for {
ba, isPrefix, err := reader.ReadLine()
if err != nil {
if err == io.EOF {
break
}
return "", err
}
buffer.Write(ba)
if !isPrefix {
break
}
}
return buffer.String(), nil
}
func WriteLine(conn net.Conn, content string) (int, error) {
content = fmt.Sprintf("%s%c", content, DELIMITER)
writer := bufio.NewWriter(conn)
number, err := writer.WriteString(content)
if err == nil {
err = writer.Flush()
}
return number, err
}
func findPort() net.Addr {
if l, err := net.Listen("tcp", "127.0.0.1:0"); err != nil {
panic("Could not find available server port")
} else {
defer func() { _ = l.Close() }()
return l.Addr()
}
}
func TestEpollListener(t *testing.T) {
addr := findPort()
ln, err := Listen(func() (listener net.Listener, e error) {
return net.Listen(addr.Network(), addr.String())
})
if err != nil {
t.Fatal(err)
}
connAccepted := make([]int, 0)
defer func() { _ = ln.Close() }()
go func() {
for {
conn, err := ln.Accept()
if err != nil {
fmt.Println("epool listener close with:", err)
return
}
go func(connIn net.Conn) {
req, err := ReadLine(connIn)
if err != nil {
t.Fatal(err)
}
id, err := strconv.Atoi(req)
if err != nil {
t.Fatalf("get io error:%s req:%s", err.Error(), req)
}
connAccepted = append(connAccepted, id)
_, err = WriteLine(connIn, req)
if err != nil {
t.Fatal(err)
}
}(conn)
}
}()
wg := sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
conn, err := net.Dial(addr.Network(), addr.String())
if err != nil {
t.Fatal(err)
}
go func(connIn net.Conn, index int) {
time.AfterFunc(time.Duration(1)*time.Second, func() {
_ = connIn.Close()
wg.Done()
})
}(conn, i)
}
connShouldAccepted := make([]int, 0)
for i := 10; i < 20; i++ {
wg.Add(1)
conn, err := net.Dial(addr.Network(), addr.String())
if err != nil {
t.Fatal(err)
}
connShouldAccepted = append(connShouldAccepted, i)
go func(connIn net.Conn, iIn int) {
req := fmt.Sprintf("%d", iIn)
_, err = WriteLine(connIn, req)
if err != nil {
t.Fatal(err)
}
rsp, err := ReadLine(connIn)
if err != nil {
t.Fatal(err)
}
if req != rsp {
t.Fatalf("req rsp not equal,expected:%s got:%s", req, rsp)
}
_ = connIn.Close()
wg.Done()
}(conn, i)
}
wg.Wait()
sort.Ints(connAccepted)
sort.Ints(connShouldAccepted)
if !reflect.DeepEqual(connAccepted, connShouldAccepted) {
t.Fatalf("conn not equal,should: %v got: %v", connShouldAccepted, connAccepted)
}
}