Test leader forwarding

This commit is contained in:
Armon Dadgar 2013-12-11 16:42:19 -08:00
parent 5135724dae
commit f73f2a6513
1 changed files with 46 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package consul
import (
"fmt"
"github.com/hashicorp/consul/rpc"
nrpc "net/rpc"
"os"
"testing"
"time"
@ -38,6 +39,51 @@ func TestCatalogRegister(t *testing.T) {
}
}
func TestCatalogRegister_ForwardLeader(t *testing.T) {
dir1, s1 := testServer(t)
defer os.RemoveAll(dir1)
defer s1.Shutdown()
client1 := rpcClient(t, s1)
defer client1.Close()
dir2, s2 := testServer(t)
defer os.RemoveAll(dir2)
defer s2.Shutdown()
client2 := rpcClient(t, s2)
defer client2.Close()
// Try to join
addr := fmt.Sprintf("127.0.0.1:%d",
s1.config.SerfLANConfig.MemberlistConfig.Port)
if err := s2.JoinLAN(addr); err != nil {
t.Fatalf("err: %v", err)
}
// Wait for a leader
time.Sleep(100 * time.Millisecond)
// Use the follower as the client
var client *nrpc.Client
if !s1.IsLeader() {
client = client1
} else {
client = client2
}
arg := rpc.RegisterRequest{
Datacenter: "dc1",
Node: "foo",
Address: "127.0.0.1",
ServiceName: "db",
ServiceTag: "master",
ServicePort: 8000,
}
var out struct{}
if err := client.Call("Catalog.Register", &arg, &out); err != nil {
t.Fatalf("err: %v", err)
}
}
func TestCatalogRegister_ForwardDC(t *testing.T) {
dir1, s1 := testServer(t)
defer os.RemoveAll(dir1)