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

Fixes race in testsales #995

Merged
merged 5 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tests/codex/helpers/mockclock.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ method waitUntil*(clock: MockClock, time: SecondsSince1970) {.async.} =
let future = newFuture[void]()
clock.waiting.add(Waiting(until: time, future: future))
await future

proc isWaiting*(clock: MockClock): bool =
clock.waiting.len > 0
2 changes: 1 addition & 1 deletion tests/codex/sales/testsales.nim
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ asyncchecksuite "Sales":

proc allowRequestToStart {.async.} =
# wait until we're in initialproving state
await sleepAsync(10.millis)
check eventually clock.isWaiting
Copy link
Contributor

Choose a reason for hiding this comment

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

If the underlying function changes (adding a waitUntil in a certain sales state), then the test will fail and it may be a bit difficult to test. Instead, could we perhaps check the state? I think this should do it:

proc isInState(idx: int, state: string): bool =
  proc description(state: State): string =
    $state
  sales.agents[idx].query(description) == state.some

check eventually isInState(0, "SaleInitialProving")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like this, it seems a lot more reusable. I think isWaiting for a mockClock makes perfect sense. I'm giving this a try.

Copy link
Contributor

Choose a reason for hiding this comment

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

Be aware though that allowRequestToStart may not actually have been checking that the state was in the initial proving state, so if you update it, some tests may break

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have applied your suggestion and I like it. The tests are all happy.
I know every keystroke I make reduces the likelihood that I can get this merged, BUT I did replace two more sleeps with check-eventually. Plz check if you agree with them.

# it won't start proving until the next period
await clock.advanceToNextPeriod(market)

Expand Down