WaitFor waits for the given criteria and fails the test if they are not met before the waiter's context is canceled.
(criteria ...WaitForAgentFn)
| 1341 | // WaitFor waits for the given criteria and fails the test if they are not met before the |
| 1342 | // waiter's context is canceled. |
| 1343 | func (w WorkspaceAgentWaiter) WaitFor(criteria ...WaitForAgentFn) { |
| 1344 | w.t.Helper() |
| 1345 | |
| 1346 | agentNamesMap := make(map[string]struct{}, len(w.agentNames)) |
| 1347 | for _, name := range w.agentNames { |
| 1348 | agentNamesMap[name] = struct{}{} |
| 1349 | } |
| 1350 | |
| 1351 | ctx := w.ctx |
| 1352 | if w.ctx == nil { |
| 1353 | ctx = testutil.Context(w.t, testutil.WaitLong) |
| 1354 | } |
| 1355 | |
| 1356 | w.t.Logf("waiting for workspace agents (workspace %s)", w.workspaceID) |
| 1357 | testutil.Eventually(ctx, w.t, func(ctx context.Context) bool { |
| 1358 | var err error |
| 1359 | workspace, err := w.client.Workspace(ctx, w.workspaceID) |
| 1360 | if err != nil { |
| 1361 | return false |
| 1362 | } |
| 1363 | if workspace.LatestBuild.Job.CompletedAt == nil { |
| 1364 | return false |
| 1365 | } |
| 1366 | if workspace.LatestBuild.Job.CompletedAt.IsZero() { |
| 1367 | return false |
| 1368 | } |
| 1369 | |
| 1370 | for _, resource := range workspace.LatestBuild.Resources { |
| 1371 | for _, agent := range resource.Agents { |
| 1372 | if len(w.agentNames) > 0 { |
| 1373 | if _, ok := agentNamesMap[agent.Name]; !ok { |
| 1374 | continue |
| 1375 | } |
| 1376 | } |
| 1377 | for _, criterium := range criteria { |
| 1378 | if !criterium(agent) { |
| 1379 | return false |
| 1380 | } |
| 1381 | } |
| 1382 | } |
| 1383 | } |
| 1384 | return true |
| 1385 | }, testutil.IntervalFast) |
| 1386 | } |
| 1387 | |
| 1388 | // Wait waits for the agent(s) to connect and fails the test if they do not connect before the |
| 1389 | // waiter's context is canceled. |