forked from weibocom/motan-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
80 lines (73 loc) · 1.72 KB
/
client_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package motan
import (
"bytes"
"fmt"
assert2 "github.com/stretchr/testify/assert"
"github.com/weibocom/motan-go/config"
"testing"
"time"
)
func TestNewClientContextFromConfig(t *testing.T) {
assert := assert2.New(t)
StartServer()
cfgText := `
motan-client:
log_dir: "stdout"
application: "app-golang"
motan-registry:
direct:
protocol: direct
host: 127.0.0.1
port: 64531
motan-refer:
mytest:
path: helloService
group: bj
protocol: motan2
registry: direct
serialization: simple
`
conf, err := config.NewConfigFromReader(bytes.NewReader([]byte(cfgText)))
assert.Nil(err)
ext := GetDefaultExtFactory()
mccontext := NewClientContextFromConfig(conf)
mccontext.Start(ext)
time.Sleep(time.Second)
mclient := mccontext.GetClient("mytest")
var reply string
req := mclient.BuildRequest("hello", []interface{}{"Ray"})
err = mclient.BaseCall(req, &reply) // sync call
assert.Nil(err)
assert.Equal("Hello Ray", reply)
}
func StartServer() {
cfgText := `
motan-server:
log_dir: "stdout"
application: "app-golang" # server identify.
motan-registry:
direct:
protocol: direct
#conf of services
motan-service:
mytest-motan2:
path: helloService
group: bj
protocol: motan2
registry: direct
serialization: simple
ref : "serviceID"
export: "motan2:64531"
`
conf, _ := config.NewConfigFromReader(bytes.NewReader([]byte(cfgText)))
ext := GetDefaultExtFactory()
mscontext := NewMotanServerContextFromConfig(conf)
mscontext.RegisterService(&HelloService1{}, "serviceID")
mscontext.Start(ext)
mscontext.ServicesAvailable()
time.Sleep(time.Second * 3)
}
type HelloService1 struct{}
func (m *HelloService1) Hello(name string) string {
return fmt.Sprintf("Hello %s", name)
}