Skip to content

Fix Core Data model duplication test flakiness - #25932

Merged
crazytonyli merged 4 commits into
trunkfrom
bugfix/coredata-test-model-duplication
Aug 24, 2026
Merged

Fix Core Data model duplication test flakiness#25932
crazytonyli merged 4 commits into
trunkfrom
bugfix/coredata-test-model-duplication

Conversation

@crazytonyli

@crazytonyli crazytonyli commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

The migration tests load previous versions of Core Data models, which lead to the same Core Data type names (Blog, Post, etc) can be resolved is mapped to different in-memory representation.

This PR made two main changes:

  • Deleting CoreDataMigrationTests. The test code does not verify the real app code anymore.
  • Use neutralizingEntityClasses to avoid mapping multiple Core Data type names. This function is duplicated in different modules, because it's small and narrow enough that I feel it's an overkill to share it.

The unit tests share one process, and Core Data registers every loaded
model's entities in a process-global class-to-entity table. Loading a
historical schema version alongside the current one leaves two entity
descriptions claiming the same NSManagedObject subclass, so +[X entity]
can no longer resolve uniquely and crashes unrelated Core Data tests
depending on execution order.

Reset a loaded historical model's entity classes to the generic
NSManagedObject so it never claims the concrete subclasses. The migration
tests only reach their data by entity name and key-value coding, so they
keep working.
Move currentObjectModel to a public extension so callers (including tests)
can reuse the single process-wide model instance instead of loading a second
copy of the same schema, which would register duplicate entity-to-class
mappings.
createContext defaulted to a freshly merged copy of the current model,
adding a second registration of the current schema's NSManagedObject
subclasses to Core Data's process-global class-to-entity table and making
unrelated tests crash depending on execution order.

Use the shared ContextManager.currentObjectModel instead, and neutralize the
entity classes of the older model versions the tests load on purpose so they
no longer register duplicate concrete subclasses either.
It only covered migrations between the legacy model versions 103 and 104.
@crazytonyli
crazytonyli marked this pull request as ready for review August 24, 2026 03:48
@crazytonyli
crazytonyli requested a review from jkmassel August 24, 2026 03:48
@crazytonyli crazytonyli added the Core Data Issues related to Core Data label Aug 24, 2026
@crazytonyli crazytonyli added this to the 27.3 milestone Aug 24, 2026
@crazytonyli
crazytonyli enabled auto-merge August 24, 2026 03:48
@wpmobilebot

Copy link
Copy Markdown
Contributor
App Icon📲 You can test the changes from this Pull Request in WordPress by scanning the QR code below to install the corresponding build.
App NameWordPress
ConfigurationRelease-Alpha
Build Number33901
VersionPR #25932
Bundle IDorg.wordpress.alpha
Commit62a74d9
Installation URL5mlg079n75o9g
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@wpmobilebot

Copy link
Copy Markdown
Contributor
App Icon📲 You can test the changes from this Pull Request in Jetpack by scanning the QR code below to install the corresponding build.
App NameJetpack
ConfigurationRelease-Alpha
Build Number33901
VersionPR #25932
Bundle IDcom.jetpack.alpha
Commit62a74d9
Installation URL0ucugq3njukvg
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@crazytonyli
crazytonyli added this pull request to the merge queue Aug 24, 2026
Merged via the queue into trunk with commit f4fd9cb Aug 24, 2026
30 checks passed
@crazytonyli
crazytonyli deleted the bugfix/coredata-test-model-duplication branch August 24, 2026 21:02
pull Bot pushed a commit to kliu/WordPress-iOS that referenced this pull request Aug 26, 2026
…mobile#25941)

The test built its context from NSManagedObjectModel.mergedModel(from:
[Bundle.main]), loading a second copy of the current schema. Every loaded
model registers its entities in Core Data's process-global class-to-entity
table, so this left two entity descriptions claiming ReaderPost, Blog, and
the other subclasses. When a later test in the same process saved, Core
Data's change processing hit the ambiguity and crashed with
"-[__NSCFSet addObject:]: attempt to insert nil", failing unrelated tests
depending on execution order.

Use ContextManager.forTesting(), which reuses the single process-wide
current model, and pass that stack straight to SharedDataIssueSolver. This
completes the deduplication started in wordpress-mobile#25932, which covered DataMigratorTests
but not this call site.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Core Data Issues related to Core Data

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants