Skip to content

Commit 0811a14

Browse files
authored
chore(e2e): move to events instead of polling when waiting for agentapi (#141)
1 parent e637dda commit 0811a14

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

e2e/echo_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -236,27 +236,28 @@ func waitAgentAPIStable(ctx context.Context, t testing.TB, apiClient *agentapisd
236236
defer waitCancel()
237237

238238
start := time.Now()
239-
tick := time.NewTicker(time.Millisecond)
240-
defer tick.Stop()
241-
var prevStatus agentapisdk.AgentStatus
239+
var currStatus agentapisdk.AgentStatus
242240
defer func() {
243241
elapsed := time.Since(start)
244-
t.Logf("%s: agent API status: %s (elapsed: %s)", msg, prevStatus, elapsed.Round(100*time.Millisecond))
242+
t.Logf("%s: agent API status: %s (elapsed: %s)", msg, currStatus, elapsed.Round(100*time.Millisecond))
245243
}()
244+
evts, errs, err := apiClient.SubscribeEvents(ctx)
245+
require.NoError(t, err, "failed to subscribe to events")
246246
for {
247247
select {
248248
case <-waitCtx.Done():
249249
return waitCtx.Err()
250-
case <-tick.C:
251-
tick.Reset(100 * time.Millisecond)
252-
sr, err := apiClient.GetStatus(ctx)
253-
if err != nil {
254-
continue
255-
}
256-
prevStatus = sr.Status
257-
if sr.Status == agentapisdk.StatusStable {
258-
return nil
250+
case evt := <-evts:
251+
if esc, ok := evt.(agentapisdk.EventStatusChange); ok {
252+
currStatus = esc.Status
253+
if currStatus == agentapisdk.StatusStable {
254+
return nil
255+
}
256+
} else {
257+
t.Logf("Got %T event", evt)
259258
}
259+
case err := <-errs:
260+
return fmt.Errorf("read events: %w", err)
260261
}
261262
}
262263
}

0 commit comments

Comments
 (0)