vvkit-swift-swift-testing-async
Use when testing Swift async/await code — a task group, async let, a timeout race, a retry, or cancellation — or when a Swift test waits with Task.sleep, polling, or Task.yield for work in flight.
- License 许可
- MIT
- 最近更新
- 2026-11-13
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- vvkit-swift-swift-testing-async
- description
- Use when testing Swift async/await code — a task group, async let, a timeout race, a retry, or cancellation — or when a Swift test waits with Task.sleep, polling, or Task.yield for work in flight.
02 资源文件 1
- 参考文档references/spies.md4.5 KB
03 SKILL.md 正文
Testing async/await code
REQUIRED BACKGROUND: $vvkit-swift-swift-testing for the framework, $vvkit-writing-tests for
whether a test is owed. State shared across locks, queues or callbacks rather than await is
swift-testing's references/concurrency.md.
Codex runtime
Load referenced skills by reading their SKILL.md. Surveyor, reviewer and runner
skills are role instructions, not registered agent types or enforced tool allowlists.
Use a generic subagent only when delegation is available and permitted; otherwise
perform the role inline. Follow the active session's model, context and permission
rules. User authorization takes precedence over imported workflow approval gates.
Claude hooks and plugin commands are not active in this package. Existing
.claude/context/ paths refer to project documents, not Codex configuration.
Never synchronise on a guessed interval.
Task.sleep, a polling loop or a run ofTask.yield()"to let the other task get there" guesses the scheduler. It flakes under parallel runs, cannot choose which racing operation wins, and is slow — 100 ms across a thousand tests is 100 s per run.- Models reach for these by default. Code under test that calls
Task.sleepor reads a clock is the missing seam: inject it —$vvkit-injecting-dependencies.
The technique: spies that report and wait
Every dependency whose timing decides the outcome — value source, sleep, clock — becomes a protocol with a test spy that:
- Reports each call on a stream the test reads with
nextCall(), with the arguments. - Suspends until released —
respond(with: Result)for a value source,resume()for a sleep. - Exits on cancellation. One that ignores it hangs the run.
A received call proves the operation got there; the test then picks which dependency proceeds, so each
ordering is its own deterministic test with no real time elapsing. A spy asserts the requested
duration (sleepCall == .seconds(3)) without waiting.
Reference spies on AsyncStream: references/spies.md. Copy the shape, adapt the signature.
Shape of a test
One ordering per test:
@Suite(.timeLimit(.minutes(1)))
struct TimedLoaderTests {
@Test("a value returned before the deadline wins")
func valueBeforeTimeout() async throws {
let provider = ValueProviderSpy()
let sleep = SleepSpy()
let sut = TimedLoader(provider: provider, sleep: sleep)
async let loaded = sut.load(within: .seconds(3))
// Both child tasks are running once both events arrive.
let valueCall = try #require(await provider.nextCall())
let sleepCall = try #require(await sleep.nextCall())
#expect(valueCall == .value)
#expect(sleepCall == .seconds(3))
provider.respond(with: .success("loaded"))
let value = try await loaded
#expect(value == "loaded")
}
}
| Scenario | After both call events arrive |
|---|---|
| Value wins | provider.respond(with: .success(…)) |
| Timeout wins | sleep.resume(), then expect the timeout error |
| Dependency fails | provider.respond(with: .failure(…)), expect that exact error back |
| Caller cancels | cancel the task — see below |
| Only the result matters | respond before starting, skip nextCall(), plain await |
- Assert an
async leterror indo/catch;#expect(throws:) { try await loaded }does not compile — a closure cannot capture anasync let. - The loser's cancellation needs no assertion: a task group awaits every child, so a loader that forgot to cancel leaves it on its spy, and the time limit fails the test.
Caller cancellation
async let cancels and awaits on scope exit. Task { } does neither: it does not inherit the test's
cancellation, and a #require throwing before cancel() leaves it waiting forever. Close both:
let loading = Task { try await sut.load(within: .seconds(3)) }
do {
try await withTaskCancellationHandler {
_ = try #require(await provider.nextCall())
_ = try #require(await sleep.nextCall())
loading.cancel()
await #expect(throws: CancellationError.self) {
try await loading.value
}
} onCancel: {
loading.cancel() // the time limit cancelled the test
}
} catch {
loading.cancel() // a #require threw before cancel()
_ = await loading.result
throw error
}
Time limits
.timeLimit(.minutes(1))on the suite, so a call that never arrives fails instead of wedging the bundle. Minutes-only.- A time limit records a failure and requests cancellation; it stops nothing. Swift Testing still
waits for the function to return — hence spies that end on cancellation and the
onCancelforwarding. A cooperative-pool deadlock defeats even this — Watchdogs in$vvkit-swift-swift-testing.
Spy rules that bite
- Fresh spies per operation. Cancellation ends the
AsyncStreamiteration; a reused sleep spy can then return withoutresume(), passing an ordering never driven. A retry after cancellation needs a spy that opens a new stream per call. - One waiter per stream.
AsyncStreamtraps when a second task awaitsnext(). The reference spies turn that into a namedpreconditionFailure; give each concurrent sleep its own spy. - A
.failureresponse is consumed by one request. The stream stays open, so the next call can succeed — what a retry test wants.
Where this fits the ladder
A spy is the virtual-time rung of swift-testing's ladder for async/await. confirmation is for how
many times, not in what order. Polling stays last, for genuinely wall-clock work.
Technique source: raska.io, "Stop Sleeping: Deterministic Tests for Concurrent Swift Code" (2026).
04 同仓库更多 Skills
no-heading
Use when proving that the validator rejects a body with no H1 and a force-loading @-link.
wrong-name-does-not-match-dir
Dispatches a subagent per task and reviews between each one.
writing-tests
Use when adding or changing behavior, fixing a bug, or judging whether an existing or generated test earns its place.
writing-skills
Use when authoring or editing a skill, agent, command or hook for this kit, or when deciding whether a piece of guidance belongs in a skill at all.
writing-project-instructions
Use when creating or editing a CLAUDE.md or AGENTS.md, when it has grown long, or when a rule in it also exists in an installed skill.
writing-logs
Use when adding or reviewing log statements, or when deciding what level a message belongs at.