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

Commit1a10475

Browse files
authored
chore(coderd): use WorkspaceAgentWaiter.WithContext in aitasks_test.go (#20360)
Fixescoder/internal#1067- Adds `WorkspaceAgentWaiter.WithContext()`- Updates usage of `WorkspaceAgentWaiter` in `aitasks_test.go` withcontext bumped to `testutil.WaitMedium`Authored by Claude with manual review and updates.
1 parent9ee3a91 commit1a10475

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

‎coderd/aitasks_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,9 @@ func TestTasks(t *testing.T) {
519519
_=agenttest.New(t,client.URL,authToken,func(o*agent.Options) {
520520
o.Client=agentClient
521521
})
522-
coderdtest.NewWorkspaceAgentWaiter(t,client,ws.ID).WaitFor(coderdtest.AgentsReady)
523522

524523
ctx:=testutil.Context(t,testutil.WaitMedium)
524+
coderdtest.NewWorkspaceAgentWaiter(t,client,ws.ID).WithContext(ctx).WaitFor(coderdtest.AgentsReady)
525525

526526
// Lookup the sidebar app ID.
527527
w,err:=client.Workspace(ctx,ws.ID)
@@ -712,7 +712,7 @@ func TestTasks(t *testing.T) {
712712
_=agenttest.New(t,client.URL,authToken,func(o*agent.Options) {
713713
o.Client=agentClient
714714
})
715-
coderdtest.NewWorkspaceAgentWaiter(t,client,ws.ID).WaitFor(coderdtest.AgentsReady)
715+
coderdtest.NewWorkspaceAgentWaiter(t,client,ws.ID).WithContext(ctx).WaitFor(coderdtest.AgentsReady)
716716

717717
// Omit sidebar app health as undefined is OK.
718718

@@ -762,7 +762,7 @@ func TestTasks(t *testing.T) {
762762
_=agenttest.New(t,client.URL,authToken,func(o*agent.Options) {
763763
o.Client=agentClient
764764
})
765-
coderdtest.NewWorkspaceAgentWaiter(t,client,ws.ID).WaitFor(coderdtest.AgentsReady)
765+
coderdtest.NewWorkspaceAgentWaiter(t,client,ws.ID).WithContext(ctx).WaitFor(coderdtest.AgentsReady)
766766

767767
exp:=codersdk.NewExperimentalClient(client)
768768
_,err:=exp.TaskLogs(ctx,"me",ws.ID)

‎coderd/coderdtest/coderdtest.go‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,7 @@ type WorkspaceAgentWaiter struct {
11281128
workspaceID uuid.UUID
11291129
agentNames []string
11301130
resourcesMatcherfunc([]codersdk.WorkspaceResource)bool
1131+
ctx context.Context
11311132
}
11321133

11331134
// NewWorkspaceAgentWaiter returns an object that waits for agents to connect when
@@ -1156,6 +1157,14 @@ func (w WorkspaceAgentWaiter) MatchResources(m func([]codersdk.WorkspaceResource
11561157
returnw
11571158
}
11581159

1160+
// WithContext instructs the waiter to use the provided context for all operations.
1161+
// If not specified, the waiter will create its own context with testutil.WaitLong timeout.
1162+
func (wWorkspaceAgentWaiter)WithContext(ctx context.Context)WorkspaceAgentWaiter {
1163+
//nolint: revive // returns modified struct
1164+
w.ctx=ctx
1165+
returnw
1166+
}
1167+
11591168
// WaitForAgentFn represents a boolean assertion to be made against each agent
11601169
// that a given WorkspaceAgentWaited knows about. Each WaitForAgentFn should apply
11611170
// the check to a single agent, but it should be named for plural, because `func (w WorkspaceAgentWaiter) WaitFor`
@@ -1176,6 +1185,8 @@ func AgentsNotReady(agent codersdk.WorkspaceAgent) bool {
11761185
return!AgentsReady(agent)
11771186
}
11781187

1188+
// WaitFor waits for the given criteria and fails the test if they are not met before the
1189+
// waiter's context is canceled.
11791190
func (wWorkspaceAgentWaiter)WaitFor(criteria...WaitForAgentFn) {
11801191
w.t.Helper()
11811192

@@ -1184,11 +1195,13 @@ func (w WorkspaceAgentWaiter) WaitFor(criteria ...WaitForAgentFn) {
11841195
agentNamesMap[name]=struct{}{}
11851196
}
11861197

1187-
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitLong)
1188-
defercancel()
1198+
ctx:=w.ctx
1199+
ifw.ctx==nil {
1200+
ctx=testutil.Context(w.t,testutil.WaitLong)
1201+
}
11891202

11901203
w.t.Logf("waiting for workspace agents (workspace %s)",w.workspaceID)
1191-
require.Eventually(w.t,func()bool {
1204+
testutil.Eventually(ctx,w.t,func(ctx context.Context)bool {
11921205
varerrerror
11931206
workspace,err:=w.client.Workspace(ctx,w.workspaceID)
11941207
iferr!=nil {
@@ -1216,10 +1229,11 @@ func (w WorkspaceAgentWaiter) WaitFor(criteria ...WaitForAgentFn) {
12161229
}
12171230
}
12181231
returntrue
1219-
},testutil.WaitLong,testutil.IntervalMedium)
1232+
},testutil.IntervalMedium)
12201233
}
12211234

1222-
// Wait waits for the agent(s) to connect and fails the test if they do not within testutil.WaitLong
1235+
// Wait waits for the agent(s) to connect and fails the test if they do not connect before the
1236+
// waiter's context is canceled.
12231237
func (wWorkspaceAgentWaiter)Wait() []codersdk.WorkspaceResource {
12241238
w.t.Helper()
12251239

@@ -1228,12 +1242,14 @@ func (w WorkspaceAgentWaiter) Wait() []codersdk.WorkspaceResource {
12281242
agentNamesMap[name]=struct{}{}
12291243
}
12301244

1231-
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitLong)
1232-
defercancel()
1245+
ctx:=w.ctx
1246+
ifw.ctx==nil {
1247+
ctx=testutil.Context(w.t,testutil.WaitLong)
1248+
}
12331249

12341250
w.t.Logf("waiting for workspace agents (workspace %s)",w.workspaceID)
12351251
varresources []codersdk.WorkspaceResource
1236-
require.Eventually(w.t,func()bool {
1252+
testutil.Eventually(ctx,w.t,func(ctx context.Context)bool {
12371253
varerrerror
12381254
workspace,err:=w.client.Workspace(ctx,w.workspaceID)
12391255
iferr!=nil {
@@ -1265,7 +1281,7 @@ func (w WorkspaceAgentWaiter) Wait() []codersdk.WorkspaceResource {
12651281
returntrue
12661282
}
12671283
returnw.resourcesMatcher(resources)
1268-
},testutil.WaitLong,testutil.IntervalMedium)
1284+
},testutil.IntervalMedium)
12691285
w.t.Logf("got workspace agents (workspace %s)",w.workspaceID)
12701286
returnresources
12711287
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp