-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_test.go
53 lines (44 loc) · 1.02 KB
/
service_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package gorpc
import (
"fmt"
"reflect"
"testing"
"github.com/sirupsen/logrus"
)
type Foo int
type Args struct {
a int
b int
}
func (f *Foo) Sum(arg Args, reply *int) error {
*reply = arg.a + arg.b
return nil
}
func (f *Foo) sum(arg Args, reply *int) error {
*reply = arg.a + arg.b
return nil
}
func _assert(condition bool, errstr string, v ...interface{}) {
if !condition {
panic(fmt.Sprintf("assert failed: "+errstr, v...))
}
}
func TestNewService(t *testing.T) {
var foo Foo
s := NewService(&foo)
logrus.Info(s)
_assert(len(s.method) == 1, "wrong service number, expected 1, got %d", len(s.method))
methodType := s.method["Sum"]
_assert(methodType != nil, "wrong method")
}
func TestMethodType_Call(t *testing.T) {
var foo Foo
s := NewService(&foo)
mt := s.method["Sum"]
argv := mt.newArgv()
replyv := mt.newReplyv()
logrus.Info(mt)
argv.Set(reflect.ValueOf(Args{1, 2}))
err := s.Call(mt, argv, replyv)
_assert(err == nil && *replyv.Interface().(*int) == 3 && mt.numCalls == 1, "fail to call Foo.Sum")
}