forked from fuyao-w/papillon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_proto_test.go
63 lines (57 loc) · 1.15 KB
/
net_proto_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
package papillon
import (
"bufio"
"fmt"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
type testRW struct {
buf []byte
}
func (t *testRW) Read(p []byte) (n int, err error) {
copy(p, t.buf)
if len(p) < len(t.buf) {
t.buf = t.buf[len(p):]
n = len(p)
return
}
n = len(t.buf)
t.buf = nil
return
}
func (t *testRW) Write(p []byte) (n int, err error) {
t.buf = append(t.buf, p...)
return len(p), nil
}
func TestProto(t *testing.T) {
rw := &testRW{}
testCase := []struct {
typ rpcType
data string
}{
{
1, string(make([]byte, 1024*4)),
}, {
100, "",
},
}
for i, s := range testCase {
Convey(fmt.Sprintf("TestProto :%d", i), t, func() {
w := bufio.NewWriter(rw)
So(defaultPackageParser.Encode(w, s.typ, []byte(s.data)), ShouldBeNil)
w.Flush()
r := bufio.NewReader(rw)
typ, data, err := defaultPackageParser.Decode(r)
So(err, ShouldBeNil)
So(typ, ShouldEqual, s.typ)
So(string(data), ShouldEqual, s.data)
})
}
rw = &testRW{
buf: []byte("!"),
}
_, _, err := defaultPackageParser.Decode(bufio.NewReader(rw))
Convey("test magic ", t, func() {
So(err, ShouldEqual, errUnrecognizedRequest)
})
}