feat: implement server and client
This commit is contained in:
48
service_test.go
Normal file
48
service_test.go
Normal file
@ -0,0 +1,48 @@
|
||||
package krpc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type Foo int
|
||||
|
||||
type Args struct{ Num1, Num2 int }
|
||||
|
||||
func (f Foo) Sum(args Args, reply *int) error {
|
||||
*reply = args.Num1 + args.Num2
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f Foo) sum(args Args, reply *int) error {
|
||||
*reply = args.Num1 + args.Num2
|
||||
return nil
|
||||
}
|
||||
|
||||
func _assert(condition bool, msg string, v ...interface{}) {
|
||||
if !condition {
|
||||
panic(fmt.Sprintf("assertion failed: "+msg+"\n", v...))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewService(t *testing.T) {
|
||||
var foo Foo
|
||||
s := newService(&foo)
|
||||
want := 1
|
||||
_assert(len(s.method) == want, "wrong service Method, expect %d, got %d", want, len(s.method))
|
||||
}
|
||||
|
||||
func TestMethodType_Call(t *testing.T) {
|
||||
var foo Foo
|
||||
s := newService(&foo)
|
||||
mType := s.method["Sum"]
|
||||
|
||||
argv := mType.newArgv()
|
||||
replyv := mType.newReplyv()
|
||||
argv.Set(reflect.ValueOf(Args{Num1: 1, Num2: 3}))
|
||||
err := s.call(mType, argv, replyv)
|
||||
want := 4
|
||||
_assert(err == nil && *replyv.Interface().(*int) == want &&
|
||||
mType.NumCalls() == 1, "failed to call Foo.Sum")
|
||||
}
|
Reference in New Issue
Block a user