Skip to content

fix(scaletest/createworkspaces): address race condition between closer and cleanup #10210

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 1 commit into from
Oct 11, 2023
Merged
Changes from all 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
31 changes: 18 additions & 13 deletions scaletest/createworkspaces/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ func Test_Runner(t *testing.T) {
version = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

closer := goEventuallyStartFakeAgent(ctx, t, client, authToken)
t.Cleanup(closer)
closerCh := goEventuallyStartFakeAgent(ctx, t, client, authToken)

const (
username = "scaletest-user"
Expand Down Expand Up @@ -147,6 +146,10 @@ func Test_Runner(t *testing.T) {
t.Log("Runner logs:\n\n" + logsStr)
require.NoError(t, err)

// Wait for the workspace agent to start.
closer := <-closerCh
t.Cleanup(func() { _ = closer.Close() })

// Ensure a user and workspace were created.
users, err := client.Users(ctx, codersdk.UsersRequest{})
require.NoError(t, err)
Expand Down Expand Up @@ -373,8 +376,7 @@ func Test_Runner(t *testing.T) {
version = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

closer := goEventuallyStartFakeAgent(ctx, t, client, authToken)
t.Cleanup(closer)
closeCh := goEventuallyStartFakeAgent(ctx, t, client, authToken)

const (
username = "scaletest-user"
Expand Down Expand Up @@ -414,6 +416,10 @@ func Test_Runner(t *testing.T) {
t.Log("Runner logs:\n\n" + logsStr)
require.NoError(t, err)

// Wait for the agent to start.
closer := <-closeCh
t.Cleanup(func() { _ = closer.Close() })

// Ensure a user and workspace were created.
users, err := client.Users(ctx, codersdk.UsersRequest{})
require.NoError(t, err)
Expand Down Expand Up @@ -519,7 +525,7 @@ func Test_Runner(t *testing.T) {
// listing workspaces until we find it, then wait for the build to
// finish, then start the agents. It is the caller's responsibility to
// call the returned function to stop the agents.
func goEventuallyStartFakeAgent(ctx context.Context, t *testing.T, client *codersdk.Client, agentToken string) func() {
func goEventuallyStartFakeAgent(ctx context.Context, t *testing.T, client *codersdk.Client, agentToken string) chan io.Closer {
t.Helper()
ch := make(chan io.Closer, 1) // Don't block.
go func() {
Expand All @@ -537,7 +543,7 @@ func goEventuallyStartFakeAgent(ctx context.Context, t *testing.T, client *coder
break
}

time.Sleep(100 * time.Millisecond)
time.Sleep(testutil.IntervalMedium)
}

coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
Expand All @@ -549,13 +555,12 @@ func goEventuallyStartFakeAgent(ctx context.Context, t *testing.T, client *coder
Logger: slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).
Named("agent").Leveled(slog.LevelWarn),
})
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
resources := coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
assert.GreaterOrEqual(t, len(resources), 1, "workspace %s has no resources", workspace.ID.String())
assert.NotEmpty(t, resources[0].Agents, "workspace %s has no agents", workspace.ID.String())
agentID := resources[0].Agents[0].ID
t.Logf("agent %s is running for workspace %s", agentID.String(), workspace.ID.String())
ch <- agentCloser
}()
closeFunc := func() {
if closer, ok := <-ch; ok {
_ = closer.Close()
}
}
return closeFunc
return ch
}