MCPcopy Index your code
hub / github.com/coder/coder / TestHeartbeatBumpsWorkspaceUsage

Function TestHeartbeatBumpsWorkspaceUsage

coderd/x/chatd/chatd_test.go:5508–5681  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

5506}
5507
5508func TestHeartbeatBumpsWorkspaceUsage(t *testing.T) {
5509 t.Parallel()
5510
5511 db, ps := dbtestutil.NewDB(t)
5512
5513 ctx := testutil.Context(t, testutil.WaitLong)
5514 user, org, model := seedChatDependencies(t, db)
5515 setOpenAIProviderBaseURL(ctx, t, db, chattest.NewOpenAI(t, func(req *chattest.OpenAIRequest) chattest.OpenAIResponse {
5516 if !req.Stream {
5517 return chattest.OpenAINonStreamingResponse("ok")
5518 }
5519 // Block until the request context is canceled so the chat
5520 // stays in a processing state long enough for heartbeats
5521 // to fire.
5522 chunks := make(chan chattest.OpenAIChunk)
5523 go func() {
5524 defer close(chunks)
5525 <-req.Context().Done()
5526 }()
5527 return chattest.OpenAIResponse{StreamingChunks: chunks}
5528 }))
5529
5530 // Create a workspace with a full build chain so we can verify
5531 // both last_used_at (dormancy) and deadline (autostop) bumps.
5532 tv := dbgen.TemplateVersion(t, db, database.TemplateVersion{
5533 OrganizationID: org.ID,
5534 CreatedBy: user.ID,
5535 })
5536 tmpl := dbgen.Template(t, db, database.Template{
5537 OrganizationID: org.ID,
5538 ActiveVersionID: tv.ID,
5539 CreatedBy: user.ID,
5540 })
5541 require.NoError(t, db.UpdateTemplateScheduleByID(ctx, database.UpdateTemplateScheduleByIDParams{
5542 ID: tmpl.ID,
5543 UpdatedAt: dbtime.Now(),
5544 AllowUserAutostop: true,
5545 ActivityBump: int64(time.Hour),
5546 }))
5547 ws := dbgen.Workspace(t, db, database.WorkspaceTable{
5548 OwnerID: user.ID,
5549 OrganizationID: org.ID,
5550 TemplateID: tmpl.ID,
5551 Ttl: sql.NullInt64{Valid: true, Int64: int64(8 * time.Hour)},
5552 })
5553 pj := dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{
5554 OrganizationID: org.ID,
5555 CompletedAt: sql.NullTime{
5556 Valid: true,
5557 Time: dbtime.Now().Add(-30 * time.Minute),
5558 },
5559 })
5560 // Build deadline is 30 minutes in the past, close enough to
5561 // be bumped by the default 1-hour activity bump.
5562 build := dbgen.WorkspaceBuild(t, db, database.WorkspaceBuild{
5563 WorkspaceID: ws.ID,
5564 TemplateVersionID: tv.ID,
5565 JobID: pj.ID,

Callers

nothing calls this directly

Calls 15

NewDBFunction · 0.92
ContextFunction · 0.92
NewOpenAIFunction · 0.92
TemplateVersionFunction · 0.92
TemplateFunction · 0.92
NowFunction · 0.92
WorkspaceFunction · 0.92
ProvisionerJobFunction · 0.92
WorkspaceBuildFunction · 0.92
NewTrackerFunction · 0.92
TrackerWithTickFlushFunction · 0.92

Tested by

no test coverage detected