MCPcopy
hub / github.com/aceld/zinx / TestDataPack

Function TestDataPack

zpack/datapack_tlv_bigendian_test.go:17–118  ·  view source on GitHub ↗

run in terminal: go test -v ./znet -run=TestDataPack This function is responsible for testing the functionality of data packet splitting and packaging.

(t *testing.T)

Source from the content-addressed store, hash-verified

15
16// This function is responsible for testing the functionality of data packet splitting and packaging.
17func TestDataPack(t *testing.T) {
18 // Create a TCP server socket.
19 listener, err := net.Listen("tcp", "127.0.0.1:7777")
20 if err != nil {
21 fmt.Println("server listen err:", err)
22 return
23 }
24
25 // Create a server goroutine, responsible for reading and parsing the data from the client goroutine that may contain sticky packets.
26 go func() {
27 for {
28 conn, err := listener.Accept()
29 if err != nil {
30 fmt.Println("server accept err:", err)
31 }
32
33 // Handle client requests
34 go func(conn net.Conn) {
35 // Create a packet splitting and packaging object dp.
36 dp := Factory().NewPack(ziface.ZinxDataPack)
37 for {
38 // 1. Read the head part of the stream first.
39 headData := make([]byte, dp.GetHeadLen())
40 _, err := io.ReadFull(conn, headData) // ReadFull will fill msg until it's full
41 if err != nil {
42 fmt.Println("read head error")
43 }
44 // Unpack the headData byte stream into msg.
45 msgHead, err := dp.Unpack(headData)
46 if err != nil {
47 fmt.Println("server unpack err:", err)
48 return
49 }
50
51 if msgHead.GetDataLen() > 0 {
52 // msg has data, read data again.
53 msg := msgHead.(*Message)
54 msg.Data = make([]byte, msg.GetDataLen())
55
56 // Read the byte stream from io based on dataLen.
57 _, err := io.ReadFull(conn, msg.Data)
58 if err != nil {
59 fmt.Println("server unpack data err:", err)
60 return
61 }
62
63 fmt.Println("==> Recv Msg: ID=", msg.ID, ", len=", msg.DataLen, ", data=", string(msg.Data))
64 }
65 }
66 }(conn)
67 }
68 }()
69
70 // Client goroutine, responsible for simulating data containing sticky packets and sending it to the server.
71 go func() {
72 conn, err := net.Dial("tcp", "127.0.0.1:7777")
73 if err != nil {
74 fmt.Println("client dial err:", err)

Callers

nothing calls this directly

Calls 7

FactoryFunction · 0.85
NewPackMethod · 0.80
WriteMethod · 0.80
GetHeadLenMethod · 0.65
UnpackMethod · 0.65
GetDataLenMethod · 0.65
PackMethod · 0.65

Tested by

no test coverage detected