diff --git a/backend/internal/api/v2/handlers/hosted_capacity_test.go b/backend/internal/api/v2/handlers/hosted_capacity_test.go index 700e4b0..f04281d 100644 --- a/backend/internal/api/v2/handlers/hosted_capacity_test.go +++ b/backend/internal/api/v2/handlers/hosted_capacity_test.go @@ -16,9 +16,11 @@ import ( "encoding/json" "net/http" "testing" + "time" "github.com/google/uuid" "github.com/michielvha/stackweaver/backend/internal/services/rbac" + "github.com/michielvha/stackweaver/backend/internal/services/runner" "github.com/michielvha/stackweaver/core/models" "github.com/michielvha/stackweaver/core/repository" ) @@ -31,7 +33,10 @@ func TestHostedCapacity_ReadByMembers(t *testing.T) { pool.PlacementGroup, pool.MaxConcurrentJobs = group, &capV proj := &models.Project{ID: uuid.New(), OrganizationID: f.orgA.ID, Name: "hostedcap-" + f.sfx} ws := &models.Workspace{ProjectID: proj.ID, Name: "hostedcap-ws-" + f.sfx, ExecutionMode: "remote"} - launcher := &models.Runner{ID: uuid.New(), Kind: models.RunnerKindHosted, HostedGroup: group, Name: "hostedcap-l-" + f.sfx, Status: models.RunnerStatusOnline} + // An online launcher has a fresh heartbeat. Without one, the runner monitor's sweep marks it + // offline, and other packages' tests run that sweep over the same database at any time. + heartbeat := time.Now() + launcher := &models.Runner{ID: uuid.New(), Kind: models.RunnerKindHosted, HostedGroup: group, Name: "hostedcap-l-" + f.sfx, Status: models.RunnerStatusOnline, LastHeartbeatAt: &heartbeat} for _, obj := range []any{pool, proj, ws, launcher} { if err := f.db.Create(obj).Error; err != nil { t.Fatal(err) @@ -56,6 +61,11 @@ func TestHostedCapacity_ReadByMembers(t *testing.T) { f.db.Where("id = ?", pool.ID).Delete(&models.AgentPool{}) }) + // The sweep another package's test may run meanwhile leaves the launcher online. + if _, err := repository.NewRunnerRepository(f.db).MarkOfflineIfStale(runner.OfflineAfter); err != nil { + t.Fatalf("sweeping stale runners: %v", err) + } + h := NewHostedCapacityHandler(repository.NewOrganizationRepository(f.db), repository.NewAgentPoolRepository(f.db), repository.NewRunRepository(f.db), f.authSvc, rbac.NewServiceWithTeams(repository.NewOrganizationRepository(f.db), repository.NewTeamRepository(f.db), repository.NewProjectRepository(f.db))) r := platformAdminRouter()