-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcpack_test.go
127 lines (106 loc) · 3.13 KB
/
tcpack_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
package tcpack
import (
"fmt"
"net"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestMsgPack_Pack(t *testing.T) {
address, _ := net.ResolveTCPAddr("tcp4", "127.0.0.1:8099")
address2, _ := net.ResolveTCPAddr("tcp4", ":8099")
//server
listener, _ := net.ListenTCP("tcp4", address2)
msgChan := make(chan *Imessage, 10)
go func() {
for {
tcpConnServer, _ := listener.AcceptTCP()
mpServer := NewMsgPack(8, tcpConnServer)
for {
msgRev, _ := mpServer.Unpack()
msgChan <- &msgRev
}
}
}()
// client
tcpConnClient, err := net.DialTCP("tcp4", nil, address)
if err != nil {
fmt.Println("create tcpConn failed")
}
mpClient := NewMsgPack(8, tcpConnClient)
data := "Hello,World"
msgSend := NewMessage(0, uint32(len([]byte(data))), []byte(data))
_, _ = mpClient.Pack(msgSend)
msgRevOut := <-msgChan
Convey("pack msg to send", t, func() {
So(msgSend, ShouldEqual, *msgRevOut)
})
// Continuously sending short messages
data1 := "a"
data2 := "b"
data3 := "c"
msgSend1 := NewMessage(0, uint32(len([]byte(data1))), []byte(data1))
_, _ = mpClient.Pack(msgSend1)
msgSend2 := NewMessage(0, uint32(len([]byte(data2))), []byte(data2))
_, _ = mpClient.Pack(msgSend2)
msgSend3 := NewMessage(0, uint32(len([]byte(data3))), []byte(data3))
_, _ = mpClient.Pack(msgSend3)
msgRevOut1 := <-msgChan
msgRevOut2 := <-msgChan
msgRevOut3 := <-msgChan
Convey("Continuously sending short messages", t, func() {
So(msgSend1, ShouldEqual, *msgRevOut1)
})
Convey("Continuously sending short messages", t, func() {
So(msgSend2, ShouldEqual, *msgRevOut2)
})
Convey("Continuously sending short messages", t, func() {
So(msgSend3, ShouldEqual, *msgRevOut3)
})
// Send a message that exceeds the size of the socket buffer
data4 := make([]byte, 65600)
msgSend4 := NewMessage(0, uint32(len(data4)), data4)
_, _ = mpClient.Pack(msgSend4)
msgRevOut4 := <-msgChan
Convey("Send a message that exceeds the size of the socket buffer", t, func() {
So(msgSend4, ShouldEqual, *msgRevOut4)
So(uint32(65600), ShouldEqual, (*msgRevOut4).GetDataLen())
})
}
func TestMsgPack_Unpack(t *testing.T) {
address, _ := net.ResolveTCPAddr("tcp4", "127.0.0.1:9099")
address2, _ := net.ResolveTCPAddr("tcp4", ":9099")
//server
listener, _ := net.ListenTCP("tcp4", address2)
msgChan := make(chan *Imessage, 10)
go func() {
for {
tcpConnServer, _ := listener.AcceptTCP()
mpServer := NewMsgPack(8, tcpConnServer)
for {
msgRev, _ := mpServer.Unpack()
msgChan <- &msgRev
}
}
}()
// client
tcpConnClient, err := net.DialTCP("tcp4", nil, address)
if err != nil {
fmt.Println("create tcpConn failed")
}
mpClient := NewMsgPack(8, tcpConnClient)
data := ""
msgSend := NewMessage(0, uint32(len([]byte(data))), []byte(data))
_, _ = mpClient.Pack(msgSend)
msgRevOut := <-msgChan
Convey("send an empty msg", t, func() {
So(msgSend, ShouldEqual, *msgRevOut)
})
}
func TestMsgPack_GetHeadLen(t *testing.T) {
address, _ := net.ResolveTCPAddr("tcp4", "127.0.0.1:8099")
tcpConn, _ := net.DialTCP("tcp4", nil, address)
mp := NewMsgPack(9, tcpConn)
Convey("get head length", t, func() {
So(mp.GetHeadLen(), ShouldEqual, 9)
})
}