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

Fix assertions mode when using coroutines in another thread #3604

Closed
wants to merge 3 commits into from

Conversation

sksamuel
Copy link
Member

Bit of coroutine trickery here @charleskorn
It swaps in the thread locals used by the assertion counters as coroutines are launched, but with test name affinity.

Fixes #3022

@sksamuel sksamuel requested a review from Kantis July 30, 2023 20:32
@sksamuel sksamuel requested a review from LeoColman July 30, 2023 20:33
@sksamuel sksamuel enabled auto-merge (squash) July 30, 2023 20:33
@sksamuel sksamuel disabled auto-merge July 30, 2023 20:33
override fun inc() = context.set(context.get() + 1)
override fun get(): Int = values.get()
override fun reset() = values.set(0)
override fun inc() = values.set(values.get() + 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this safe if multiple coroutines are running for the same test simultaneously?

If all we need to track is "an assertion did / did not occur", perhaps an atomic boolean would be easier to reason about?

// this is invoked after coroutine has suspended on current thread
override fun restoreThreadContext(context: CoroutineContext, oldState: Int) {
// need to put the current thread-local value into our backing map before the coroutine is switched out
testNameCounters[testName] = ThreadLocalAssertionCounter.values.get()
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if a test launches multiple coroutines and makes assertions in both?

What happens if multiple coroutines launch at the same time but the last one to finish doesn't make any assertions? Won't we end up with a count of 0 assertions for the test?

@OliverO2 OliverO2 mentioned this pull request Aug 17, 2023
@sksamuel
Copy link
Member Author

Closing in favour of #3623

@sksamuel sksamuel closed this Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants