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

A more generic version of eventually in asynctest #1044

Open
marcinczenko opened this issue Dec 16, 2024 · 0 comments
Open

A more generic version of eventually in asynctest #1044

marcinczenko opened this issue Dec 16, 2024 · 0 comments
Labels
Marketplace See https://miro.com/app/board/uXjVNZ03E-c=/ for details

Comments

@marcinczenko
Copy link
Contributor

marcinczenko commented Dec 16, 2024

In #922, a more generic version of eventually has been introduced to have a bit more control over awaiting for asynchronous tasks in tests.

The version was called eventuallyS where S stands for slow. It changes the timeout argument to use seconds instead of milliseconds (default 10), introduces step argument to adjust the waiting time between testing the condition (default 5, meaning 5s) and optional cancelExpression to interrupt the waiting loop and return false when cancelExpression evaluates to true:

template eventuallyS(expression: untyped, timeout=10, step = 5,
    cancelExpression: untyped = false): bool =
  bind Moment, now, seconds

  proc eventuallyS: Future[bool] {.async.} =
    let endTime = Moment.now() + timeout.seconds
    var secondsElapsed = 0
    while not expression:
      if endTime < Moment.now():
        return false
      if cancelExpression:
        return false
      await sleepAsync(step.seconds)
    return true

  await eventuallyS()

The intention is to use the above function to replace the original implementation.

If we stick to milliseconds as the unit and keep the default timeout value set to 5000 and step being 10 we would get fully backward-compatible version. We could also consider increasing the default step to something more appropriate for (slow) integration tests, e.g. 1000 (meaning we would have 1s between testing the condition).

Two functions need to be changed in the asynctest module:

  1. asynctest/private/chronos/eventually.nim
  2. asynctest/asynctest/private/asyncdispatch/eventually.nim

Estimated cost: 1 (ideal working day)

@marcinczenko marcinczenko added the Marketplace See https://miro.com/app/board/uXjVNZ03E-c=/ for details label Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Marketplace See https://miro.com/app/board/uXjVNZ03E-c=/ for details
Projects
None yet
Development

No branches or pull requests

1 participant