Skip to content

Commit 0ba1350

Browse files
author
James Phillips
committed
Adds some supplemental tests for RPC "no leader" retries.
This adds some extra tests for #2175.
1 parent c6ef1d8 commit 0ba1350

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

consul/rpc_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package consul
2+
3+
import (
4+
"os"
5+
"testing"
6+
"time"
7+
8+
"github.com/hashicorp/consul/consul/structs"
9+
"github.com/hashicorp/consul/testutil"
10+
"github.com/hashicorp/net-rpc-msgpackrpc"
11+
)
12+
13+
func TestRPC_NoLeader_Fail(t *testing.T) {
14+
dir1, s1 := testServerWithConfig(t, func(c *Config) {
15+
c.RPCHoldTimeout = 1 * time.Millisecond
16+
})
17+
defer os.RemoveAll(dir1)
18+
defer s1.Shutdown()
19+
codec := rpcClient(t, s1)
20+
defer codec.Close()
21+
22+
arg := structs.RegisterRequest{
23+
Datacenter: "dc1",
24+
Node: "foo",
25+
Address: "127.0.0.1",
26+
}
27+
var out struct{}
28+
29+
// Make sure we eventually fail with a no leader error, which we should
30+
// see given the short timeout.
31+
err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out)
32+
if err.Error() != structs.ErrNoLeader.Error() {
33+
t.Fatalf("bad: %v", err)
34+
}
35+
36+
// Now make sure it goes through.
37+
testutil.WaitForLeader(t, s1.RPC, "dc1")
38+
err = msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out)
39+
if err != nil {
40+
t.Fatalf("bad: %v", err)
41+
}
42+
}
43+
44+
func TestRPC_NoLeader_Retry(t *testing.T) {
45+
dir1, s1 := testServerWithConfig(t, func(c *Config) {
46+
c.RPCHoldTimeout = 10 * time.Second
47+
})
48+
defer os.RemoveAll(dir1)
49+
defer s1.Shutdown()
50+
codec := rpcClient(t, s1)
51+
defer codec.Close()
52+
53+
arg := structs.RegisterRequest{
54+
Datacenter: "dc1",
55+
Node: "foo",
56+
Address: "127.0.0.1",
57+
}
58+
var out struct{}
59+
60+
// This isn't sure-fire but tries to check that we don't have a
61+
// leader going into the RPC, so we exercise the retry logic.
62+
if ok, _ := s1.getLeader(); ok {
63+
t.Fatalf("should not have a leader yet")
64+
}
65+
66+
// The timeout is long enough to ride out any reasonable leader
67+
// election.
68+
err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out)
69+
if err != nil {
70+
t.Fatalf("bad: %v", err)
71+
}
72+
}

0 commit comments

Comments
 (0)