Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
allow blocking rollup init to handle data insertion
  • Loading branch information
mafredri committed Mar 22, 2024
commit 9c2d0851b59c47bd465b59c4573a896a75b468a1
12 changes: 11 additions & 1 deletion coderd/database/dbrollup/dbrollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const (
)

type Event struct {
TemplateUsageStats bool
Init bool `json:"-"`
TemplateUsageStats bool `json:"template_usage_stats"`
}

type Rolluper struct {
Expand Down Expand Up @@ -138,6 +139,15 @@ func (r *Rolluper) start(ctx context.Context) {
}
}

// For testing.
if r.event != nil {
select {
case <-ctx.Done():
return
case r.event <- Event{Init: true}:
}
}

// Perform do immediately and on every tick of the ticker,
// disregarding the execution time of do. This ensure that
// the rollup is performed every interval assuming do does
Expand Down
4 changes: 4 additions & 0 deletions coderd/database/dbrollup/dbrollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func TestRollup_TwoInstancesUseLocking(t *testing.T) {
)
defer closeRolluper(rolluper2, resume2)

_, _ = <-events1, <-events2 // Deplete init event, resume operation.

ctx := testutil.Context(t, testutil.WaitMedium)

// One of the rollup instances should roll up and the other should not.
Expand Down Expand Up @@ -222,6 +224,8 @@ func TestRollupTemplateUsageStats(t *testing.T) {
rolluper := dbrollup.New(logger, db, dbrollup.WithInterval(250*time.Millisecond), dbrollup.WithEventChannel(events))
defer rolluper.Close()

<-events // Deplete init event, resume operation.

ctx := testutil.Context(t, testutil.WaitMedium)

select {
Expand Down
3 changes: 0 additions & 3 deletions coderd/insights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2024,9 +2024,6 @@ func TestUserActivityInsights_Golden(t *testing.T) {
templates, users, testData := prepareFixtureAndTestData(t, tt.makeFixture, tt.makeTestData)
client, events := prepare(t, templates, users, testData)

// TODO(mafredri): Remove the need for this.
time.Sleep(3 * time.Second)

for _, req := range tt.requests {
req := req
t.Run(req.name, func(t *testing.T) {
Expand Down