testutil: allow skipping leader wait, update example

pull/964/head
Ryan Uber 10 years ago
parent db134f6190
commit 04d73628d1

@ -28,8 +28,12 @@ func TestMain(t *testing.T) {
srv1 := testutil.NewTestServer(t) srv1 := testutil.NewTestServer(t)
defer srv1.Stop() defer srv1.Stop()
// Create a secondary server // Create a secondary server, passing in configuration
srv2 := testutil.NewTestServer(t) // to avoid bootstrapping or waiting for a leader.
srv2 := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
c.Bootstrap = false
c.NoLeaderWait = true
})
defer srv2.Stop() defer srv2.Stop()
// Join the servers together // Join the servers together

@ -59,6 +59,11 @@ type TestServerConfig struct {
Bind string `json:"bind_addr,omitempty"` Bind string `json:"bind_addr,omitempty"`
Addresses *TestAddressConfig `json:"addresses,omitempty"` Addresses *TestAddressConfig `json:"addresses,omitempty"`
Ports *TestPortConfig `json:"ports,omitempty"` Ports *TestPortConfig `json:"ports,omitempty"`
// NoLeaderWait is a special config option used to instruct
// the test harness not to wait for a leader. Useful for
// bootstrapping a multi-node cluster for testing.
NoLeaderWait bool `json:"-"`
} }
// ServerConfigCallback is a function interface which can be // ServerConfigCallback is a function interface which can be
@ -142,12 +147,11 @@ func NewTestServerConfig(t *testing.T, cb ServerConfigCallback) *TestServer {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
configFile, err := ioutil.TempFile("", "consul") configFile, err := ioutil.TempFile(dataDir, "config")
if err != nil { if err != nil {
defer os.RemoveAll(dataDir) defer os.RemoveAll(dataDir)
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
defer os.Remove(configFile.Name())
consulConfig := defaultServerConfig() consulConfig := defaultServerConfig()
consulConfig.DataDir = dataDir consulConfig.DataDir = dataDir
@ -203,7 +207,11 @@ func NewTestServerConfig(t *testing.T, cb ServerConfigCallback) *TestServer {
} }
// Wait for the server to be ready // Wait for the server to be ready
if consulConfig.NoLeaderWait {
server.waitForAPI()
} else {
server.waitForLeader() server.waitForLeader()
}
return server return server
} }
@ -219,6 +227,26 @@ func (s *TestServer) Stop() {
} }
} }
// waitForAPI waits for only the agent HTTP endpoint to start
// responding. This is an indication that the agent has started,
// but will likely return before a leader is elected.
func (s *TestServer) waitForAPI() {
WaitForResult(func() (bool, error) {
resp, err := s.HttpClient.Get(s.url("/v1/agent/self"))
if err != nil {
return false, err
}
defer resp.Body.Close()
if err := s.requireOK(resp); err != nil {
return false, err
}
return true, nil
}, func(err error) {
defer s.Stop()
s.t.Fatalf("err: %s", err)
})
}
// waitForLeader waits for the Consul server's HTTP API to become // waitForLeader waits for the Consul server's HTTP API to become
// available, and then waits for a known leader and an index of // available, and then waits for a known leader and an index of
// 1 or more to be observed to confirm leader election is done. // 1 or more to be observed to confirm leader election is done.

Loading…
Cancel
Save