-
Notifications
You must be signed in to change notification settings - Fork 1
/
codec_server_test.go
92 lines (88 loc) · 2.13 KB
/
codec_server_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
// Copyright (c) 2019 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package rpc
import (
"github.com/hslam/socket"
"sync"
"testing"
)
func TestNewServerCodec(t *testing.T) {
network := "tcp"
addr := ":9999"
wg := sync.WaitGroup{}
wg.Add(1)
sock, _ := socket.NewSocket(network, nil)
lis, _ := sock.Listen(addr)
go func() {
defer wg.Done()
conn, _ := lis.Accept()
message := conn.Messages()
headerEncoder := NewHeaderEncoder("json")
if NewServerCodec(nil, nil, nil, false, bufferSize) != nil {
t.Error("should be nil")
}
if NewServerCodec(nil, nil, message, false, bufferSize) != nil {
t.Error("should be nil")
}
codec := NewServerCodec(nil, headerEncoder(), message, false, bufferSize)
if codec == nil {
t.Error("should not be nil")
}
if codec.ReadRequestBody(nil, nil) == nil {
t.Error("The err should not be nil")
}
wg.Add(1)
go func() {
defer wg.Done()
ServeCodec(codec)
}()
message.Close()
lis.Close()
codec.ReadRequestHeader(&Context{})
codec.Close()
if codec.ReadRequestHeader(nil) == nil {
t.Error("The err should not be nil")
}
if codec.WriteResponse(nil, nil) == nil {
t.Error("The err should not be nil")
}
}()
sock.Dial(addr)
wg.Wait()
}
func TestNewServerCodecNoBatch(t *testing.T) {
network := "tcp"
addr := ":9999"
wg := sync.WaitGroup{}
wg.Add(1)
sock, _ := socket.NewSocket(network, nil)
lis, _ := sock.Listen(addr)
go func() {
defer wg.Done()
conn, _ := lis.Accept()
message := conn.Messages()
headerEncoder := NewHeaderEncoder("json")
if NewServerCodec(nil, nil, nil, true, bufferSize) != nil {
t.Error("should be nil")
}
if NewServerCodec(nil, nil, message, true, bufferSize) != nil {
t.Error("should be nil")
}
codec := NewServerCodec(nil, headerEncoder(), message, true, bufferSize)
if codec == nil {
t.Error("should not be nil")
}
if codec.ReadRequestBody(nil, nil) == nil {
t.Error("The err should not be nil")
}
wg.Add(1)
go func() {
defer wg.Done()
ServeCodec(codec)
}()
message.Close()
lis.Close()
}()
sock.Dial(addr)
wg.Wait()
}