(t *testing.T)
| 48 | } |
| 49 | |
| 50 | func TestCache_TemplateWorkspaceOwners(t *testing.T) { |
| 51 | t.Parallel() |
| 52 | |
| 53 | var ( |
| 54 | ctx = testutil.Context(t, testutil.WaitShort) |
| 55 | log = testutil.Logger(t) |
| 56 | clock = quartz.NewMock(t) |
| 57 | ) |
| 58 | |
| 59 | trapTickerFunc := clock.Trap().TickerFunc("metricscache") |
| 60 | defer trapTickerFunc.Close() |
| 61 | |
| 62 | cache, db := newMetricsCache(t, log, clock, metricscache.Intervals{ |
| 63 | TemplateBuildTimes: time.Minute, |
| 64 | }, false) |
| 65 | |
| 66 | org := dbgen.Organization(t, db, database.Organization{}) |
| 67 | user1 := dbgen.User(t, db, database.User{}) |
| 68 | user2 := dbgen.User(t, db, database.User{}) |
| 69 | template := dbgen.Template(t, db, database.Template{ |
| 70 | OrganizationID: org.ID, |
| 71 | Provisioner: database.ProvisionerTypeEcho, |
| 72 | CreatedBy: user1.ID, |
| 73 | }) |
| 74 | |
| 75 | // Wait for both ticker functions to be created (template build times and deployment stats) |
| 76 | trapTickerFunc.MustWait(ctx).MustRelease(ctx) |
| 77 | trapTickerFunc.MustWait(ctx).MustRelease(ctx) |
| 78 | |
| 79 | clock.Advance(time.Minute).MustWait(ctx) |
| 80 | |
| 81 | count, ok := cache.TemplateWorkspaceOwners(template.ID) |
| 82 | require.True(t, ok, "TemplateWorkspaceOwners should be populated") |
| 83 | require.Equal(t, 0, count, "should have 0 owners initially") |
| 84 | |
| 85 | dbgen.Workspace(t, db, database.WorkspaceTable{ |
| 86 | OrganizationID: org.ID, |
| 87 | TemplateID: template.ID, |
| 88 | OwnerID: user1.ID, |
| 89 | }) |
| 90 | |
| 91 | clock.Advance(time.Minute).MustWait(ctx) |
| 92 | |
| 93 | count, _ = cache.TemplateWorkspaceOwners(template.ID) |
| 94 | require.Equal(t, 1, count, "should have 1 owner after adding workspace") |
| 95 | |
| 96 | workspace2 := dbgen.Workspace(t, db, database.WorkspaceTable{ |
| 97 | OrganizationID: org.ID, |
| 98 | TemplateID: template.ID, |
| 99 | OwnerID: user2.ID, |
| 100 | }) |
| 101 | |
| 102 | clock.Advance(time.Minute).MustWait(ctx) |
| 103 | |
| 104 | count, _ = cache.TemplateWorkspaceOwners(template.ID) |
| 105 | require.Equal(t, 2, count, "should have 2 owners after adding second workspace") |
| 106 | |
| 107 | // 3rd workspace should not be counted since we have the same owner as workspace2. |
nothing calls this directly
no test coverage detected