Skip to content

Commit

Permalink
Merge pull request #2178 from hashicorp/f-rpc-test
Browse files Browse the repository at this point in the history
Adds some supplemental tests for RPC "no leader" retries.
  • Loading branch information
slackpad authored Jul 11, 2016
2 parents c6ef1d8 + 0ba1350 commit c635afd
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions consul/rpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package consul

import (
"os"
"testing"
"time"

"github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/consul/testutil"
"github.com/hashicorp/net-rpc-msgpackrpc"
)

func TestRPC_NoLeader_Fail(t *testing.T) {
dir1, s1 := testServerWithConfig(t, func(c *Config) {
c.RPCHoldTimeout = 1 * time.Millisecond
})
defer os.RemoveAll(dir1)
defer s1.Shutdown()
codec := rpcClient(t, s1)
defer codec.Close()

arg := structs.RegisterRequest{
Datacenter: "dc1",
Node: "foo",
Address: "127.0.0.1",
}
var out struct{}

// Make sure we eventually fail with a no leader error, which we should
// see given the short timeout.
err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out)
if err.Error() != structs.ErrNoLeader.Error() {
t.Fatalf("bad: %v", err)
}

// Now make sure it goes through.
testutil.WaitForLeader(t, s1.RPC, "dc1")
err = msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out)
if err != nil {
t.Fatalf("bad: %v", err)
}
}

func TestRPC_NoLeader_Retry(t *testing.T) {
dir1, s1 := testServerWithConfig(t, func(c *Config) {
c.RPCHoldTimeout = 10 * time.Second
})
defer os.RemoveAll(dir1)
defer s1.Shutdown()
codec := rpcClient(t, s1)
defer codec.Close()

arg := structs.RegisterRequest{
Datacenter: "dc1",
Node: "foo",
Address: "127.0.0.1",
}
var out struct{}

// This isn't sure-fire but tries to check that we don't have a
// leader going into the RPC, so we exercise the retry logic.
if ok, _ := s1.getLeader(); ok {
t.Fatalf("should not have a leader yet")
}

// The timeout is long enough to ride out any reasonable leader
// election.
err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out)
if err != nil {
t.Fatalf("bad: %v", err)
}
}

0 comments on commit c635afd

Please sign in to comment.