mirror of
https://github.com/geektutu/7days-golang.git
synced 2024-04-21 12:32:11 +00:00
49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
package geerpc
|
|
|
|
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
|
|
}
|
|
|
|
// it's not a exported Method
|
|
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, v...))
|
|
}
|
|
}
|
|
|
|
func TestNewService(t *testing.T) {
|
|
var foo Foo
|
|
s := newService(&foo)
|
|
_assert(len(s.method) == 1, "wrong service Method, expect 1, but got %d", len(s.method))
|
|
mType := s.method["Sum"]
|
|
_assert(mType != nil, "wrong Method, Sum shouldn't nil")
|
|
}
|
|
|
|
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)
|
|
_assert(err == nil && *replyv.Interface().(*int) == 4 && mType.NumCalls() == 1, "failed to call Foo.Sum")
|
|
}
|