chore: refactor notifier to use quartz.TickerFunc (#15134)

In investigating https://github.com/coder/internal/issues/109 I noticed many of the notification tests are still using `time.Sleep` and `require.Eventually`. This is an initial effort to start converting these to Quartz.

One product change is to switch the `notifier` to use a `TickerFunc` instead of a normal Ticker, since it allows the test to assert that a batch process is complete via the Quartz `Mock` clock.  This does introduce one slight behavioral change in that the notifier waits the fetch interval before processing its first batch.  In practice, this is inconsequential: no one will notice if we send notifications immediately on startup, or just a little later.

But, it does make a difference to some tests, which are fixed up here.
This commit is contained in:
Spike Curtis
2024-10-21 12:07:19 +04:00
committed by GitHub
parent 8c8bd3141f
commit 29099d4727
7 changed files with 167 additions and 93 deletions

View File

@ -92,3 +92,43 @@ func (i *dispatchInterceptor) Dispatcher(payload types.MessagePayload, title, bo
return retryable, err
}, nil
}
type dispatchCall struct {
payload types.MessagePayload
title, body string
result chan<- dispatchResult
}
type dispatchResult struct {
retryable bool
err error
}
type chanHandler struct {
calls chan dispatchCall
}
func (c chanHandler) Dispatcher(payload types.MessagePayload, title, body string) (dispatch.DeliveryFunc, error) {
result := make(chan dispatchResult)
call := dispatchCall{
payload: payload,
title: title,
body: body,
result: result,
}
return func(ctx context.Context, _ uuid.UUID) (bool, error) {
select {
case c.calls <- call:
select {
case r := <-result:
return r.retryable, r.err
case <-ctx.Done():
return false, ctx.Err()
}
case <-ctx.Done():
return false, ctx.Err()
}
}, nil
}
var _ notifications.Handler = &chanHandler{}