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

This commit is contained in:
Cian Johnston 2023-10-11 12:10:51 +01:00 committed by GitHub
parent b3471bd23a
commit ed8092c83d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 13 deletions

View File

@ -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"
@ -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)
@ -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"
@ -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)
@ -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() {
@ -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)
@ -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
}