fix: Fix cleanup in test helpers, prefer defer in tests (#3113)

* fix: Change uses of t.Cleanup -> defer in test bodies

Mixing t.Cleanup and defer can lead to unexpected order of execution.

* fix: Ensure t.Cleanup is not aborted by require

* chore: Add helper annotations
This commit is contained in:
Mathias Fredriksson
2022-07-25 19:22:02 +03:00
committed by GitHub
parent c2cd51d8b8
commit 6916d34458
14 changed files with 60 additions and 53 deletions
+4 -7
View File
@@ -109,9 +109,9 @@ func TestConfigSSH(t *testing.T) {
agentCloser := agent.New(agentClient.ListenWorkspaceAgent, &agent.Options{
Logger: slogtest.Make(t, nil),
})
t.Cleanup(func() {
defer func() {
_ = agentCloser.Close()
})
}()
resources := coderdtest.AwaitWorkspaceAgents(t, client, workspace.LatestBuild.ID)
agentConn, err := client.DialWorkspaceAgent(context.Background(), resources[0].Agents[0].ID, nil)
require.NoError(t, err)
@@ -119,9 +119,9 @@ func TestConfigSSH(t *testing.T) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
t.Cleanup(func() {
defer func() {
_ = listener.Close()
})
}()
go func() {
for {
conn, err := listener.Accept()
@@ -134,9 +134,6 @@ func TestConfigSSH(t *testing.T) {
go io.Copy(ssh, conn)
}
}()
t.Cleanup(func() {
_ = listener.Close()
})
sshConfigFile, _ := sshConfigFileNames(t)