Skip to content

Commit

Permalink
Merge pull request #136 from hashicorp/f-testutil-package
Browse files Browse the repository at this point in the history
WIP: Proof of concept using `WaitForResult` in tests
  • Loading branch information
armon committed May 16, 2014
2 parents e08ca71 + dcfd03b commit baa831d
Show file tree
Hide file tree
Showing 22 changed files with 369 additions and 365 deletions.
22 changes: 13 additions & 9 deletions command/agent/agent_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package agent

import (
"fmt"
"github.com/hashicorp/consul/testutil"
"github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/serf/serf"
"net/http"
"os"
"testing"
"time"
"errors"
)

func TestHTTPAgentServices(t *testing.T) {
Expand Down Expand Up @@ -167,9 +169,11 @@ func TestHTTPAgentJoin_WAN(t *testing.T) {
t.Fatalf("Err: %v", obj)
}

if len(a2.WANMembers()) != 2 {
testutil.WaitForResult(func() (bool, error) {
return len(a2.WANMembers()) == 2, nil
}, func(err error) {
t.Fatalf("should have 2 members")
}
})
}

func TestHTTPAgentForceLeave(t *testing.T) {
Expand All @@ -189,9 +193,7 @@ func TestHTTPAgentForceLeave(t *testing.T) {
t.Fatalf("err: %v", err)
}

// Shutdown, wait for detection
a2.Shutdown()
time.Sleep(500 * time.Millisecond)

// Force leave now
req, err := http.NewRequest("GET", fmt.Sprintf("/v1/agent/force-leave/%s", a2.config.NodeName), nil)
Expand All @@ -207,11 +209,13 @@ func TestHTTPAgentForceLeave(t *testing.T) {
t.Fatalf("Err: %v", obj)
}

// SHould be left
mem := srv.agent.LANMembers()
if mem[1].Status != serf.StatusLeft {
t.Fatalf("should have left: %v", mem)
}
testutil.WaitForResult(func() (bool, error) {
m := srv.agent.LANMembers()
success := m[1].Status == serf.StatusLeft
return success, errors.New(m[1].Status.String())
}, func(err error) {
t.Fatalf("member status is %v, should be left", err)
})
}

func TestHTTPAgentRegisterCheck(t *testing.T) {
Expand Down
35 changes: 16 additions & 19 deletions command/agent/catalog_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package agent

import (
"fmt"
"github.com/hashicorp/consul/testutil"
"github.com/hashicorp/consul/consul/structs"
"net/http"
"net/http/httptest"
Expand All @@ -16,8 +17,7 @@ func TestCatalogRegister(t *testing.T) {
defer srv.Shutdown()
defer srv.agent.Shutdown()

// Wait for a leader
time.Sleep(100 * time.Millisecond)
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")

// Register node
req, err := http.NewRequest("GET", "/v1/catalog/register", nil)
Expand Down Expand Up @@ -47,8 +47,7 @@ func TestCatalogDeregister(t *testing.T) {
defer srv.Shutdown()
defer srv.agent.Shutdown()

// Wait for a leader
time.Sleep(100 * time.Millisecond)
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")

// Register node
req, err := http.NewRequest("GET", "/v1/catalog/deregister", nil)
Expand Down Expand Up @@ -77,9 +76,6 @@ func TestCatalogDatacenters(t *testing.T) {
defer srv.Shutdown()
defer srv.agent.Shutdown()

// Wait for initialization
time.Sleep(10 * time.Millisecond)

obj, err := srv.CatalogDatacenters(nil, nil)
if err != nil {
t.Fatalf("err: %v", err)
Expand All @@ -97,15 +93,15 @@ func TestCatalogNodes(t *testing.T) {
defer srv.Shutdown()
defer srv.agent.Shutdown()

// Wait for a leader
time.Sleep(100 * time.Millisecond)
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")

// Register node
args := &structs.RegisterRequest{
Datacenter: "dc1",
Node: "foo",
Address: "127.0.0.1",
}

var out struct{}
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -137,13 +133,13 @@ func TestCatalogNodes_Blocking(t *testing.T) {
defer srv.Shutdown()
defer srv.agent.Shutdown()

// Wait for a leader
time.Sleep(100 * time.Millisecond)
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")

// Register node
args := &structs.DCSpecificRequest{
Datacenter: "dc1",
}

var out structs.IndexedNodes
if err := srv.agent.RPC("Catalog.ListNodes", *args, &out); err != nil {
t.Fatalf("err: %v", err)
Expand All @@ -152,7 +148,7 @@ func TestCatalogNodes_Blocking(t *testing.T) {
// Do an update in a little while
start := time.Now()
go func() {
time.Sleep(100 * time.Millisecond)
time.Sleep(50 * time.Millisecond)
args := &structs.RegisterRequest{
Datacenter: "dc1",
Node: "foo",
Expand All @@ -178,7 +174,8 @@ func TestCatalogNodes_Blocking(t *testing.T) {
}

// Should block for a while
if time.Now().Sub(start) < 100*time.Millisecond {
if time.Now().Sub(start) < 50 * time.Millisecond {
// TODO: Failing
t.Fatalf("too fast")
}

Expand All @@ -198,8 +195,7 @@ func TestCatalogServices(t *testing.T) {
defer srv.Shutdown()
defer srv.agent.Shutdown()

// Wait for a leader
time.Sleep(100 * time.Millisecond)
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")

// Register node
args := &structs.RegisterRequest{
Expand All @@ -210,6 +206,7 @@ func TestCatalogServices(t *testing.T) {
Service: "api",
},
}

var out struct{}
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -240,8 +237,7 @@ func TestCatalogServiceNodes(t *testing.T) {
defer srv.Shutdown()
defer srv.agent.Shutdown()

// Wait for a leader
time.Sleep(100 * time.Millisecond)
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")

// Register node
args := &structs.RegisterRequest{
Expand All @@ -253,6 +249,7 @@ func TestCatalogServiceNodes(t *testing.T) {
Tags: []string{"a"},
},
}

var out struct{}
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -283,8 +280,7 @@ func TestCatalogNodeServices(t *testing.T) {
defer srv.Shutdown()
defer srv.agent.Shutdown()

// Wait for a leader
time.Sleep(100 * time.Millisecond)
testutil.WaitForLeader(t, srv.agent.RPC, "dc1")

// Register node
args := &structs.RegisterRequest{
Expand All @@ -296,6 +292,7 @@ func TestCatalogNodeServices(t *testing.T) {
Tags: []string{"a"},
},
}

var out struct{}
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion command/agent/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func expectStatus(t *testing.T, script, status string) {
Notify: mock,
CheckID: "foo",
Script: script,
Interval: 25 * time.Millisecond,
Interval: 10 * time.Millisecond,
Logger: log.New(os.Stderr, "", log.LstdFlags),
}
check.Start()
Expand Down
Loading

0 comments on commit baa831d

Please sign in to comment.