Skip to content

Commit 9c2d085

Browse files
committed
allow blocking rollup init to handle data insertion
1 parent 60d812d commit 9c2d085

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

coderd/database/dbrollup/dbrollup.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const (
2121
)
2222

2323
type Event struct {
24-
TemplateUsageStats bool
24+
Init bool `json:"-"`
25+
TemplateUsageStats bool `json:"template_usage_stats"`
2526
}
2627

2728
type Rolluper struct {
@@ -138,6 +139,15 @@ func (r *Rolluper) start(ctx context.Context) {
138139
}
139140
}
140141

142+
// For testing.
143+
if r.event != nil {
144+
select {
145+
case <-ctx.Done():
146+
return
147+
case r.event <- Event{Init: true}:
148+
}
149+
}
150+
141151
// Perform do immediately and on every tick of the ticker,
142152
// disregarding the execution time of do. This ensure that
143153
// the rollup is performed every interval assuming do does

coderd/database/dbrollup/dbrollup_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ func TestRollup_TwoInstancesUseLocking(t *testing.T) {
110110
)
111111
defer closeRolluper(rolluper2, resume2)
112112

113+
_, _ = <-events1, <-events2 // Deplete init event, resume operation.
114+
113115
ctx := testutil.Context(t, testutil.WaitMedium)
114116

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

227+
<-events // Deplete init event, resume operation.
228+
225229
ctx := testutil.Context(t, testutil.WaitMedium)
226230

227231
select {

coderd/insights_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -2024,9 +2024,6 @@ func TestUserActivityInsights_Golden(t *testing.T) {
20242024
templates, users, testData := prepareFixtureAndTestData(t, tt.makeFixture, tt.makeTestData)
20252025
client, events := prepare(t, templates, users, testData)
20262026

2027-
// TODO(mafredri): Remove the need for this.
2028-
time.Sleep(3 * time.Second)
2029-
20302027
for _, req := range tt.requests {
20312028
req := req
20322029
t.Run(req.name, func(t *testing.T) {

0 commit comments

Comments
 (0)