rpc/message: Add compatibility tests for marshaling

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2024-08-27 10:50:15 +03:00
parent 9e82a5a31a
commit 83b9d5e612

View file

@ -7,6 +7,7 @@ import (
"testing" "testing"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto/encoding"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -46,6 +47,12 @@ func TestRPCMessage(t *testing.T, msgGens ...func(empty bool) message.Message) {
data, err := jm.MarshalJSON() data, err := jm.MarshalJSON()
require.NoError(t, err) require.NoError(t, err)
t.Run("compatibility", func(t *testing.T) {
data1, err := msg.ToGRPCMessage().(jsonMessage).MarshalJSON()
require.NoError(t, err)
require.JSONEq(t, string(data), string(data1))
})
jm2 := msgGen(true).(jsonMessage) jm2 := msgGen(true).(jsonMessage)
require.NoError(t, jm2.UnmarshalJSON(data)) require.NoError(t, jm2.UnmarshalJSON(data))
@ -62,12 +69,79 @@ func TestRPCMessage(t *testing.T, msgGens ...func(empty bool) message.Message) {
t.Run(fmt.Sprintf("Binary_%T", msg), func(t *testing.T) { t.Run(fmt.Sprintf("Binary_%T", msg), func(t *testing.T) {
data := bm.StableMarshal(nil) data := bm.StableMarshal(nil)
t.Run("compatibility", func(t *testing.T) {
data1 := msg.ToGRPCMessage().(encoding.ProtoMarshaler).MarshalProtobuf(nil)
if len(data) == 0 {
require.Empty(t, data1)
} else {
require.Equal(t, data, data1)
}
})
bm2 := msgGen(true).(binaryMessage) bm2 := msgGen(true).(binaryMessage)
require.NoError(t, bm2.Unmarshal(data)) require.NoError(t, bm2.Unmarshal(data))
require.Equal(t, bm, bm2) require.Equal(t, bm, bm2)
}) })
} }
t.Run("compatibility", func(t *testing.T) {
testCompatibility(t, msgGen)
})
}) })
} }
} }
func testCompatibility(t *testing.T, msgGen func(empty bool) message.Message) {
compareBinary := func(t *testing.T, msg message.Message) {
am, ok := msg.(binaryMessage)
if !ok {
t.Skip()
}
a := am.StableMarshal(nil)
b := msg.ToGRPCMessage().(encoding.ProtoMarshaler).MarshalProtobuf(nil)
if len(a) == 0 {
require.Empty(t, b)
} else {
require.Equal(t, a, b)
}
}
compareJSON := func(t *testing.T, msg message.Message) {
am, ok := msg.(jsonMessage)
if !ok {
t.Skip()
}
a, err := am.MarshalJSON()
require.NoError(t, err)
b, err := json.Marshal(msg.ToGRPCMessage())
require.NoError(t, err)
require.JSONEq(t, string(a), string(b))
if len(a) == 0 {
require.Empty(t, b)
} else {
require.Equal(t, a, b)
}
}
t.Run("empty", func(t *testing.T) {
t.Skip()
msg := msgGen(true)
t.Run(fmt.Sprintf("Binary_%T", msg), func(t *testing.T) {
compareBinary(t, msg)
})
t.Run(fmt.Sprintf("JSON_%T", msg), func(t *testing.T) {
compareJSON(t, msg)
})
})
t.Run("not empty", func(t *testing.T) {
msg := msgGen(false)
t.Run(fmt.Sprintf("Binary_%T", msg), func(t *testing.T) {
compareBinary(t, msg)
})
t.Run(fmt.Sprintf("JSON_%T", msg), func(t *testing.T) {
compareJSON(t, msg)
})
})
}