Skip to content

Commit 746203d

Browse files
committed
update tests
1 parent f613f31 commit 746203d

File tree

4 files changed

+144
-18
lines changed

4 files changed

+144
-18
lines changed

coderd/database/querier.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/quotas.sql

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ SELECT
2323
wb.daily_cost
2424
FROM
2525
workspace_builds wb
26-
-- This INNER JOIN prevents a
26+
-- This INNER JOIN prevents a seq scan of the workspace_builds table.
27+
-- Limit the rows to the absolute minimum required, which is all workspaces
28+
-- in a given organization for a given user.
2729
INNER JOIN
2830
workspaces on wb.workspace_id = workspaces.id
2931
WHERE
30-
workspaces.owner_id = @owner_id
32+
workspaces.owner_id = @owner_id AND
33+
workspaces.organization_id = @organization_id
3134
ORDER BY
3235
wb.workspace_id,
3336
wb.created_at DESC
@@ -38,8 +41,8 @@ FROM
3841
workspaces
3942
JOIN latest_builds ON
4043
latest_builds.workspace_id = workspaces.id
41-
WHERE NOT
42-
deleted AND
44+
WHERE
45+
NOT deleted AND
4346
workspaces.owner_id = @owner_id AND
4447
workspaces.organization_id = @organization_id
4548
;

enterprise/coderd/workspacequota_test.go

