Skip to content

fix: Try to fix cli portforward test flakes #1650

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

Merged
merged 6 commits into from
May 24, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: Improve test teardown in setupTestListener, cleanup
  • Loading branch information
mafredri committed May 23, 2022
commit 0259a190153e95423f080af3739f3ca5a85246c5
42 changes: 20 additions & 22 deletions cli/portforward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,8 @@ func TestPortForward(t *testing.T) {
client = coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
user = coderdtest.CreateFirstUser(t, client)
_, workspace = runAgent(t, client, user.UserID)
l1, p1 = setupTestListener(t, c.setupRemote(t))
p1 = setupTestListener(t, c.setupRemote(t))
)
t.Cleanup(func() {
_ = l1.Close()
})

// Create a flag that forwards from local to listener 1.
localAddress, localFlag := c.setupLocal(t)
Expand Down Expand Up @@ -201,13 +198,9 @@ func TestPortForward(t *testing.T) {
client = coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
user = coderdtest.CreateFirstUser(t, client)
_, workspace = runAgent(t, client, user.UserID)
l1, p1 = setupTestListener(t, c.setupRemote(t))
l2, p2 = setupTestListener(t, c.setupRemote(t))
p1 = setupTestListener(t, c.setupRemote(t))
p2 = setupTestListener(t, c.setupRemote(t))
)
t.Cleanup(func() {
_ = l1.Close()
_ = l2.Close()
})

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

// Setup remote Unix listener.
l1, p1 = setupTestListener(t, unixCase.setupRemote(t))
p1 = setupTestListener(t, unixCase.setupRemote(t))
)
t.Cleanup(func() {
_ = l1.Close()
})

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

l, p := setupTestListener(t, c.setupRemote(t))
t.Cleanup(func() {
_ = l.Close()
})
p := setupTestListener(t, c.setupRemote(t))

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

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

// setupTestListener starts accepting connections and echoing a single packet.
// Returns the listener and the listen port or Unix path.
func setupTestListener(t *testing.T, l net.Listener) (net.Listener, string) {
func setupTestListener(t *testing.T, l net.Listener) string {
// Wait for listener to completely exit before releasing.
done := make(chan struct{})
t.Cleanup(func() {
_ = l.Close()
<-done
})
go func() {
defer close(done)
// Guard against testAccept running require after test completion.
var wg sync.WaitGroup
defer wg.Wait()

for {
c, err := l.Accept()
if err != nil {
return
}

go testAccept(t, c)
wg.Add(1)
go func() {
testAccept(t, c)
wg.Done()
}()
}
}()

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

return l, addr
return addr
}

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