forked from Allenxuxu/gev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_conn_test.go
121 lines (97 loc) · 2.07 KB
/
server_conn_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
package gev
import (
"github.com/Allenxuxu/toolkit/sync"
"io"
"net"
"testing"
"time"
"github.com/Allenxuxu/gev/connection"
"github.com/Allenxuxu/gev/log"
)
type example2 struct {
}
func (s *example2) OnConnect(c *connection.Connection) {
log.Info(" OnConnect : ", c.PeerAddr())
if err := c.Close(); err != nil {
panic(err)
}
}
func (s *example2) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
log.Info("OnMessage")
return
}
func (s *example2) OnClose(c *connection.Connection) {
log.Info("OnClose")
}
func TestConnClose(t *testing.T) {
log.SetLevel(log.LevelDebug)
handler := new(example2)
s, err := NewServer(handler,
Network("tcp"),
Address(":1833"),
NumLoops(8),
ReusePort(true))
if err != nil {
t.Fatal(err)
}
go s.Start()
conn, err := net.DialTimeout("tcp", "127.0.0.1:1833", time.Second*60)
if err != nil {
log.Error(err)
return
}
buf := make([]byte, 10)
n, err := conn.Read(buf)
if n != 0 || err != io.EOF {
t.Fatal()
}
s.Stop()
}
type example3 struct {
}
func (s *example3) OnConnect(c *connection.Connection) {
//log.Info(" OnConnect : ", c.PeerAddr())
}
func (s *example3) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out []byte) {
//log.Info("OnMessage")
return
}
func (s *example3) OnClose(c *connection.Connection) {
//log.Info("OnClose")
}
func TestIdleTime(t *testing.T) {
log.SetLevel(log.LevelDebug)
handler := new(example3)
s, err := NewServer(handler,
Network("tcp"),
Address(":1830"),
NumLoops(8),
ReusePort(true),
IdleTime(3*time.Second))
if err != nil {
t.Fatal(err)
}
go s.Start()
start := time.Now()
wg := &sync.WaitGroupWrapper{}
for i := 0; i < 100; i++ {
wg.AddAndRun(func() {
conn, err := net.DialTimeout("tcp", "127.0.0.1:1830", time.Second*60)
if err != nil {
log.Error(err)
return
}
buf := make([]byte, 10)
n, err := conn.Read(buf)
if n != 0 || err != io.EOF {
t.Fatal()
}
})
}
wg.Wait()
et := time.Since(start)
if et.Seconds() > 4 || et.Seconds() < 3 {
t.Fatal(et.Seconds())
}
s.Stop()
}