-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_test.go
54 lines (40 loc) · 1.12 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"
)
type Foo int
type Args struct{Num1, Num2 int}
// test exported method
func(f Foo)Sum(args Args,reply *int)error{
*reply = args.Num1 + args.Num2
return nil
}
// test not exported method - should not be register as an service
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 should not be 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: 2}))
err := s.call(mType,argv,replyv)
_assert(err == nil && *replyv.Interface().(*int) == 3 && mType.NumCalls() == 1,"Function is not returning correct value")
}