forked from TrueCloudLab/frostfs-api-go
[#263] Define generic protobuf message
Define `Message` interface of the generic protobuf message. In the initial implementation, the message should be convertible to and from related gRPC message. Implement encoding functions over the `Message` that can: * unmarshal the `Message` via related gRPC message; * decode the `Message` from JSON format via related gRPC message; * encode the `Message` to JSON. Implement nested `test` package for testing the implementation. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
cf765a61a6
commit
c61656a43f
3 changed files with 159 additions and 0 deletions
68
rpc/message/test/message.go
Normal file
68
rpc/message/test/message.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
package messagetest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/rpc/message"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type jsonMessage interface {
|
||||
json.Marshaler
|
||||
json.Unmarshaler
|
||||
}
|
||||
|
||||
type binaryMessage interface {
|
||||
StableMarshal([]byte) ([]byte, error)
|
||||
Unmarshal([]byte) error
|
||||
}
|
||||
|
||||
func TestRPCMessage(t *testing.T, msgGens ...func(empty bool) message.Message) {
|
||||
for _, msgGen := range msgGens {
|
||||
msg := msgGen(false)
|
||||
|
||||
t.Run(fmt.Sprintf("convert_%T", msg), func(t *testing.T) {
|
||||
msg := msgGen(false)
|
||||
|
||||
err := msg.FromGRPCMessage(100)
|
||||
|
||||
require.True(t, errors.As(err, new(message.ErrUnexpectedMessageType)))
|
||||
|
||||
msg2 := msgGen(true)
|
||||
|
||||
err = msg2.FromGRPCMessage(msg.ToGRPCMessage())
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, msg, msg2)
|
||||
})
|
||||
|
||||
t.Run("encoding", func(t *testing.T) {
|
||||
if jm, ok := msg.(jsonMessage); ok {
|
||||
t.Run(fmt.Sprintf("JSON_%T", msg), func(t *testing.T) {
|
||||
data, err := jm.MarshalJSON()
|
||||
require.NoError(t, err)
|
||||
|
||||
jm2 := msgGen(true).(jsonMessage)
|
||||
require.NoError(t, jm2.UnmarshalJSON(data))
|
||||
|
||||
require.Equal(t, jm, jm2)
|
||||
})
|
||||
}
|
||||
|
||||
if bm, ok := msg.(binaryMessage); ok {
|
||||
t.Run(fmt.Sprintf("Binary_%T", msg), func(t *testing.T) {
|
||||
data, err := bm.StableMarshal(nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
bm2 := msgGen(true).(binaryMessage)
|
||||
require.NoError(t, bm2.Unmarshal(data))
|
||||
|
||||
require.Equal(t, bm, bm2)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue