Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global sleep function for testing #2

Open
pda opened this issue Sep 2, 2022 · 0 comments
Open

Global sleep function for testing #2

pda opened this issue Sep 2, 2022 · 0 comments

Comments

@pda
Copy link
Member

pda commented Sep 2, 2022

WithSleepFunc is a great way to test retries without waiting for them, and allows assertions against the requested sleep time 👌🏼

However, threading this through from tests to the retryer can be tricky.
I'd love to be able to set it globally for the duration of a test like this:

func TestRetryingThings(t *testing.T) {
  oldSleepFunc := roko.GlobalSleepFunc
  roko.GlobalSleepFunc = func(t time.Duration) {}
  defer func() { roko.GlobalSleepFunc = nil }()

  result := doThingsThatIndirectlyRetry()
  // assert things
}

Or maybe it can be designed to be defer-friendly nested/reentrant-ish:

func TestRetryingThings(t *testing.T) {
  revert := roko.WithGlobalSleepFunc(func (t time.Duration) {})
  defer revert() // restore whatever the previous global sleep func was

  result := doThingsThatIndirectlyRetry()
  // assert things
}
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

No branches or pull requests

1 participant