mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
test: fix TestCache_DeploymentStats flake (#19683)
Closes https://github.com/coder/internal/issues/961 Likely the same deal as in #19599, the body of `require.Eventually` now fires immediately, when it used to fire after 250ms (the interval). Presumably, the deployment stats become ready before the vs code session count gets incremented. This was never an issue with the 250ms delay, as this flake has only cropped up after the testify version bump. We'll fix the issue by making it possible to wait for a full metrics cache refresh, i.e. removing `require.Eventually` in this test altogether.
This commit is contained in:
@@ -181,16 +181,14 @@ func (c *Cache) refreshDeploymentStats(ctx context.Context) error {
|
||||
|
||||
func (c *Cache) run(ctx context.Context, name string, interval time.Duration, refresh func(context.Context) error) {
|
||||
logger := c.log.With(slog.F("name", name), slog.F("interval", interval))
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
tickerFunc := func() error {
|
||||
start := c.clock.Now()
|
||||
for r := retry.New(time.Millisecond*100, time.Minute); r.Wait(ctx); {
|
||||
start := time.Now()
|
||||
err := refresh(ctx)
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
if xerrors.Is(err, sql.ErrNoRows) {
|
||||
break
|
||||
@@ -198,18 +196,17 @@ func (c *Cache) run(ctx context.Context, name string, interval time.Duration, re
|
||||
logger.Error(ctx, "refresh metrics failed", slog.Error(err))
|
||||
continue
|
||||
}
|
||||
logger.Debug(ctx, "metrics refreshed", slog.F("took", time.Since(start)))
|
||||
logger.Debug(ctx, "metrics refreshed", slog.F("took", c.clock.Since(start)))
|
||||
break
|
||||
}
|
||||
|
||||
select {
|
||||
case <-ticker.C:
|
||||
case <-c.done:
|
||||
return
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Call once immediately before starting ticker
|
||||
_ = tickerFunc()
|
||||
|
||||
tkr := c.clock.TickerFunc(ctx, interval, tickerFunc, "metricscache", name)
|
||||
_ = tkr.Wait()
|
||||
}
|
||||
|
||||
func (c *Cache) Close() error {
|
||||
|
||||
Reference in New Issue
Block a user