-
Notifications
You must be signed in to change notification settings - Fork 1
/
protocol_select_test.go
149 lines (121 loc) · 2.74 KB
/
protocol_select_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package tentacle
import (
"errors"
"net"
"testing"
)
func TestSelectVersion(t *testing.T) {
a := []string{
"1.0.0",
"1.1.1",
"2.0.0",
}
b := []string{
"1.0.0",
"2.0.0",
"3.0.0",
}
c := []string{}
d := []string{"5.0.0"}
e := []string{"1.0.0"}
var res string
var err error
res, err = SelectVersion(b, a)
if res != "2.0.0" {
t.Fatal("b, a != 2.0.0")
}
res, err = SelectVersion(b, e)
if res != "1.0.0" {
t.Fatal("b, e != 1.0.0")
}
res, err = SelectVersion(b, c)
if err == nil {
t.Fatal("b, c != nil")
}
res, err = SelectVersion(b, d)
if err == nil {
t.Fatal("b, d != nil")
}
res, err = SelectVersion(d, a)
if err == nil {
t.Fatal("d, a != nil")
}
res, err = SelectVersion(d, e)
if err == nil {
t.Fatal("d, e != nil")
}
res, err = SelectVersion(e, d)
if err == nil {
t.Fatal("e, d != nil")
}
}
func TestProtocolInfoDecodeEncode(t *testing.T) {
info := new(ProtocolInfo)
info.name = "test"
info.supportVersion = []string{"1.0.0", "2.0.0"}
b := info.encode()
decode, err := decodeToProtocolInfo(b)
if err != nil {
t.Fatal(err)
}
if info.name != decode.name {
t.Fatal("name must eq")
}
if len(info.supportVersion) != len(decode.supportVersion) {
t.Fatal("supportVersion len must eq")
}
for i, v := range info.supportVersion {
if decode.supportVersion[i] != v {
t.Fatal("supportVersion must eq")
}
}
}
func selectProtocol(server, client []string, result string) error {
serverInfo := new(ProtocolInfo)
serverInfo.name = "test"
serverInfo.supportVersion = server
infoI := info{inner: *serverInfo, fn: SelectVersion}
serverInfos := map[string]info{"test": infoI}
clientInfo := new(ProtocolInfo)
clientInfo.name = "test"
clientInfo.supportVersion = client
resChan := make(chan string)
addrChan := make(chan net.Addr)
go func() {
listener, _ := net.Listen("tcp", ":0")
addrChan <- listener.Addr()
defer listener.Close()
conn, _ := listener.Accept()
_, res, _ := serverSelect(conn, serverInfos)
resChan <- res
}()
addr := <-addrChan
conn, _ := net.Dial("tcp", addr.String())
_, res, _ := clientSelect(conn, *clientInfo)
serverRet := <-resChan
if res != serverRet {
return errors.New("client server don't match")
}
if res != result {
return errors.New("result don't match")
}
return nil
}
func TestSelectSame(t *testing.T) {
err := selectProtocol([]string{"1.0.0", "1.1.1"}, []string{"1.0.0", "1.1.1"}, "1.1.1")
if err != nil {
t.Fatal(err)
}
}
func TestSelectDiff(t *testing.T) {
err := selectProtocol([]string{"1.0.0", "2.1.1"}, []string{"1.0.0", "1.1.1"}, "1.0.0")
if err != nil {
t.Fatal(err)
}
}
func TestSelectFail(t *testing.T) {
err := selectProtocol([]string{"1.0.0", "1.1.1"}, []string{"2.0.0", "2.1.1"}, "")
if err != nil {
t.Fatal(err)
}
}