-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remaining response bytes returned by the call protocol is used now only to generate an error. But the call protocol is able to do it by itself so its interface may be simplified what was done in this PR. Additionally, all call protocol tests were refactored to use regular mocks and to be more accurate.
- Loading branch information
Showing
6 changed files
with
141 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,139 @@ | ||
package avroipc_test | ||
|
||
import ( | ||
"fmt" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/myzhan/avroipc" | ||
"github.com/myzhan/avroipc/mocks" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestCallProtocol_PrepareRequest(t *testing.T) { | ||
p, err := avroipc.NewCallProtocol(&mocks.MockProtocol{}) | ||
func prepareCallProtocol(t *testing.T) (avroipc.CallProtocol, *mocks.MockProtocol) { | ||
m := &mocks.MockProtocol{} | ||
|
||
p, err := avroipc.NewCallProtocol(m) | ||
require.NoError(t, err) | ||
|
||
return p, m | ||
} | ||
|
||
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) { | ||
actual, err := p.PrepareRequest("", nil) | ||
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}, actual) | ||
require.Equal(t, []byte{0x0, 0x0, 0xD, 0xE, 0xF}, actual) | ||
|
||
m.AssertExpectations(t) | ||
}) | ||
t.Run("append method", func(t *testing.T) { | ||
actual, err := p.PrepareRequest("append", nil) | ||
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}, actual) | ||
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, err := avroipc.NewCallProtocol(&mocks.MockProtocol{ | ||
Err: fmt.Errorf("test error"), | ||
}) | ||
require.NoError(t, err) | ||
p, m := prepareCallProtocol(t) | ||
m.On("PrepareMessage", "append", datum).Return(message, testError).Once() | ||
|
||
_, err = p.PrepareRequest("append", nil) | ||
_, 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) { | ||
expected := "test" | ||
p, err := avroipc.NewCallProtocol(&mocks.MockProtocol{ | ||
Response: expected, | ||
ParseResponseBytes: []byte{0x77, 0x78}, | ||
}) | ||
require.NoError(t, err) | ||
p, m := prepareCallProtocol(t) | ||
m.On("ParseMessage", method, data).Return(status, rest, nilError).Once() | ||
|
||
actual, bytes, err := p.ParseResponse("", []byte{0x0, 0x0}) | ||
actual, err := p.ParseResponse(method, okResponse) | ||
require.NoError(t, err) | ||
require.Equal(t, []byte{0x77, 0x78}, bytes) | ||
require.Equal(t, expected, actual) | ||
require.Equal(t, status, actual) | ||
|
||
m.AssertExpectations(t) | ||
}) | ||
|
||
t.Run("short buffer", func(t *testing.T) { | ||
expected := "test" | ||
p, err := avroipc.NewCallProtocol(&mocks.MockProtocol{ | ||
Response: expected, | ||
ParseResponseBytes: []byte{0x77, 0x78}, | ||
}) | ||
require.NoError(t, err) | ||
t.Run("bad flag", func(t *testing.T) { | ||
p, m := prepareCallProtocol(t) | ||
|
||
_, bytes, err := p.ParseResponse("", []byte{0x0}) | ||
require.EqualError(t, err, "short buffer") | ||
require.Equal(t, []byte{}, bytes) | ||
_, 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("bad flag", func(t *testing.T) { | ||
expected := "test" | ||
p, err := avroipc.NewCallProtocol(&mocks.MockProtocol{ | ||
Response: expected, | ||
ParseResponseBytes: []byte{0x77, 0x78}, | ||
}) | ||
require.NoError(t, err) | ||
t.Run("short buffer", func(t *testing.T) { | ||
p, m := prepareCallProtocol(t) | ||
|
||
_, err := p.ParseResponse(method, shortResponse) | ||
require.EqualError(t, err, "short buffer") | ||
|
||
_, bytes, err := p.ParseResponse("", []byte{0x0, 0x2}) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "cannot decode binary boolean") | ||
require.Equal(t, []byte{0x2}, bytes) | ||
m.AssertExpectations(t) | ||
}) | ||
|
||
t.Run("process error", func(t *testing.T) { | ||
expected := fmt.Errorf("test error") | ||
p, err := avroipc.NewCallProtocol(&mocks.MockProtocol{ | ||
Err: expected, | ||
ErrorResponseBytes: []byte{0x88, 0x89}, | ||
}) | ||
require.NoError(t, err) | ||
p, m := prepareCallProtocol(t) | ||
m.On("ParseError", method, data).Return(rest, testError).Once() | ||
|
||
_, bytes, err := p.ParseResponse("", []byte{0x0, 0x1}) | ||
_, err := p.ParseResponse(method, errorResponse) | ||
require.EqualError(t, err, "test error") | ||
require.Equal(t, []byte{0x88, 0x89}, bytes) | ||
|
||
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.