From 3d9e6795eb6ac4cbcf42d0020ea86986dc1f076c Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 21 Apr 2014 12:08:00 -0700 Subject: [PATCH] consul: Adding strongly consistent read tests --- consul/catalog_endpoint_test.go | 99 +++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/consul/catalog_endpoint_test.go b/consul/catalog_endpoint_test.go index c9c87dcee2..3a174ce456 100644 --- a/consul/catalog_endpoint_test.go +++ b/consul/catalog_endpoint_test.go @@ -6,6 +6,7 @@ import ( "net/rpc" "os" "sort" + "strings" "testing" "time" ) @@ -296,6 +297,104 @@ func TestCatalogListNodes_StaleRaad(t *testing.T) { } } +func TestCatalogListNodes_ConsistentRead_Fail(t *testing.T) { + dir1, s1 := testServer(t) + defer os.RemoveAll(dir1) + defer s1.Shutdown() + client1 := rpcClient(t, s1) + defer client1.Close() + + dir2, s2 := testServerDCBootstrap(t, "dc1", false) + 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.BindPort) + if _, err := s2.JoinLAN([]string{addr}); err != nil { + t.Fatalf("err: %v", err) + } + + // Wait for a leader + time.Sleep(100 * time.Millisecond) + + // Use the leader as the client, kill the follower + var client *rpc.Client + if s1.IsLeader() { + client = client1 + s2.Shutdown() + } else { + client = client2 + s1.Shutdown() + } + + args := structs.DCSpecificRequest{ + Datacenter: "dc1", + QueryOptions: structs.QueryOptions{RequireConsistent: true}, + } + var out structs.IndexedNodes + if err := client.Call("Catalog.ListNodes", &args, &out); !strings.HasPrefix(err.Error(), "leadership lost") { + t.Fatalf("err: %v", err) + } + + if out.QueryMeta.LastContact != 0 { + t.Fatalf("should not have a last contact time") + } + if out.QueryMeta.KnownLeader { + t.Fatalf("should have no known leader") + } +} + +func TestCatalogListNodes_ConsistentRead(t *testing.T) { + dir1, s1 := testServer(t) + defer os.RemoveAll(dir1) + defer s1.Shutdown() + client1 := rpcClient(t, s1) + defer client1.Close() + + dir2, s2 := testServerDCBootstrap(t, "dc1", false) + 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.BindPort) + if _, err := s2.JoinLAN([]string{addr}); err != nil { + t.Fatalf("err: %v", err) + } + + // Wait for a leader + time.Sleep(100 * time.Millisecond) + + // Use the leader as the client, kill the follower + var client *rpc.Client + if s1.IsLeader() { + client = client1 + } else { + client = client2 + } + + args := structs.DCSpecificRequest{ + Datacenter: "dc1", + QueryOptions: structs.QueryOptions{RequireConsistent: true}, + } + var out structs.IndexedNodes + if err := client.Call("Catalog.ListNodes", &args, &out); err != nil { + t.Fatalf("err: %v", err) + } + + if out.QueryMeta.LastContact != 0 { + t.Fatalf("should not have a last contact time") + } + if !out.QueryMeta.KnownLeader { + t.Fatalf("should have known leader") + } +} + func BenchmarkCatalogListNodes(t *testing.B) { dir1, s1 := testServer(nil) defer os.RemoveAll(dir1)