2018-03-19 17:14:50 +00:00
|
|
|
package mrpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
. "testing"
|
|
|
|
|
2018-07-03 00:20:00 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mrand"
|
2018-03-19 17:14:50 +00:00
|
|
|
"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())
|
|
|
|
return resT{Out: args.In}, nil
|
|
|
|
}))
|
|
|
|
|
|
|
|
{ // test with arg being non-pointer
|
2018-07-03 00:20:00 +00:00
|
|
|
in := mrand.Hex(8)
|
2018-03-19 17:14:50 +00:00
|
|
|
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
|
2018-07-03 00:20:00 +00:00
|
|
|
in := mrand.Hex(8)
|
2018-03-19 17:14:50 +00:00
|
|
|
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())
|
|
|
|
return &resT{Out: args.In}, nil
|
|
|
|
}))
|
|
|
|
|
|
|
|
{ // test with arg being non-pointer
|
2018-07-03 00:20:00 +00:00
|
|
|
in := mrand.Hex(8)
|
2018-03-19 17:14:50 +00:00
|
|
|
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
|
2018-07-03 00:20:00 +00:00
|
|
|
in := mrand.Hex(8)
|
2018-03-19 17:14:50 +00:00
|
|
|
var res resT
|
|
|
|
assert.NoError(t, client.CallRPC(ctx, &res, "foo", &argT{In: in}))
|
|
|
|
assert.Equal(t, in, res.Out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|