-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrpc_test.go
119 lines (102 loc) · 2.92 KB
/
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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package gorums_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/relab/gorums"
"github.com/relab/gorums/tests/dummy"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func TestRPCCallSuccess(t *testing.T) {
addrs, teardown := gorums.TestSetup(t, 1, func(_ int) gorums.ServerIface {
return initServer()
})
defer teardown()
mgr := gorumsTestMgr()
_, err := mgr.NewConfiguration(gorums.WithNodeList(addrs))
if err != nil {
t.Fatal(err)
}
node := mgr.Nodes()[0]
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
response, err := node.RPCCall(ctx, gorums.CallData{
Message: &dummy.Empty{},
Method: "dummy.Dummy.Test",
})
if err != nil {
t.Fatalf("Unexpected error, got: %v, want: %v", err, nil)
}
if response == nil {
t.Fatalf("Unexpected response, got: %v, want: %v", nil, &dummy.Empty{})
}
}
func TestRPCCallDownedNode(t *testing.T) {
addrs, teardown := gorums.TestSetup(t, 1, func(_ int) gorums.ServerIface {
return initServer()
})
mgr := gorumsTestMgr()
_, err := mgr.NewConfiguration(gorums.WithNodeList(addrs))
if err != nil {
t.Fatal(err)
}
teardown() // stop all servers on purpose
time.Sleep(300 * time.Millisecond) // servers are not stopped immediately
node := mgr.Nodes()[0]
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
response, err := node.RPCCall(ctx, gorums.CallData{
Message: &dummy.Empty{},
Method: "dummy.Dummy.Test",
})
if err == nil {
t.Fatalf("Expected error, got: %v, want: %v", err, fmt.Errorf("rpc error: code = Unavailable desc = stream is down"))
}
if response != nil {
t.Fatalf("Unexpected response, got: %v, want: %v", response, nil)
}
}
func TestRPCCallTimedOut(t *testing.T) {
addrs, teardown := gorums.TestSetup(t, 1, func(_ int) gorums.ServerIface {
return initServer()
})
defer teardown()
mgr := gorumsTestMgr()
_, err := mgr.NewConfiguration(gorums.WithNodeList(addrs))
if err != nil {
t.Fatal(err)
}
node := mgr.Nodes()[0]
ctx, cancel := context.WithTimeout(context.Background(), 0*time.Second)
time.Sleep(50 * time.Millisecond)
defer cancel()
response, err := node.RPCCall(ctx, gorums.CallData{
Message: &dummy.Empty{},
Method: "dummy.Dummy.Test",
})
if err == nil {
t.Fatalf("Expected error, got: %v, want: %v", err, fmt.Errorf("context deadline exceeded"))
}
if response != nil {
t.Fatalf("Unexpected response, got: %v, want: %v", response, nil)
}
}
func initServer() *gorums.Server {
srv := gorums.NewServer()
dummy.RegisterDummyServer(srv, &testSrv{})
return srv
}
func gorumsTestMgr() *dummy.Manager {
mgr := dummy.NewManager(
gorums.WithGrpcDialOptions(
grpc.WithTransportCredentials(insecure.NewCredentials()),
),
)
return mgr
}
type testSrv struct{}
func (t testSrv) Test(ctx gorums.ServerCtx, request *dummy.Empty) (response *dummy.Empty, err error) {
return &dummy.Empty{}, nil
}