-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrpc_test.go
51 lines (44 loc) · 908 Bytes
/
rpc_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
package alephzero
import (
"sync"
"testing"
)
func TestRpc(t *testing.T) {
FileRemove("alephzero/foo.rpc.a0")
topic := RpcTopic{"foo", nil}
cnd := sync.NewCond(&sync.Mutex{})
gotReply := false
gotCancel := false
rs, err := NewRpcServer(
topic,
func(req RpcRequest) {
if string(req.Packet().Payload) == "reply" {
req.Reply(NewPacket(nil, []byte("echo reply")))
}
},
func(id string) {
cnd.L.Lock()
gotCancel = true
cnd.Signal()
cnd.L.Unlock()
})
check(t, err)
defer rs.Close()
rc, err := NewRpcClient(topic)
check(t, err)
defer rc.Close()
check(t, rc.Send(NewPacket(nil, []byte("reply")), func(resp Packet) {
cnd.L.Lock()
gotReply = true
cnd.Signal()
cnd.L.Unlock()
}))
pkt := NewPacket(nil, []byte("cancel"))
check(t, rc.Send(pkt, nil))
check(t, rc.Cancel(pkt.Id))
cnd.L.Lock()
for !gotReply || !gotCancel {
cnd.Wait()
}
cnd.L.Unlock()
}