Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new test case for force-leave #16260

Merged
merged 4 commits into from
Mar 3, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/agent.go
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ func (a *Agent) Region() (string, error) {

// Join is used to instruct a server node to join another server
// via the gossip protocol. Multiple addresses may be specified.
// We attempt to join all of the hosts in the list. Returns the
// We attempt to join all the hosts in the list. Returns the
// number of nodes successfully joined and any error. If one or
// more nodes have a successful result, no error is returned.
func (a *Agent) Join(addrs ...string) (int, error) {
26 changes: 25 additions & 1 deletion api/agent_test.go
Original file line number Diff line number Diff line change
@@ -110,7 +110,31 @@ func TestAgent_ForceLeave(t *testing.T) {
err := a.ForceLeave("nope")
must.NoError(t, err)

// TODO: test force-leave on an existing node
// Force-leave on an existing node
_, s2 := makeClient(t, nil, func(c *testutil.TestServerConfig) {
c.Server.BootstrapExpect = 0
})
defer s2.Stop()
// Create a new node to join
n, err := a.Join(s2.SerfAddr)
must.NoError(t, err)
must.One(t, n)

membersBefore, err := a.MembersOpts(&QueryOptions{})
must.Eq(t, membersBefore.Members[1].Status, "alive")

err = a.ForceLeave(membersBefore.Members[1].Name)
must.NoError(t, err)

time.Sleep(3 * time.Second)

membersAfter, err := a.MembersOpts(&QueryOptions{})

for _, node := range membersAfter.Members {
if node.Name == membersBefore.Members[1].Name {
must.Eq(t, node.Status, "leaving")
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this kind of thing, rather than waiting a static period of time, we can use shoenig/test/wait to wait until the assertion is true or the timeout expires. That lets us move the test along if the desired state is reached quickly.

For example, the following takes <3sec on my machine instead of >6sec. This adds up given the number of tests we run!

Suggested change
time.Sleep(3 * time.Second)
membersAfter, err := a.MembersOpts(&QueryOptions{})
for _, node := range membersAfter.Members {
if node.Name == membersBefore.Members[1].Name {
must.Eq(t, node.Status, "leaving")
}
}
f := func() error {
membersAfter, err := a.MembersOpts(&QueryOptions{})
if err != nil {
return err
}
for _, node := range membersAfter.Members {
if node.Name == membersBefore.Members[1].Name {
if node.Status != "leaving" {
return fmt.Errorf("node did not leave")
}
}
}
return nil
}
must.Wait(t, wait.InitialSuccess(
wait.ErrorFunc(f),
wait.Timeout(3*time.Second),
wait.Gap(100*time.Millisecond),
))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks alot for the suggestion and the code snippet. This is exactly what I'm looking for when writing this test. Totally agree that we can greatly reduce the test run time by making the wait.Gap() more aggressive instead of waiting for the whole duration.

}

func (a *AgentMember) String() string {
2 changes: 1 addition & 1 deletion api/nodes.go
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ func (c *Client) Nodes() *Nodes {
return &Nodes{client: c}
}

// List is used to list out all of the nodes
// List is used to list out all the nodes
func (n *Nodes) List(q *QueryOptions) ([]*NodeListStub, *QueryMeta, error) {
var resp NodeIndexSort
qm, err := n.client.query("/v1/nodes", &resp, q)