Skip to content

Commit 81c25f3

Browse files
committed
fix: Improve test teardown in setupTestListener, cleanup
1 parent f137169 commit 81c25f3

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

cli/portforward_test.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,8 @@ func TestPortForward(t *testing.T) {
154154
client = coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
155155
user = coderdtest.CreateFirstUser(t, client)
156156
_, workspace = runAgent(t, client, user.UserID)
157-
l1, p1 = setupTestListener(t, c.setupRemote(t))
157+
p1 = setupTestListener(t, c.setupRemote(t))
158158
)
159-
t.Cleanup(func() {
160-
_ = l1.Close()
161-
})
162159

163160
// Create a flag that forwards from local to listener 1.
164161
localAddress, localFlag := c.setupLocal(t)
@@ -201,13 +198,9 @@ func TestPortForward(t *testing.T) {
201198
client = coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
202199
user = coderdtest.CreateFirstUser(t, client)
203200
_, workspace = runAgent(t, client, user.UserID)
204-
l1, p1 = setupTestListener(t, c.setupRemote(t))
205-
l2, p2 = setupTestListener(t, c.setupRemote(t))
201+
p1 = setupTestListener(t, c.setupRemote(t))
202+
p2 = setupTestListener(t, c.setupRemote(t))
206203
)
207-
t.Cleanup(func() {
208-
_ = l1.Close()
209-
_ = l2.Close()
210-
})
211204

212205
// Create a flags for listener 1 and listener 2.
213206
localAddress1, localFlag1 := c.setupLocal(t)
@@ -262,11 +255,8 @@ func TestPortForward(t *testing.T) {
262255
unixCase = cases[2]
263256

264257
// Setup remote Unix listener.
265-
l1, p1 = setupTestListener(t, unixCase.setupRemote(t))
258+
p1 = setupTestListener(t, unixCase.setupRemote(t))
266259
)
267-
t.Cleanup(func() {
268-
_ = l1.Close()
269-
})
270260

271261
// Create a flag that forwards from local TCP to Unix listener 1.
272262
// Notably this is a --unix flag.
@@ -324,10 +314,7 @@ func TestPortForward(t *testing.T) {
324314
continue
325315
}
326316

327-
l, p := setupTestListener(t, c.setupRemote(t))
328-
t.Cleanup(func() {
329-
_ = l.Close()
330-
})
317+
p := setupTestListener(t, c.setupRemote(t))
331318

332319
localAddress, localFlag := c.setupLocal(t)
333320
dials = append(dials, addr{
@@ -425,7 +412,6 @@ func runAgent(t *testing.T, client *codersdk.Client, userID uuid.UUID) ([]coders
425412
})
426413
go func() {
427414
errC <- cmd.ExecuteContext(agentCtx)
428-
require.NoError(t, err)
429415
}()
430416

431417
coderdtest.AwaitWorkspaceAgents(t, client, workspace.LatestBuild.ID)
@@ -437,18 +423,30 @@ func runAgent(t *testing.T, client *codersdk.Client, userID uuid.UUID) ([]coders
437423

438424
// setupTestListener starts accepting connections and echoing a single packet.
439425
// Returns the listener and the listen port or Unix path.
440-
func setupTestListener(t *testing.T, l net.Listener) (net.Listener, string) {
426+
func setupTestListener(t *testing.T, l net.Listener) string {
427+
// Wait for listener to completely exit before releasing.
428+
done := make(chan struct{})
441429
t.Cleanup(func() {
442430
_ = l.Close()
431+
<-done
443432
})
444433
go func() {
434+
defer close(done)
435+
// Guard against testAccept running require after test completion.
436+
var wg sync.WaitGroup
437+
defer wg.Wait()
438+
445439
for {
446440
c, err := l.Accept()
447441
if err != nil {
448442
return
449443
}
450444

451-
go testAccept(t, c)
445+
wg.Add(1)
446+
go func() {
447+
wg.Done()
448+
testAccept(t, c)
449+
}()
452450
}
453451
}()
454452

@@ -459,7 +457,7 @@ func setupTestListener(t *testing.T, l net.Listener) (net.Listener, string) {
459457
addr = port
460458
}
461459

462-
return l, addr
460+
return addr
463461
}
464462

465463
var dialTestPayload = []byte("dean-was-here123")

0 commit comments

Comments
 (0)