-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistener_test.go
More file actions
73 lines (68 loc) · 1.33 KB
/
listener_test.go
File metadata and controls
73 lines (68 loc) · 1.33 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
package raknet
import (
"io"
"testing"
)
type ConnReadOperation struct {
ReadBytes int
ExpectedData string
}
type ConnReadTestCase struct {
Packets []string
Reads []*ConnReadOperation
}
var (
ConnReadTestCases = []*ConnReadTestCase{
{
[]string{"first", "second"},
[]*ConnReadOperation{
{5, "first"},
{6, "second"},
},
},
{
[]string{"first", "second"},
[]*ConnReadOperation{
{4, "firs"},
{7, "tsecond"},
},
},
{
[]string{"first", "second"},
[]*ConnReadOperation{
{6, "firsts"},
{5, "econd"},
},
},
{
[]string{"first", "", "second"},
[]*ConnReadOperation{
{6, "firsts"},
{5, "econd"},
},
},
}
)
func TestConnRead(t *testing.T) {
for _, testcase := range ConnReadTestCases {
conn := &Conn{
buff: []byte{},
buffReadIdx: 0,
chData: make(chan []byte, ConnPacketBuffer),
}
for _, packet := range testcase.Packets {
conn.chData <- []byte(packet)
}
for _, read := range testcase.Reads {
buff := make([]byte, read.ReadBytes)
if _, err := io.ReadFull(conn, buff); err != nil {
t.Errorf("Conn.Read expects \"%s\", got error", string(read.ExpectedData), err)
return
}
if string(buff) != read.ExpectedData {
t.Errorf("Conn.Read expects \"%s\", got %s", string(read.ExpectedData), string(buff))
return
}
}
}
}