@@ -1128,6 +1128,7 @@ type WorkspaceAgentWaiter struct {
11281128workspaceID uuid.UUID
11291129agentNames []string
11301130resourcesMatcher func ([]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
11561157return w
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 (w WorkspaceAgentWaiter )WithContext (ctx context.Context )WorkspaceAgentWaiter {
1163+ //nolint: revive // returns modified struct
1164+ w .ctx = ctx
1165+ return w
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 {
11761185return ! 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.
11791190func (w WorkspaceAgentWaiter )WaitFor (criteria ... WaitForAgentFn ) {
11801191w .t .Helper ()
11811192
@@ -1184,11 +1195,13 @@ func (w WorkspaceAgentWaiter) WaitFor(criteria ...WaitForAgentFn) {
11841195agentNamesMap [name ]= struct {}{}
11851196}
11861197
1187- ctx ,cancel := context .WithTimeout (context .Background (),testutil .WaitLong )
1188- defer cancel ()
1198+ ctx := w .ctx
1199+ if w .ctx == nil {
1200+ ctx = testutil .Context (w .t ,testutil .WaitLong )
1201+ }
11891202
11901203w .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 {
11921205var err error
11931206workspace ,err := w .client .Workspace (ctx ,w .workspaceID )
11941207if err != nil {
@@ -1216,10 +1229,11 @@ func (w WorkspaceAgentWaiter) WaitFor(criteria ...WaitForAgentFn) {
12161229}
12171230}
12181231return true
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.
12231237func (w WorkspaceAgentWaiter )Wait () []codersdk.WorkspaceResource {
12241238w .t .Helper ()
12251239
@@ -1228,12 +1242,14 @@ func (w WorkspaceAgentWaiter) Wait() []codersdk.WorkspaceResource {
12281242agentNamesMap [name ]= struct {}{}
12291243}
12301244
1231- ctx ,cancel := context .WithTimeout (context .Background (),testutil .WaitLong )
1232- defer cancel ()
1245+ ctx := w .ctx
1246+ if w .ctx == nil {
1247+ ctx = testutil .Context (w .t ,testutil .WaitLong )
1248+ }
12331249
12341250w .t .Logf ("waiting for workspace agents (workspace %s)" ,w .workspaceID )
12351251var resources []codersdk.WorkspaceResource
1236- require .Eventually (w .t ,func ()bool {
1252+ testutil .Eventually (ctx , w .t ,func (ctx context. Context )bool {
12371253var err error
12381254workspace ,err := w .client .Workspace (ctx ,w .workspaceID )
12391255if err != nil {
@@ -1265,7 +1281,7 @@ func (w WorkspaceAgentWaiter) Wait() []codersdk.WorkspaceResource {
12651281return true
12661282}
12671283return w .resourcesMatcher (resources )
1268- },testutil .WaitLong , testutil . IntervalMedium )
1284+ },testutil .IntervalMedium )
12691285w .t .Logf ("got workspace agents (workspace %s)" ,w .workspaceID )
12701286return resources
12711287}