Lines changed: 127 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ func TestWorkspaceSerialization(t *testing.T) {
356356
// - BeginTX -> Bump! -> GetQuota -> GetAllowance -> UpdateCost -> EndTx
357357
// - BeginTX -> GetQuota -> GetAllowance -> UpdateCost -> Bump! -> EndTx
358358
t.Run("UpdateBuildDeadline", func(t *testing.T) {
359+
t.Skip("Expected to fail. As long as quota & deadline are on the same " +
360+
" table and affect the same row, this will likely always fail.")
359361
// +------------------------------+------------------+
360362
// | Begin Tx | |
361363
// +------------------------------+------------------+
@@ -470,6 +472,8 @@ func TestWorkspaceSerialization(t *testing.T) {
470472
})
471473

472474
t.Run("ActivityBump", func(t *testing.T) {
475+
t.Skip("Expected to fail. As long as quota & deadline are on the same " +
476+
" table and affect the same row, this will likely always fail.")
473477
// +---------------------+----------------------------------+
474478
// | W1 Quota Tx | |
475479
// +---------------------+----------------------------------+
@@ -528,7 +532,7 @@ func TestWorkspaceSerialization(t *testing.T) {
528532
// +---------------------+----------------------------------+
529533
// | GetAllowance(w1) | |
530534
// +---------------------+----------------------------------+
531-
// | | UpdateWorkspaceBuildDeadline(w1) |
535+
// | | UpdateWorkspaceLastUsedAt(w1) |
532536
// +---------------------+----------------------------------+
533537
// | UpdateBuildCost(w1) | |
534538
// +---------------------+----------------------------------+
@@ -549,11 +553,9 @@ func TestWorkspaceSerialization(t *testing.T) {
549553
one.GetQuota(ctx, t)
550554
one.GetAllowance(ctx, t)
551555

552-
err := db.UpdateWorkspaceBuildDeadlineByID(ctx, database.UpdateWorkspaceBuildDeadlineByIDParams{
553-
Deadline: dbtime.Now(),
554-
MaxDeadline: dbtime.Now(),
555-
UpdatedAt: dbtime.Now(),
556-
ID: myWorkspace.Build.ID,
556+
err := db.UpdateWorkspaceLastUsedAt(ctx, database.UpdateWorkspaceLastUsedAtParams{
557+
ID: myWorkspace.Workspace.ID,
558+
LastUsedAt: dbtime.Now(),
557559
})
558560
assert.NoError(t, err)
559561

@@ -610,7 +612,7 @@ func TestWorkspaceSerialization(t *testing.T) {
610612

611613
// QuotaCommit 2 workspaces in different orgs.
612614
// Workspaces do not share templates, owners, or orgs
613-
t.Run("DoubleQuotaWorkspaces", func(t *testing.T) {
615+
t.Run("DoubleQuotaUnrelatedWorkspaces", func(t *testing.T) {
614616
// +---------------------+---------------------+
615617
// | W1 Quota Tx | W2 Quota Tx |
616618
// +---------------------+---------------------+
@@ -634,7 +636,6 @@ func TestWorkspaceSerialization(t *testing.T) {
634636
// +---------------------+---------------------+
635637
// | | CommitTx() |
636638
// +---------------------+---------------------+
637-
// pq: could not serialize access due to read/write dependencies among transactions
638639
ctx := testutil.Context(t, testutil.WaitLong)
639640
ctx = dbauthz.AsSystemRestricted(ctx)
640641

@@ -667,9 +668,124 @@ func TestWorkspaceSerialization(t *testing.T) {
667668
assert.NoError(t, two.Done())
668669
})
669670

670-
// Autobuild, then quota, then autobuild read agin in the same tx
671-
// https://www.richardstrnad.ch/posts/go-sql-how-to-get-detailed-error/
672-
// https://blog.danslimmon.com/2024/01/10/why-transaction-order-matters-even-if-youre-only-reading/
671+
// QuotaCommit 2 workspaces in different orgs.
672+
// Workspaces do not share templates or orgs
673+
t.Run("DoubleQuotaUserWorkspacesDiffOrgs", func(t *testing.T) {
674+
// +---------------------+---------------------+
675+
// | W1 Quota Tx | W2 Quota Tx |
676+
// +---------------------+---------------------+
677+
// | Begin Tx | |
678+
// +---------------------+---------------------+
679+
// | | Begin Tx |
680+
// +---------------------+---------------------+
681+
// | GetQuota(w1) | |
682+
// +---------------------+---------------------+
683+
// | GetAllowance(w1) | |
684+
// +---------------------+---------------------+
685+
// | UpdateBuildCost(w1) | |
686+
// +---------------------+---------------------+
687+
// | | UpdateBuildCost(w2) |
688+
// +---------------------+---------------------+
689+
// | | GetQuota(w2) |
690+
// +---------------------+---------------------+
691+
// | | GetAllowance(w2) |
692+
// +---------------------+---------------------+
693+
// | CommitTx() | |
694+
// +---------------------+---------------------+
695+
// | | CommitTx() |
696+
// +---------------------+---------------------+
697+
ctx := testutil.Context(t, testutil.WaitLong)
698+
ctx = dbauthz.AsSystemRestricted(ctx)
699+
700+
myWorkspace := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
701+
OrganizationID: org.Org.ID,
702+
OwnerID: user.ID,
703+
}).Do()
704+
705+
myOtherWorkspace := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
706+
OrganizationID: otherOrg.Org.ID, // Different org!
707+
OwnerID: user.ID,
708+
}).Do()
709+
710+
one := newCommitter(t, db, myWorkspace.Workspace, myWorkspace.Build)
711+
two := newCommitter(t, db, myOtherWorkspace.Workspace, myOtherWorkspace.Build)
712+
713+
var _, _ = one, two
714+
// Run order
715+
one.GetQuota(ctx, t)
716+
one.GetAllowance(ctx, t)
717+
718+
one.UpdateWorkspaceBuildCostByID(ctx, t, 10)
719+
720+
two.GetQuota(ctx, t)
721+
two.GetAllowance(ctx, t)
722+
two.UpdateWorkspaceBuildCostByID(ctx, t, 10)
723+
724+
// End commit
725+
assert.NoError(t, one.Done())
726+
assert.NoError(t, two.Done())
727+
})
728+
729+
// QuotaCommit 2 workspaces in the same org.
730+
// Workspaces do not share templates
731+
t.Run("DoubleQuotaUserWorkspaces", func(t *testing.T) {
732+
t.Skip("Setting a new build cost to a workspace in a org affects other " +
733+
"workspaces in the same org. This is expected to fail.")
734+
// +---------------------+---------------------+
735+
// | W1 Quota Tx | W2 Quota Tx |
736+
// +---------------------+---------------------+
737+
// | Begin Tx | |
738+
// +---------------------+---------------------+
739+
// | | Begin Tx |
740+
// +---------------------+---------------------+
741+
// | GetQuota(w1) | |
742+
// +---------------------+---------------------+
743+
// | GetAllowance(w1) | |
744+
// +---------------------+---------------------+
745+
// | UpdateBuildCost(w1) | |
746+
// +---------------------+---------------------+
747+
// | | UpdateBuildCost(w2) |
748+
// +---------------------+---------------------+
749+
// | | GetQuota(w2) |
750+
// +---------------------+---------------------+
751+
// | | GetAllowance(w2) |
752+
// +---------------------+---------------------+
753+
// | CommitTx() | |
754+
// +---------------------+---------------------+
755+
// | | CommitTx() |
756+
// +---------------------+---------------------+
757+
// pq: could not serialize access due to read/write dependencies among transactions
758+
ctx := testutil.Context(t, testutil.WaitLong)
759+
ctx = dbauthz.AsSystemRestricted(ctx)
760+
761+
myWorkspace := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
762+
OrganizationID: org.Org.ID,
763+
OwnerID: user.ID,
764+
}).Do()
765+
766+
myOtherWorkspace := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
767+
OrganizationID: org.Org.ID,
768+
OwnerID: user.ID,
769+
}).Do()
770+
771+
one := newCommitter(t, db, myWorkspace.Workspace, myWorkspace.Build)
772+
two := newCommitter(t, db, myOtherWorkspace.Workspace, myOtherWorkspace.Build)
773+
774+
var _, _ = one, two
775+
// Run order
776+
one.GetQuota(ctx, t)
777+
one.GetAllowance(ctx, t)
778+
779+
one.UpdateWorkspaceBuildCostByID(ctx, t, 10)
780+
781+
two.GetQuota(ctx, t)
782+
two.GetAllowance(ctx, t)
783+
two.UpdateWorkspaceBuildCostByID(ctx, t, 10)
784+
785+
// End commit
786+
assert.NoError(t, one.Done())
787+
assert.NoError(t, two.Done())
788+
})
673789
}
674790

675791
func deprecatedQuotaEndpoint(ctx context.Context, client *codersdk.Client, userID string) (codersdk.WorkspaceQuota, error) {

0 commit comments

Comments
 (0)