-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
60 lines (54 loc) · 1.62 KB
/
main_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
package main
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestSuccess(t *testing.T) {
r := require.New(t)
testCases := []struct {
req RPCRequest
buf []byte
}{
{RPCRequest{method: Hello, connID: [12]byte{}}, []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{RPCRequest{method: Busy, connID: [12]byte{}}, []byte{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{RPCRequest{method: Ping, connID: [12]byte{}}, []byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{RPCRequest{method: Pong, connID: [12]byte{}}, []byte{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{RPCRequest{method: Connect, connID: [12]byte{44}}, []byte{5, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{RPCRequest{method: Ack, connID: [12]byte{55}}, []byte{6, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
}
for i := range testCases {
tc := testCases[i]
t.Run("", func(t *testing.T) {
buf := tc.req.Encode()
r.Equal(tc.buf, buf)
req := RPCRequest{}
err := req.Decode(buf)
r.NoError(err)
r.Equal(tc.req, req)
})
}
}
func TestBufIsNil(t *testing.T) {
r := require.New(t)
req := RPCRequest{}
err := req.Decode(nil)
r.Error(err)
r.ErrorIs(err, ErrSerialization)
r.ErrorContains(err, "buf is nil")
}
func TestIncorrectBufLength(t *testing.T) {
r := require.New(t)
req := RPCRequest{}
err := req.Decode([]byte{0})
r.Error(err)
r.ErrorIs(err, ErrSerialization)
r.ErrorContains(err, "bad request size")
}
func TestMethodIsZero(t *testing.T) {
r := require.New(t)
req := RPCRequest{}
err := req.Decode([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
r.Error(err)
r.ErrorIs(err, ErrSerialization)
r.ErrorContains(err, "rpc method is zero")
}