Skip to content

Add concurrency testing patterns to write-unit-tests skill#13300

Merged
cttsai-stripe merged 2 commits into
masterfrom
cttsai/update-write-unit-tests-skill
Jun 26, 2026
Merged

Add concurrency testing patterns to write-unit-tests skill#13300
cttsai-stripe merged 2 commits into
masterfrom
cttsai/update-write-unit-tests-skill

Conversation

@cttsai-stripe

Copy link
Copy Markdown
Contributor

Summary

  • Adds a "Concurrency Testing with Real I/O" section to the write-unit-tests skill
  • Documents the runTest + advanceUntilIdle() + async/await() pattern for testing coroutines that involve real network I/O (NetworkRule/OkHttp)
  • Includes two canonical patterns: mid-flight state assertion and mutation queuing assertion
  • Adds common mistakes: Thread.sleep, testing stdlib, vacuous assertions

Context

Learned from code review on #13296 where timing-dependent tests needed to be rewritten using deterministic coroutine test patterns.

Test plan

  • Skill is documentation only, no code changes to test

🤖 Generated with Claude Code

Covers runTest + advanceUntilIdle + async/await pattern for testing
coroutines with real I/O, mutex queuing assertions, and common mistakes.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Committed-By-Agent: claude
@cttsai-stripe cttsai-stripe requested review from a team as code owners June 25, 2026 20:28

// Coroutine is now suspended on the network call.
// Assert mid-flight state here.
assertThat(systemUnderTest.isLoading.value).isTrue()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

These should really use turbines, right?

fun `mutex is locked during mutation`() = runTest {
val holdResponse = CountDownLatch(1)
networkRule.checkoutUpdate { response ->
holdResponse.await()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We should really have timeouts for all these, even if something unreasonable like 10 seconds.


| Do | Don't |
|----|-------|
| `async { }` + `await()` | `launch { }` + `join()` |

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why not launch/join?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ahh, I see -- it's explained below. Makes sense to me.

Comment thread .claude/skills/write-unit-tests/SKILL.md Outdated
@cttsai-stripe cttsai-stripe marked this pull request as draft June 25, 2026 20:59
- Turbine .test { } for asserting StateFlow transitions (false -> true -> false)
- CountDownLatch for asserting request ordering (negative + positive)
- Remove .value recommendation; prefer Turbine for StateFlow assertions
- Add timeouts to latch awaits in mock handlers

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Committed-By-Agent: claude
@cttsai-stripe cttsai-stripe marked this pull request as ready for review June 26, 2026 16:58
@cttsai-stripe cttsai-stripe merged commit 7b2c68a into master Jun 26, 2026
53 checks passed
@cttsai-stripe cttsai-stripe deleted the cttsai/update-write-unit-tests-skill branch June 26, 2026 16:58
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.

2 participants