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

Commite02ef6f

Browse files
authored
chore: executor_test: reduce test execution time (#1876)
Removes 5-second wait in autobuild.executor unit tests:- Adds a write-only channel to Executor and plumbs through to unit tests- Modifies runOnce to return an executor.RunStats struct and write to statsCh if not nil
1 parentae4b2d8 commite02ef6f

File tree

3 files changed

+182
-129
lines changed

3 files changed

+182
-129
lines changed

‎coderd/autobuild/executor/lifecycle_executor.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ import (
1717

1818
// Executor automatically starts or stops workspaces.
1919
typeExecutorstruct {
20-
ctx context.Context
21-
db database.Store
22-
log slog.Logger
23-
tick<-chan time.Time
20+
ctx context.Context
21+
db database.Store
22+
log slog.Logger
23+
tick<-chan time.Time
24+
statsChchan<-Stats
25+
}
26+
27+
// Stats contains information about one run of Executor.
28+
typeStatsstruct {
29+
Transitionsmap[uuid.UUID]database.WorkspaceTransition
30+
Elapsed time.Duration
31+
Errorerror
2432
}
2533

2634
// New returns a new autobuild executor.
@@ -34,22 +42,42 @@ func New(ctx context.Context, db database.Store, log slog.Logger, tick <-chan ti
3442
returnle
3543
}
3644

45+
// WithStatsChannel will cause Executor to push a RunStats to ch after
46+
// every tick.
47+
func (e*Executor)WithStatsChannel(chchan<-Stats)*Executor {
48+
e.statsCh=ch
49+
returne
50+
}
51+
3752
// Run will cause executor to start or stop workspaces on every
3853
// tick from its channel. It will stop when its context is Done, or when
3954
// its channel is closed.
4055
func (e*Executor)Run() {
4156
gofunc() {
4257
fort:=rangee.tick {
43-
iferr:=e.runOnce(t);err!=nil {
44-
e.log.Error(e.ctx,"error running once",slog.Error(err))
58+
stats:=e.runOnce(t)
59+
ifstats.Error!=nil {
60+
e.log.Error(e.ctx,"error running once",slog.Error(stats.Error))
4561
}
62+
ife.statsCh!=nil {
63+
e.statsCh<-stats
64+
}
65+
e.log.Debug(e.ctx,"run stats",slog.F("elapsed",stats.Elapsed),slog.F("transitions",stats.Transitions))
4666
}
4767
}()
4868
}
4969

50-
func (e*Executor)runOnce(t time.Time)error {
70+
func (e*Executor)runOnce(t time.Time)Stats {
71+
varerrerror
72+
stats:=Stats{
73+
Transitions:make(map[uuid.UUID]database.WorkspaceTransition),
74+
}
75+
deferfunc() {
76+
stats.Elapsed=time.Since(t)
77+
stats.Error=err
78+
}()
5179
currentTick:=t.Truncate(time.Minute)
52-
returne.db.InTx(func(db database.Store)error {
80+
err=e.db.InTx(func(db database.Store)error {
5381
// TTL is set at the workspace level, and deadline at the workspace build level.
5482
// When a workspace build is created, its deadline initially starts at zero.
5583
// When provisionerd successfully completes a provision job, the deadline is
@@ -146,6 +174,7 @@ func (e *Executor) runOnce(t time.Time) error {
146174
slog.F("transition",validTransition),
147175
)
148176

177+
stats.Transitions[ws.ID]=validTransition
149178
iferr:=build(e.ctx,db,ws,validTransition,priorHistory,priorJob);err!=nil {
150179
e.log.Error(e.ctx,"unable to transition workspace",
151180
slog.F("workspace_id",ws.ID),
@@ -156,6 +185,7 @@ func (e *Executor) runOnce(t time.Time) error {
156185
}
157186
returnnil
158187
})
188+
returnstats
159189
}
160190

161191
// TODO(cian): this function duplicates most of api.postWorkspaceBuilds. Refactor.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp