Skip to content

chore(coderd/database): optimize GetRunningPrebuiltWorkspaces #18588

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 3 commits into from
Jul 1, 2025
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
Next Next commit
chore(coderd/database): add test GetRunningPrebuiltWorkspaces
  • Loading branch information
johnstcn committed Jun 30, 2025
commit 02d1576f5f593750b3f1041a80259dda3dd93918
95 changes: 92 additions & 3 deletions coderd/database/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3716,9 +3716,9 @@ func createPrebuiltWorkspace(
job := dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{
Type: database.ProvisionerJobTypeWorkspaceBuild,
OrganizationID: orgID,

CreatedAt: now.Add(-1 * time.Minute),
Error: jobError,
CreatedAt: now.Add(-1 * time.Minute),
CompletedAt: sql.NullTime{Time: now, Valid: true},
Error: jobError,
})

// create ready agents
Expand Down Expand Up @@ -3888,6 +3888,95 @@ func TestWorkspacePrebuildsView(t *testing.T) {
}
}

func TestGetRunningPrebuiltWorkspaces(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reivew: adapted from TestWorkspacePrebuildsView.

t.Parallel()
if !dbtestutil.WillUsePostgres() {
t.SkipNow()
}

now := dbtime.Now()
orgID := uuid.New()
userID := uuid.New()

testCases := []struct {
name string
readyAgents int
notReadyAgents int
expectRows int
expectReady bool
}{
{
name: "one ready agent",
readyAgents: 1,
notReadyAgents: 0,
expectRows: 1,
expectReady: true,
},
{
name: "one not ready agent",
readyAgents: 0,
notReadyAgents: 1,
expectRows: 1,
expectReady: false,
},
{
name: "one ready, one not ready",
readyAgents: 1,
notReadyAgents: 1,
expectRows: 1,
expectReady: false,
},
{
name: "both ready",
readyAgents: 2,
notReadyAgents: 0,
expectRows: 1,
expectReady: true,
},
{
name: "five ready, one not ready",
readyAgents: 5,
notReadyAgents: 1,
expectRows: 1,
expectReady: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

sqlDB := testSQLDB(t)
err := migrations.Up(sqlDB)
require.NoError(t, err)
db := database.New(sqlDB)

ctx := testutil.Context(t, testutil.WaitShort)

dbgen.Organization(t, db, database.Organization{
ID: orgID,
})
dbgen.User(t, db, database.User{
ID: userID,
})

tmpl := createTemplate(t, db, orgID, userID)
tmplV1 := createTmplVersionAndPreset(t, db, tmpl, tmpl.ActiveVersionID, now, nil)
createPrebuiltWorkspace(ctx, t, db, tmpl, tmplV1, orgID, now, &createPrebuiltWorkspaceOpts{
readyAgents: tc.readyAgents,
notReadyAgents: tc.notReadyAgents,
})

workspacePrebuilds, err := db.GetRunningPrebuiltWorkspaces(ctx)
require.NoError(t, err)
require.Len(t, workspacePrebuilds, tc.expectRows)
if tc.expectRows > 0 {
require.Equal(t, tc.expectReady, workspacePrebuilds[0].Ready)
}
})
}
}

func TestGetPresetsBackoff(t *testing.T) {
t.Parallel()
if !dbtestutil.WillUsePostgres() {
Expand Down