-
Notifications
You must be signed in to change notification settings - Fork 6
/
call_test.go
146 lines (107 loc) · 3.93 KB
/
call_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
package protocols_test
import (
"errors"
"testing"
"github.com/myzhan/avroipc/mocks"
"github.com/stretchr/testify/require"
"github.com/myzhan/avroipc/protocols"
)
func prepareCallProtocol(t *testing.T) (protocols.CallProtocol, *mocks.MockProtocol) {
m := &mocks.MockProtocol{}
p, err := protocols.NewCall(m)
require.NoError(t, err)
return p, m
}
// Test successful schema compilation
func TestNewCall(t *testing.T) {
_, err := protocols.NewCall(nil)
require.NoError(t, err)
}
func TestCallProtocol_PrepareRequest(t *testing.T) {
datum := []byte{0xA, 0xB, 0xC}
message := []byte{0xD, 0xE, 0xF}
emptyMethod := ""
appendMethod := "append"
nilError := error(nil)
testError := errors.New("test error")
t.Run("empty method", func(t *testing.T) {
p, m := prepareCallProtocol(t)
m.On("PrepareMessage", emptyMethod, datum).Return(message, nilError).Once()
actual, err := p.PrepareRequest(emptyMethod, datum)
require.NoError(t, err)
require.Equal(t, []byte{0x0, 0x0, 0xD, 0xE, 0xF}, actual)
m.AssertExpectations(t)
})
t.Run("append method", func(t *testing.T) {
p, m := prepareCallProtocol(t)
m.On("PrepareMessage", appendMethod, datum).Return(message, nilError).Once()
actual, err := p.PrepareRequest(appendMethod, datum)
require.NoError(t, err)
require.Equal(t, []byte{0x0, 0xc, 0x61, 0x70, 0x70, 0x65, 0x6e, 0x64, 0xD, 0xE, 0xF}, actual)
m.AssertExpectations(t)
})
t.Run("protocol error", func(t *testing.T) {
p, m := prepareCallProtocol(t)
m.On("PrepareMessage", "append", datum).Return(message, testError).Once()
_, err := p.PrepareRequest("append", datum)
require.EqualError(t, err, "test error")
m.AssertExpectations(t)
})
}
func TestCallProtocol_ParseResponse(t *testing.T) {
status := "test status"
method := "test method"
rest := []byte{}
data := append([]byte{0xA, 0xB, 0xC}, rest...)
longRest := []byte{0xD, 0xE, 0xF}
longData := append([]byte{}, longRest...)
okResponse := append([]byte{0x0, 0x0}, data...)
badResponse := append([]byte{0x0, 0x2}, data...)
longResponse := append([]byte{0x0, 0x0}, longData...)
shortResponse := []byte{0x0}
errorResponse := append([]byte{0x0, 0x1}, data...)
errorLongResponse := append([]byte{0x0, 0x1}, longData...)
nilError := error(nil)
testError := errors.New("test error")
t.Run("success", func(t *testing.T) {
p, m := prepareCallProtocol(t)
m.On("ParseMessage", method, data).Return(status, rest, nilError).Once()
actual, err := p.ParseResponse(method, okResponse)
require.NoError(t, err)
require.Equal(t, status, actual)
m.AssertExpectations(t)
})
t.Run("bad flag", func(t *testing.T) {
p, m := prepareCallProtocol(t)
_, err := p.ParseResponse(method, badResponse)
require.EqualError(t, err, "cannot decode binary boolean: expected: Go byte(0) or byte(1); received: byte(2)")
m.AssertExpectations(t)
})
t.Run("short buffer", func(t *testing.T) {
p, m := prepareCallProtocol(t)
_, err := p.ParseResponse(method, shortResponse)
require.EqualError(t, err, "short buffer")
m.AssertExpectations(t)
})
t.Run("process error", func(t *testing.T) {
p, m := prepareCallProtocol(t)
m.On("ParseError", method, data).Return(rest, testError).Once()
_, err := p.ParseResponse(method, errorResponse)
require.EqualError(t, err, "test error")
m.AssertExpectations(t)
})
t.Run("buffer not empty", func(t *testing.T) {
p, m := prepareCallProtocol(t)
m.On("ParseMessage", method, longData).Return(status, longRest, nilError).Once()
_, err := p.ParseResponse(method, longResponse)
require.EqualError(t, err, "response buffer is not empty: len=3, rest=0x0D0E0F")
m.AssertExpectations(t)
})
t.Run("process error with non-empty buffer", func(t *testing.T) {
p, m := prepareCallProtocol(t)
m.On("ParseError", method, longData).Return(longRest, nilError).Once()
_, err := p.ParseResponse(method, errorLongResponse)
require.EqualError(t, err, "response buffer is not empty: len=3, rest=0x0D0E0F")
m.AssertExpectations(t)
})
}