-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathrpc_test.go
More file actions
95 lines (83 loc) · 2.78 KB
/
rpc_test.go
File metadata and controls
95 lines (83 loc) · 2.78 KB
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
package gorums_test
import (
"context"
"fmt"
"sync"
"testing"
"time"
"github.com/relab/gorums"
"github.com/relab/gorums/internal/testutils/mock"
pb "google.golang.org/protobuf/types/known/wrapperspb"
)
func TestRPCCallSuccess(t *testing.T) {
node := gorums.TestNode(t, gorums.DefaultTestServer)
ctx := gorums.TestContext(t, 5*time.Second)
nodeCtx := node.Context(ctx)
response, err := gorums.RPCCall[*pb.StringValue, *pb.StringValue](nodeCtx, pb.String(""), mock.TestMethod)
if err != nil {
t.Fatalf("Unexpected error, got: %v, want: %v", err, nil)
}
if response == nil {
t.Fatalf("Unexpected response, got: %v, want: non-nil", nil)
}
}
func TestRPCCallDownedNode(t *testing.T) {
node := gorums.TestNode(t, gorums.DefaultTestServer, gorums.WithPreConnect(t, func(stopServers func()) {
stopServers()
time.Sleep(300 * time.Millisecond) // wait for servers to fully stop
}))
ctx := gorums.TestContext(t, 5*time.Second)
nodeCtx := node.Context(ctx)
response, err := gorums.RPCCall[*pb.StringValue, *pb.StringValue](nodeCtx, pb.String(""), mock.TestMethod)
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) {
node := gorums.TestNode(t, gorums.DefaultTestServer)
ctx, cancel := context.WithTimeout(t.Context(), 0*time.Second)
time.Sleep(50 * time.Millisecond)
defer cancel()
nodeCtx := node.Context(ctx)
response, err := gorums.RPCCall[*pb.StringValue, *pb.StringValue](nodeCtx, pb.String(""), mock.TestMethod)
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 TestRPCCallTypeMismatch(t *testing.T) {
node := gorums.TestNode(t, gorums.DefaultTestServer)
ctx := gorums.TestContext(t, 5*time.Second)
nodeCtx := node.Context(ctx)
response, err := gorums.RPCCall[*pb.StringValue, *pb.Int32Value](nodeCtx, pb.String(""), mock.TestMethod)
if err != gorums.ErrTypeMismatch {
t.Fatalf("Expected error, got: %v, want: %v", err, gorums.ErrTypeMismatch)
}
if response != nil {
t.Fatalf("Unexpected response, got: %v, want: %v", response, nil)
}
}
func TestRPCCallConcurrentAccess(t *testing.T) {
node := gorums.TestNode(t, gorums.DefaultTestServer)
concurrency := 10
errCh := make(chan error, concurrency)
var wg sync.WaitGroup
for range concurrency {
wg.Go(func() {
_, err := gorums.RPCCall[*pb.StringValue, *pb.StringValue](node.Context(t.Context()), pb.String(""), mock.TestMethod)
if err != nil {
errCh <- err
}
})
}
wg.Wait()
close(errCh)
for err := range errCh {
t.Error(err)
}
}