Skip to content

Commit 2311bd7

Browse files
committed
Add test for querying
1 parent 1ab03e2 commit 2311bd7

File tree

5 files changed

+84
-6
lines changed

5 files changed

+84
-6
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ jobs:
153153

154154
- name: Install sqlc
155155
run: |
156-
curl -sSL https://github.com/kyleconroy/sqlc/releases/download/v1.17.2/sqlc_1.17.2_linux_amd64.tar.gz | sudo tar -C /usr/bin -xz sqlc
156+
curl -sSL https://github.com/kyleconroy/sqlc/releases/download/v1.18.0/sqlc_1.18.0_linux_amd64.tar.gz | sudo tar -C /usr/bin -xz sqlc
157157
158158
- name: go install tools
159159
run: |

cli/testdata/coder_list_--output_json.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"tags": {
3737
"scope": "organization"
3838
},
39-
"queue_position": 1,
39+
"queue_position": 0,
4040
"queue_size": 0
4141
},
4242
"reason": "initiator",

coderd/database/dbfake/dbfake.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,10 +2060,13 @@ func (q *fakeQuerier) GetProvisionerJobsByIDsWithQueuePosition(_ context.Context
20602060
for _, job := range q.provisionerJobs {
20612061
for _, id := range ids {
20622062
if id == job.ID {
2063-
jobs = append(jobs, database.GetProvisionerJobsByIDsWithQueuePositionRow{
2063+
job := database.GetProvisionerJobsByIDsWithQueuePositionRow{
20642064
ProvisionerJob: job,
2065-
QueuePosition: queuePosition,
2066-
})
2065+
}
2066+
if !job.ProvisionerJob.StartedAt.Valid {
2067+
job.QueuePosition = queuePosition
2068+
}
2069+
jobs = append(jobs, job)
20672070
break
20682071
}
20692072
}

coderd/database/querier_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package database_test
55
import (
66
"context"
77
"database/sql"
8+
"encoding/json"
9+
"sort"
810
"testing"
911
"time"
1012

@@ -314,3 +316,76 @@ func TestDefaultProxy(t *testing.T) {
314316
require.NoError(t, err, "get deployment id")
315317
require.Equal(t, depID, found)
316318
}
319+
320+
func TestQueuePosition(t *testing.T) {
321+
t.Parallel()
322+
323+
if testing.Short() {
324+
t.SkipNow()
325+
}
326+
sqlDB := testSQLDB(t)
327+
err := migrations.Up(sqlDB)
328+
require.NoError(t, err)
329+
db := database.New(sqlDB)
330+
ctx := testutil.Context(t, testutil.WaitLong)
331+
332+
org := dbgen.Organization(t, db, database.Organization{})
333+
jobCount := 10
334+
jobs := []database.ProvisionerJob{}
335+
jobIDs := []uuid.UUID{}
336+
for i := 0; i < jobCount; i++ {
337+
job := dbgen.ProvisionerJob(t, db, database.ProvisionerJob{
338+
OrganizationID: org.ID,
339+
Tags: database.StringMap{},
340+
})
341+
jobs = append(jobs, job)
342+
jobIDs = append(jobIDs, job.ID)
343+
344+
// We need a slight amount of time between each insertion to ensure that
345+
// the queue position is correct... it's sorted by `created_at`.
346+
time.Sleep(time.Millisecond)
347+
}
348+
349+
queued, err := db.GetProvisionerJobsByIDsWithQueuePosition(ctx, jobIDs)
350+
require.NoError(t, err)
351+
require.Len(t, queued, jobCount)
352+
sort.Slice(queued, func(i, j int) bool {
353+
return queued[i].QueuePosition < queued[j].QueuePosition
354+
})
355+
// Ensure that the queue positions are correct based on insertion ID!
356+
for index, job := range queued {
357+
require.Equal(t, job.QueuePosition, int64(index+1))
358+
require.Equal(t, job.ProvisionerJob.ID, jobs[index].ID)
359+
}
360+
361+
job, err := db.AcquireProvisionerJob(ctx, database.AcquireProvisionerJobParams{
362+
StartedAt: sql.NullTime{
363+
Time: database.Now(),
364+
Valid: true,
365+
},
366+
Types: database.AllProvisionerTypeValues(),
367+
WorkerID: uuid.NullUUID{
368+
UUID: uuid.New(),
369+
Valid: true,
370+
},
371+
Tags: json.RawMessage("{}"),
372+
})
373+
require.NoError(t, err)
374+
require.Equal(t, jobs[0].ID, job.ID)
375+
376+
queued, err = db.GetProvisionerJobsByIDsWithQueuePosition(ctx, jobIDs)
377+
require.NoError(t, err)
378+
require.Len(t, queued, jobCount)
379+
sort.Slice(queued, func(i, j int) bool {
380+
return queued[i].QueuePosition < queued[j].QueuePosition
381+
})
382+
// Ensure that queue positions are updated now that the first job has been acquired!
383+
for index, job := range queued {
384+
if index == 0 {
385+
require.Equal(t, job.QueuePosition, int64(0))
386+
continue
387+
}
388+
require.Equal(t, job.QueuePosition, int64(index))
389+
require.Equal(t, job.ProvisionerJob.ID, jobs[index].ID)
390+
}
391+
}

dogfood/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ RUN mkdir --parents "$GOPATH" && \
5353
# charts and values files
5454
go install github.com/norwoodj/helm-docs/cmd/helm-docs@v1.5.0 && \
5555
# sqlc for Go code generation
56-
go install github.com/kyleconroy/sqlc/cmd/sqlc@v1.17.2 && \
56+
go install github.com/kyleconroy/sqlc/cmd/sqlc@v1.18.0 && \
5757
# gcr-cleaner-cli used by CI to prune unused images
5858
go install github.com/sethvargo/gcr-cleaner/cmd/gcr-cleaner-cli@v0.5.1 && \
5959
# ruleguard for checking custom rules, without needing to run all of

0 commit comments

Comments
 (0)