frostfs-api-go/rpc/message/test/message.go
Evgenii Stratonikov 7133a01ccf
All checks were successful
Tests and linters / Tests (1.19) (pull_request) Successful in 54s
Tests and linters / Lint (pull_request) Successful in 1m44s
Tests and linters / Tests (1.20) (pull_request) Successful in 2m28s
Tests and linters / Tests with -race (pull_request) Successful in 4m46s
[#49] message/test: Add test for zero-alloc StableSize()
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
2023-07-26 18:32:17 +03:00

73 lines
1.6 KiB
Go

package messagetest
import (
"encoding/json"
"errors"
"fmt"
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message"
"github.com/stretchr/testify/require"
)
type jsonMessage interface {
json.Marshaler
json.Unmarshaler
}
type binaryMessage interface {
StableMarshal([]byte) []byte
StableSize() int
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("%T.StableSize() does no allocations", bm), func(t *testing.T) {
require.Zero(t, testing.AllocsPerRun(1000, func() {
_ = bm.StableSize()
}))
})
t.Run(fmt.Sprintf("Binary_%T", msg), func(t *testing.T) {
data := bm.StableMarshal(nil)
bm2 := msgGen(true).(binaryMessage)
require.NoError(t, bm2.Unmarshal(data))
require.Equal(t, bm, bm2)
})
}
})
}
}