forked from weibocom/motan-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
76 lines (65 loc) · 1.91 KB
/
server_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
package motan
import (
"bytes"
"fmt"
assert2 "github.com/stretchr/testify/assert"
"github.com/weibocom/motan-go/config"
motan "github.com/weibocom/motan-go/core"
"testing"
"time"
)
func TestNewMotanServerContextFromConfig(t *testing.T) {
assert := assert2.New(t)
ext := startServer(t, "helloService", 64532)
clientExt := GetDefaultExtFactory()
u := motan.FromExtInfo("motan2://127.0.0.1:64532/helloService?serialization=simple")
assert.NotNil(u)
ep := clientExt.GetEndPoint(u)
assert.NotNil(ep)
ep.SetSerialization(motan.GetSerialization(u, ext))
motan.Initialize(ep)
// wait ha
time.Sleep(time.Second * 1)
request := newRequest("helloService", "hello", "Ray")
request.Attachment = motan.NewStringMap(motan.DefaultAttachmentSize)
resp := ep.Call(request)
assert.Nil(resp.GetException())
assert.Equal("Hello Ray from motan server", resp.GetValue())
}
func startServer(t *testing.T, path string, port int) motan.ExtensionFactory {
cfgText := `
motan-server:
log_dir: "stdout"
application: "app-golang" # server identify.
motan-registry:
direct:
protocol: direct
#conf of services
motan-service:
mytest-motan2:
path: %s
group: bj
protocol: motan2
registry: direct
serialization: simple
ref : "serviceID"
export: "motan2:%d"
`
cfgText = fmt.Sprintf(cfgText, path, port)
assert := assert2.New(t)
conf, err := config.NewConfigFromReader(bytes.NewReader([]byte(cfgText)))
assert.Nil(err)
ext := GetDefaultExtFactory()
mscontext := NewMotanServerContextFromConfig(conf)
err = mscontext.RegisterService(&HelloService{}, "serviceID")
assert.Nil(err)
mscontext.Start(ext)
mscontext.ServicesAvailable()
service := motan.FromExtInfo(fmt.Sprintf("motan2://127.0.0.1:%d/%s?serialization=simple", port, path))
assert.NotNil(service)
return ext
}
type HelloService struct{}
func (m *HelloService) Hello(name string) string {
return fmt.Sprintf("Hello %s from motan server", name)
}