Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb4fd819

Browse files
authored
test(coderd): enabledbrollup service for insights tests (#12673)
1 parent12e6fbf commitb4fd819

File tree

3 files changed

+83
-16
lines changed

3 files changed

+83
-16
lines changed

‎coderd/database/dbrollup/dbrollup.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const (
2121
)
2222

2323
typeEventstruct {
24-
TemplateUsageStatsbool
24+
Initbool`json:"-"`
25+
TemplateUsageStatsbool`json:"template_usage_stats"`
2526
}
2627

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

142+
// For testing.
143+
ifr.event!=nil {
144+
select {
145+
case<-ctx.Done():
146+
return
147+
caser.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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ func TestRollup_TwoInstancesUseLocking(t *testing.T) {
110110
)
111111
defercloseRolluper(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
deferrolluper.Close()
224226

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

227231
select {

‎coderd/insights_test.go

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/coder/coder/v2/coderd/database"
2727
"github.com/coder/coder/v2/coderd/database/dbauthz"
2828
"github.com/coder/coder/v2/coderd/database/dbgen"
29+
"github.com/coder/coder/v2/coderd/database/dbrollup"
2930
"github.com/coder/coder/v2/coderd/database/dbtestutil"
3031
"github.com/coder/coder/v2/coderd/rbac"
3132
"github.com/coder/coder/v2/coderd/workspaceapps"
@@ -44,10 +45,19 @@ func TestDeploymentInsights(t *testing.T) {
4445
clientTz,err:=time.LoadLocation("America/Chicago")
4546
require.NoError(t,err)
4647

48+
db,ps:=dbtestutil.NewDB(t)
49+
logger:=slogtest.Make(t,nil)
4750
client:=coderdtest.New(t,&coderdtest.Options{
48-
IncludeProvisionerDaemon:true,
49-
AgentStatsRefreshInterval:time.Millisecond*100,
50-
MetricsCacheRefreshInterval:time.Millisecond*100,
51+
Database:db,
52+
Pubsub:ps,
53+
Logger:&logger,
54+
IncludeProvisionerDaemon:true,
55+
AgentStatsRefreshInterval:time.Millisecond*50,
56+
DatabaseRolluper:dbrollup.New(
57+
logger.Named("dbrollup"),
58+
db,
59+
dbrollup.WithInterval(time.Millisecond*100),
60+
),
5161
})
5262

5363
user:=coderdtest.CreateFirstUser(t,client)
@@ -119,10 +129,19 @@ func TestDeploymentInsights(t *testing.T) {
119129
funcTestUserActivityInsights_SanityCheck(t*testing.T) {
120130
t.Parallel()
121131

132+
db,ps:=dbtestutil.NewDB(t)
122133
logger:=slogtest.Make(t,nil)
123134
client:=coderdtest.New(t,&coderdtest.Options{
135+
Database:db,
136+
Pubsub:ps,
137+
Logger:&logger,
124138
IncludeProvisionerDaemon:true,
125-
AgentStatsRefreshInterval:time.Millisecond*100,
139+
AgentStatsRefreshInterval:time.Millisecond*50,
140+
DatabaseRolluper:dbrollup.New(
141+
logger.Named("dbrollup"),
142+
db,
143+
dbrollup.WithInterval(time.Millisecond*100),
144+
),
126145
})
127146

128147
// Create two users, one that will appear in the report and another that
@@ -207,10 +226,19 @@ func TestUserActivityInsights_SanityCheck(t *testing.T) {
207226
funcTestUserLatencyInsights(t*testing.T) {
208227
t.Parallel()
209228

229+
db,ps:=dbtestutil.NewDB(t)
210230
logger:=slogtest.Make(t,nil)
211231
client:=coderdtest.New(t,&coderdtest.Options{
232+
Database:db,
233+
Pubsub:ps,
234+
Logger:&logger,
212235
IncludeProvisionerDaemon:true,
213-
AgentStatsRefreshInterval:time.Millisecond*100,
236+
AgentStatsRefreshInterval:time.Millisecond*50,
237+
DatabaseRolluper:dbrollup.New(
238+
logger.Named("dbrollup"),
239+
db,
240+
dbrollup.WithInterval(time.Millisecond*100),
241+
),
214242
})
215243

216244
// Create two users, one that will appear in the report and another that
@@ -474,16 +502,24 @@ func TestTemplateInsights_Golden(t *testing.T) {
474502
returntemplates,users,testData
475503
}
476504

477-
prepare:=func(t*testing.T,templates []*testTemplate,users []*testUser,testDatamap[*testWorkspace]testDataGen)*codersdk.Client {
505+
prepare:=func(t*testing.T,templates []*testTemplate,users []*testUser,testDatamap[*testWorkspace]testDataGen)(*codersdk.Client,chan dbrollup.Event) {
478506
logger:=slogtest.Make(t,&slogtest.Options{IgnoreErrors:false}).Leveled(slog.LevelDebug)
479-
db,pubsub:=dbtestutil.NewDB(t)
507+
db,ps:=dbtestutil.NewDB(t)
508+
events:=make(chan dbrollup.Event)
480509
client:=coderdtest.New(t,&coderdtest.Options{
481510
Database:db,
482-
Pubsub:pubsub,
511+
Pubsub:ps,
483512
Logger:&logger,
484513
IncludeProvisionerDaemon:true,
485514
AgentStatsRefreshInterval:time.Hour,// Not relevant for this test.
515+
DatabaseRolluper:dbrollup.New(
516+
logger.Named("dbrollup"),
517+
db,
518+
dbrollup.WithInterval(time.Millisecond*50),
519+
dbrollup.WithEventChannel(events),
520+
),
486521
})
522+
487523
firstUser:=coderdtest.CreateFirstUser(t,client)
488524

489525
// Prepare all test users.
@@ -706,7 +742,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
706742
err=reporter.Report(dbauthz.AsSystemRestricted(ctx),stats)
707743
require.NoError(t,err,"want no error inserting app stats")
708744

709-
returnclient
745+
returnclient,events
710746
}
711747

712748
baseTemplateAndUserFixture:=func() ([]*testTemplate, []*testUser) {
@@ -1200,7 +1236,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
12001236
require.NotNil(t,tt.makeFixture,"test bug: makeFixture must be set")
12011237
require.NotNil(t,tt.makeTestData,"test bug: makeTestData must be set")
12021238
templates,users,testData:=prepareFixtureAndTestData(t,tt.makeFixture,tt.makeTestData)
1203-
client:=prepare(t,templates,users,testData)
1239+
client,events:=prepare(t,templates,users,testData)
12041240

12051241
for_,req:=rangett.requests {
12061242
req:=req
@@ -1209,6 +1245,11 @@ func TestTemplateInsights_Golden(t *testing.T) {
12091245

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

1248+
// Drain two events, the first one resumes rolluper
1249+
// operation and the second one waits for the rollup
1250+
// to complete.
1251+
_,_=<-events,<-events
1252+
12121253
report,err:=client.TemplateInsights(ctx,req.makeRequest(templates))
12131254
require.NoError(t,err,"want no error getting template insights")
12141255

@@ -1381,15 +1422,22 @@ func TestUserActivityInsights_Golden(t *testing.T) {
13811422
returntemplates,users,testData
13821423
}
13831424

1384-
prepare:=func(t*testing.T,templates []*testTemplate,users []*testUser,testDatamap[*testWorkspace]testDataGen)*codersdk.Client {
1425+
prepare:=func(t*testing.T,templates []*testTemplate,users []*testUser,testDatamap[*testWorkspace]testDataGen)(*codersdk.Client,chan dbrollup.Event) {
13851426
logger:=slogtest.Make(t,&slogtest.Options{IgnoreErrors:false}).Leveled(slog.LevelDebug)
1386-
db,pubsub:=dbtestutil.NewDB(t)
1427+
db,ps:=dbtestutil.NewDB(t)
1428+
events:=make(chan dbrollup.Event)
13871429
client:=coderdtest.New(t,&coderdtest.Options{
13881430
Database:db,
1389-
Pubsub:pubsub,
1431+
Pubsub:ps,
13901432
Logger:&logger,
13911433
IncludeProvisionerDaemon:true,
13921434
AgentStatsRefreshInterval:time.Hour,// Not relevant for this test.
1435+
DatabaseRolluper:dbrollup.New(
1436+
logger.Named("dbrollup"),
1437+
db,
1438+
dbrollup.WithInterval(time.Millisecond*50),
1439+
dbrollup.WithEventChannel(events),
1440+
),
13931441
})
13941442
firstUser:=coderdtest.CreateFirstUser(t,client)
13951443

@@ -1593,7 +1641,7 @@ func TestUserActivityInsights_Golden(t *testing.T) {
15931641
err=reporter.Report(dbauthz.AsSystemRestricted(ctx),stats)
15941642
require.NoError(t,err,"want no error inserting app stats")
15951643

1596-
returnclient
1644+
returnclient,events
15971645
}
15981646

15991647
baseTemplateAndUserFixture:=func() ([]*testTemplate, []*testUser) {
@@ -1974,7 +2022,7 @@ func TestUserActivityInsights_Golden(t *testing.T) {
19742022
require.NotNil(t,tt.makeFixture,"test bug: makeFixture must be set")
19752023
require.NotNil(t,tt.makeTestData,"test bug: makeTestData must be set")
19762024
templates,users,testData:=prepareFixtureAndTestData(t,tt.makeFixture,tt.makeTestData)
1977-
client:=prepare(t,templates,users,testData)
2025+
client,events:=prepare(t,templates,users,testData)
19782026

19792027
for_,req:=rangett.requests {
19802028
req:=req
@@ -1983,6 +2031,11 @@ func TestUserActivityInsights_Golden(t *testing.T) {
19832031

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

2034+
// Drain two events, the first one resumes rolluper
2035+
// operation and the second one waits for the rollup
2036+
// to complete.
2037+
_,_=<-events,<-events
2038+
19862039
report,err:=client.UserActivityInsights(ctx,req.makeRequest(templates))
19872040
require.NoError(t,err,"want no error getting template insights")
19882041

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp