-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol_test.go
More file actions
165 lines (156 loc) · 4.35 KB
/
protocol_test.go
File metadata and controls
165 lines (156 loc) · 4.35 KB
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
package protocol
import (
"errors"
"fmt"
"github.com/Akvicor/glog"
"net"
"sync/atomic"
"testing"
"time"
)
var serverClosed = make(chan bool, 1)
var clientClosed = make(chan bool, 1)
func TestProtocol(t *testing.T) {
SetLogMask(MaskFATAL | MaskERROR | MaskWARNING | MaskINFO)
go testServer(t)
time.Sleep(time.Second)
go testClient(t)
<-clientClosed
<-serverClosed
}
func testServer(t *testing.T) {
defer func() {
serverClosed <- true
}()
listen, err := net.Listen("tcp", "0.0.0.0:9999")
if err != nil {
glog.Error("[server] Listen() failed, err: %s", err)
return
}
glog.Info("[server] Listen 0.0.0.0:9999")
for {
conn, err := listen.Accept() // 监听客户端的连接请求
if err != nil {
glog.Error("[server] Accept() failed, err: %s", err)
continue
}
glog.Info("[server] Accept %s %s", conn.LocalAddr().String(), conn.RemoteAddr().String())
var Index uint32 = 0
protServer := New("server", conn, conn, 8, func(data []byte) {
// 处理获取到的数据
fmt.Printf("[server] received [%s]\n", string(data))
atomic.AddUint32(&Index, 1)
ans := fmt.Sprintf("client msg %d", atomic.LoadUint32(&Index))
if ans != string(data) {
t.Errorf("test client error need %s got %s", ans, string(data))
}
}, func(p *Protocol) bool {
// protocol还在运行,但心跳超时
fmt.Println("[server] heartbeat timeout")
return false
}, func() {
// 每次conn.Read前运行
_ = conn.SetReadDeadline(time.Now().Add(10 * time.Second))
}, nil, func() {
// 每次conn.Write前运行
_ = conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
}, nil, func() {
// protocol状态更改为killed时运行
_ = conn.Close()
})
protServer.SetHeartbeatInterval(3)
protServer.SetHeartbeatTimeout(10)
protServer.Connect(true)
i := 0
for {
time.Sleep(5 * time.Second)
i++
msg := fmt.Sprintf("serv msg %d", i)
fmt.Printf("[server] send [%s]\n", msg)
err = protServer.Write([]byte(msg))
if err != nil {
if !errors.Is(err, ErrorWriterIsKilled) {
glog.Warning("[server] failed to write %v", err)
} else {
if protServer.GetHeartbeatLastSend() == 0 {
t.Error("server.GetHeartbeatLastSend is zero")
}
if protServer.GetHeartbeatLastReceived() == 0 {
t.Error("server.GetHeartbeatLastReceived is zero")
}
glog.Info("wait server killed")
err = protServer.WaitKilled(60)
if err != nil {
t.Errorf("server killed failed [%d]", protServer.GetRunningRoutine())
}
glog.Info("wait server killed [%d]", protServer.GetRunningRoutine())
}
return
}
}
}
}
func testClient(t *testing.T) {
defer func() {
clientClosed <- true
}()
conn, err := net.Dial("tcp", "127.0.0.1:9999")
if err != nil {
glog.Error("[client] Dial() failed, err: %s", err)
return
}
glog.Info("[client] Connected")
var Index uint32 = 0
protClient := New("client", conn, conn, 8, func(data []byte) {
fmt.Printf("[client] received [%s]\n", string(data))
atomic.AddUint32(&Index, 1)
ans := fmt.Sprintf("serv msg %d", atomic.LoadUint32(&Index))
if ans != string(data) {
t.Errorf("test client error need %s got %s", ans, string(data))
}
}, func(*Protocol) bool {
fmt.Println("[client] heartbeat timeout")
return true
}, func() {
_ = conn.SetReadDeadline(time.Now().Add(10 * time.Second))
}, nil, func() {
_ = conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
}, nil, func() {
_ = conn.Close()
})
protClient.SetHeartbeatInterval(3)
protClient.SetHeartbeatTimeout(10)
protClient.Connect(false)
time.Sleep(1 * time.Second)
i := 0
for {
time.Sleep(5 * time.Second)
i++
msg := fmt.Sprintf("client msg %d", i)
fmt.Printf("[client] send [%s]\n", msg)
err = protClient.Write([]byte(msg))
if err != nil {
if !errors.Is(err, ErrorWriterIsKilled) {
glog.Warning("[client] failed to write %v", err)
}
return
}
if i == 6 {
if protClient.GetHeartbeatLastSend() == 0 {
t.Error("client.GetHeartbeatLastSend is zero")
}
if protClient.GetHeartbeatLastReceived() == 0 {
t.Error("client.GetHeartbeatLastReceived is zero")
}
glog.Info("kill client")
protClient.Kill()
glog.Info("wait client killed")
err := protClient.WaitKilled(60)
if err != nil {
t.Errorf("kill client failed [%d]", protClient.GetRunningRoutine())
}
glog.Info("wait client killed [%d]", protClient.GetRunningRoutine())
return
}
}
}