Skip to content

Conversation

@Stebalien
Copy link
Member

This isn't really necessary for small-scale tests, but... I wanted to clean some things up a bit.

  1. Don't advance time all at once, advance to each deadline.
  2. Use a heap instead of constantly sorting/iterating.
  3. Modernize the context logic to avoid extra goroutines.
  4. Make calling Set/Add/etc. thread-safe. All callers will cooperatively advance the clock until the requested time.

// WARNING: Tickers will cause this function to continue forever until something cancels the ticker.
// It will sleep after each bit of progress to the tickers a chance to run, but using this with
// tickers is not recommended.
func (m *Mock) WaitForAllTimers() time.Time {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add a RunNextTimer later, that should be fairly trivial.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RunNextTimer or a way to cancel WaitForAllTimers would be nice.

t.stopped = true
t.mock.removeClockTimer((*internalTimer)(t))
// Ticker is a mock implementation of [time.Ticker].
type Ticker struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restructured to be smaller (when not using mocktime) and to avoid having to abstract over Ticker versus Timer in the mock logic.

1. Don't advance time all at once, advance to each deadline.
2. Use a heap instead of constantly sorting/iterating.
3. Modernize the context logic to avoid extra goroutines.
4. Make calling Set/Add/etc. thread-safe. All callers will cooperatively
advance the clock until the requested time.
Also improve reliability of immediately firing timers.
Copy link

@Kubuxu Kubuxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM assuming tests pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants