Skip to content

Commit 933f12b

Browse files
authored
testing: fix flake in Agent.Host RPC handler test (#26911)
When a client switches servers, there may be a brief window where the original server still thinks it's connected. In the `TestAgentHost_Server` test, if the client gets a RPC error then it'll retry the other server and the total count will be 2 instead of 1 within the window of the test. Fix this so that we're just detecting that the node connected at all, and tidy up test setup and assertions.
1 parent b50dbb8 commit 933f12b

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

nomad/client_agent_endpoint_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -840,10 +840,7 @@ func TestAgentHost_Server(t *testing.T) {
840840
defer cleanup()
841841

842842
TestJoin(t, s1, s2)
843-
testutil.WaitForLeader(t, s1.RPC)
844843
testutil.WaitForKeyring(t, s1.RPC, s1.Region())
845-
testutil.WaitForLeader(t, s2.RPC)
846-
testutil.WaitForKeyring(t, s2.RPC, s2.Region())
847844

848845
// determine leader and nonleader
849846
servers := []*Server{s1, s2}
@@ -868,9 +865,9 @@ func TestAgentHost_Server(t *testing.T) {
868865

869866
testutil.WaitForResult(func() (bool, error) {
870867
numNodes := len(s1.connectedNodes()) + len(s2.connectedNodes())
871-
return numNodes == 1, nil
868+
return numNodes > 0, nil
872869
}, func(err error) {
873-
t.Fatalf("should have a clients")
870+
t.Fatalf("client should be connected to a server")
874871
})
875872

876873
cases := []struct {
@@ -940,13 +937,14 @@ func TestAgentHost_Server(t *testing.T) {
940937

941938
err := tc.origin.RPC("Agent.Host", &req, &reply)
942939
if tc.expectedErr != "" {
943-
require.Contains(t, err.Error(), tc.expectedErr)
940+
must.ErrorContains(t, err, tc.expectedErr)
944941
} else {
945-
require.Nil(t, err)
946-
require.NotEmpty(t, reply.HostData)
942+
must.NoError(t, err)
943+
must.NotNil(t, reply.HostData)
947944
}
948945

949-
require.Equal(t, tc.expectedAgentID, reply.AgentID)
946+
// note: we expect this to be populated even on error
947+
must.Eq(t, tc.expectedAgentID, reply.AgentID)
950948
})
951949
}
952950
}

0 commit comments

Comments
 (0)