forked from reo7sp/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed nil query for server for unknown request + improved raw marshal…
…ing of result
- Loading branch information
Showing
7 changed files
with
131 additions
and
27 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package tarantool | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestResultMarshaling(t *testing.T) { | ||
var result Result | ||
|
||
// The result of a call17 to: | ||
// function a() | ||
// return "a" | ||
// end | ||
tntBodyBytes := []byte{ | ||
0x81, // MP_MAP | ||
0x30, // key IPROTO_DATA | ||
0xdd, 0x0, 0x0, 0x0, 0x1, // MP_ARRAY | ||
0xa1, 0x61, // string value "a" | ||
} | ||
|
||
expectedDefaultMarshalerBytes := []byte{ | ||
0x81, // MP_MAP | ||
0x30, // key IPROTO_DATA | ||
0x91, // MP_ARRAY | ||
0x91, // MP_ARRAY | ||
0xa1, 0x61, // string value "a" | ||
} | ||
|
||
buf, err := result.UnmarshalMsg(tntBodyBytes) | ||
assert.NoError(t, err, "error unmarshaling result") | ||
assert.Empty(t, buf, "unmarshaling residual buffer is not empty") | ||
|
||
defaultMarshalerRes, err := result.MarshalMsg(nil) | ||
assert.NoError(t, err, "error marshaling by default marshaller") | ||
assert.Equal( | ||
t, | ||
expectedDefaultMarshalerBytes, | ||
defaultMarshalerRes, | ||
) | ||
|
||
result.WithBytesMarshaller() | ||
bytesMarshalerRes, err := result.MarshalMsg(nil) | ||
assert.NoError(t, err, "error marshaling by bytes marshaller") | ||
assert.Equal(t, tntBodyBytes, bytesMarshalerRes) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package tarantool | ||
|
||
type UnknownQuery struct { | ||
cmd uint | ||
data []byte | ||
} | ||
|
||
var _ Query = (*UnknownQuery)(nil) | ||
|
||
func NewUnknownQuery(cmd uint) *UnknownQuery { | ||
return &UnknownQuery{cmd: cmd} | ||
} | ||
|
||
func (q *UnknownQuery) GetCommandID() uint { | ||
return q.cmd | ||
} | ||
|
||
func (q *UnknownQuery) MarshalMsg(b []byte) ([]byte, error) { | ||
return append(b, q.data...), nil | ||
} | ||
|
||
// UnmarshalMsg saves all of the data into the query. | ||
// So make sure it doesn't contain part of another packet. | ||
func (q *UnknownQuery) UnmarshalMsg(data []byte) (buf []byte, err error) { | ||
q.data = make([]byte, len(data)) | ||
copy(q.data, data) | ||
|
||
return nil, nil | ||
} | ||
|
||
// IsKnownQuery returns true if passed query is known and supported. | ||
func IsKnownQuery(q Query) bool { | ||
_, unknown := q.(*UnknownQuery) | ||
|
||
return q != nil && !unknown | ||
} |