-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjsonproto_test.go
63 lines (55 loc) · 1.27 KB
/
jsonproto_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
package jsonproto_test
import (
"testing"
"time"
tp "github.com/henrylee2cn/teleport"
jsonproto "github.com/henrylee2cn/tp-ext/proto-jsonproto"
)
type Home struct {
tp.PullCtx
}
func (h *Home) Test(arg *map[string]interface{}) (map[string]interface{}, *tp.Rerror) {
h.Session().Push("/push/test", map[string]interface{}{
"your_id": h.Query().Get("peer_id"),
})
meta := h.CopyMeta()
return map[string]interface{}{
"arg": *arg,
"meta": meta.String(),
}, nil
}
func TestJsonProto(t *testing.T) {
// Server
srv := tp.NewPeer(tp.PeerConfig{ListenPort: 9090})
srv.RoutePull(new(Home))
go srv.ListenAndServe(jsonproto.NewJsonProtoFunc)
time.Sleep(1e9)
// Client
cli := tp.NewPeer(tp.PeerConfig{})
cli.RoutePush(new(Push))
sess, err := cli.Dial(":9090", jsonproto.NewJsonProtoFunc)
if err != nil {
t.Error(err)
}
var result interface{}
rerr := sess.Pull("/home/test?peer_id=110",
map[string]interface{}{
"bytes": []byte("test bytes"),
},
&result,
tp.WithAddMeta("add", "1"),
tp.WithXferPipe('g'),
).Rerror()
if rerr != nil {
t.Error(rerr)
}
t.Logf("result:%v", result)
time.Sleep(3e9)
}
type Push struct {
tp.PushCtx
}
func (p *Push) Test(arg *map[string]interface{}) *tp.Rerror {
tp.Infof("receive push(%s):\narg: %#v\n", p.Ip(), arg)
return nil
}