Repository navigation
Harden the Foundation Models test fixtures - #599
Merged
thechriswebb merged 1 commit intoSep 1, 2026
Merged
Conversation
- In `GenerationEventObserverTests`, two tasks drained the generation channel with an empty `catch {}` block. A read failure there would pass the test silently. Extracts one drain helper. Each test now awaits its result and accepts only a cancellation error.
- Asserts the expected error from every load and warmup task instead of discarding it with `try?`.
- The downloader and tokenizer-loader test stubs were classes with no stored state, marked `@unchecked Sendable`. That annotation tells the compiler to trust the type is safe to share across threads, without the compiler checking it. Converts the stubs to structs, which are `Sendable` by construction, so the compiler proves the guarantee instead of the annotation asserting it.
- `EventBox` and `SeedRecorder` hold mutable state a closure captures across a task boundary, so a struct will not work for them. They used `NSLock` with the same `@unchecked Sendable` escape hatch as the stubs above. Replaces the lock with the standard library's `Mutex`, so these types are provably `Sendable` too, instead of asserting it.
- In `GenerationEventObserverTests`, `withValue` took two closures, one passed as an argument and one as a trailing closure. swift-format's `OnlyOneTrailingClosureArgument` rule flags that form because it is easy to misread which parameter each closure binds to. Spells out the second closure with its argument label instead.
- `EvictBiasStubTokenizerLoader.load()` returns a `CountingTokenizer`, which requires OS 27. The old code checked that at runtime with a `guard` and a `preconditionFailure` that could never actually fire. Marks the stub itself with the OS 27 requirement, so the compiler enforces it and the dead runtime check is gone.
An empty catch and a discarded task result both hid a real loader failure, so a broken load could not fail a test. The fixtures also claimed thread safety by annotation where value semantics or a mutex states it, and asserted an availability requirement at runtime that the type system can enforce at compile time.
davidkoski
approved these changes
Sep 1, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
Changes look good to me, thanks!
djandrewgibbons
pushed a commit
to djandrewgibbons/mlx-swift-lm
that referenced
this pull request
Sep 26, 2026
- In `GenerationEventObserverTests`, two tasks drained the generation channel with an empty `catch {}` block. A read failure there would pass the test silently. Extracts one drain helper. Each test now awaits its result and accepts only a cancellation error.
- Asserts the expected error from every load and warmup task instead of discarding it with `try?`.
- The downloader and tokenizer-loader test stubs were classes with no stored state, marked `@unchecked Sendable`. That annotation tells the compiler to trust the type is safe to share across threads, without the compiler checking it. Converts the stubs to structs, which are `Sendable` by construction, so the compiler proves the guarantee instead of the annotation asserting it.
- `EventBox` and `SeedRecorder` hold mutable state a closure captures across a task boundary, so a struct will not work for them. They used `NSLock` with the same `@unchecked Sendable` escape hatch as the stubs above. Replaces the lock with the standard library's `Mutex`, so these types are provably `Sendable` too, instead of asserting it.
- In `GenerationEventObserverTests`, `withValue` took two closures, one passed as an argument and one as a trailing closure. swift-format's `OnlyOneTrailingClosureArgument` rule flags that form because it is easy to misread which parameter each closure binds to. Spells out the second closure with its argument label instead.
- `EvictBiasStubTokenizerLoader.load()` returns a `CountingTokenizer`, which requires OS 27. The old code checked that at runtime with a `guard` and a `preconditionFailure` that could never actually fire. Marks the stub itself with the OS 27 requirement, so the compiler enforces it and the dead runtime check is gone.
An empty catch and a discarded task result both hid a real loader failure, so a broken load could not fail a test. The fixtures also claimed thread safety by annotation where value semantics or a mutex states it, and asserted an availability requirement at runtime that the type system can enforce at compile time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
An audit of
MLXFoundationModelsagainst Swift's published style guidance surfaced eight test fixture files that broke concrete rules, not just style preferences. Two channel drains swallowed every error in an emptycatchblock. Six load and warmup tasks discarded their result withtry?. Five stub types asserted thread safety with@unchecked Sendableinstead of the compiler proving it. One stub enforced an OS availability requirement with a runtime guard the compiler could enforce instead. Each of these could hide a real failure or a real race and let a broken fixture report a passing test.What this PR does
GenerationEventObserverTestsand asserts its result. A drain that fails with anything but cancellation now fails the test, instead of vanishing into an emptycatch {}.AvailabilityTestsandModelCacheEvictionTests, instead of discarding it withtry?.@unchecked Sendableto structs, so the compiler provesSendableinstead of the annotation asserting it.NSLockand@unchecked SendableonEventBoxandSeedRecorder, the two fixtures that hold mutable state across a task boundary, with the standard library'sMutex.GenerationEventObserverTeststhat swift-format'sOnlyOneTrailingClosureArgumentrule flagged, so it is unambiguous which parameter each closure binds to.EvictBiasStubTokenizerLoaderwith the OS 27 availability its returned tokenizer already requires, replacing a runtimeguardand apreconditionFailurethat could never fire.Tests
pre-commit run --all(swift-format): passes with zero files changed.xcodebuild build-for-testingformlx-swift-lm-Package: builds clean.xcrun xctest MLXFoundationModelsTests.xctest: 139 tests in 21 suites pass, run 20 times in a row with no flakes.xcrun xctest MLXLMTests.xctest: 722 tests in 55 suites pass. This is CI's only required suite, but it covers none of this PR's diff;MLXFoundationModelsTests.xctestis the suite that exercises it.#expectin each of the six affected test files, confirmed all six produce the expected failure, then reverted and confirmed the suite returns to 139 tests in 21 suites passing.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI usage
accurately describes the code changes.