mediocre-go-lib/mrpc/mrpc_test.go

68 lines
1.5 KiB
Go
Raw Normal View History

2018-03-19 17:14:50 +00:00
package mrpc
import (
"context"
. "testing"
"github.com/mediocregopher/mediocre-go-lib/mtest"
"github.com/stretchr/testify/assert"
)
func TestReflectClient(t *T) {
type argT struct {
In string
}
type resT struct {
Out string
}
ctx := context.Background()
{ // test with handler returning non-pointer
client := ReflectClient(HandlerFunc(func(c Call) (interface{}, error) {
var args argT
assert.NoError(t, c.UnmarshalArgs(&args))
assert.Equal(t, "foo", c.Method)
2018-03-19 17:14:50 +00:00
return resT{Out: args.In}, nil
}))
{ // test with arg being non-pointer
in := mtest.RandHex(8)
var res resT
assert.NoError(t, client.CallRPC(ctx, &res, "foo", argT{In: in}))
assert.Equal(t, in, res.Out)
}
{ // test with arg being pointer
in := mtest.RandHex(8)
var res resT
assert.NoError(t, client.CallRPC(ctx, &res, "foo", &argT{In: in}))
assert.Equal(t, in, res.Out)
}
}
{ // test with handler returning pointer
client := ReflectClient(HandlerFunc(func(c Call) (interface{}, error) {
var args argT
assert.NoError(t, c.UnmarshalArgs(&args))
assert.Equal(t, "foo", c.Method)
2018-03-19 17:14:50 +00:00
return &resT{Out: args.In}, nil
}))
{ // test with arg being non-pointer
in := mtest.RandHex(8)
var res resT
assert.NoError(t, client.CallRPC(ctx, &res, "foo", argT{In: in}))
assert.Equal(t, in, res.Out)
}
{ // test with arg being pointer
in := mtest.RandHex(8)
var res resT
assert.NoError(t, client.CallRPC(ctx, &res, "foo", &argT{In: in}))
assert.Equal(t, in, res.Out)
}
}
}