-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Remove "context saved" expectation from CoreDataStack mocks #18485
Conversation
They are moved to test methods.
// FIXME: Remove this method to use superclass one instead | ||
// This log magically resolves a deadlock in | ||
// `ZDashboardCardTests.testShouldNotShowQuickStartIfDefaultSectionIsSiteMenu` | ||
NSLog(@"Context save completed"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still trying to figure out cause of this deadlock 👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For what is worth, I can see the deadlock without this line on my end, too. The test you mention just sits there with a spinner next to it in the test navigator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is shaping up well, thank you @crazytonili!
The deadlock behavior, and it's resolution with a simple log, is quite baffling... 😳
I'd say there's something deeper down in the CoreData stack and/or how the Dashboard part of the app uses it. I'm saying this because the tests were explicitly prefixed with Z
to let them run at the end.
See:
- 3a3e1ae
- https://github.com/wordpress-mobile/WordPress-iOS/blob/112b352938ecbea191addd94be7f61d8cd4d5bf4/WordPress/WordPressTest/Dashboard/BlogDashboardServiceTests.swift
Also, BlogDashboardServiceTests
explicitly mentions CoreData as a possible cause of issues:
WordPress-iOS/WordPress/WordPressTest/Dashboard/BlogDashboardServiceTests.swift
Lines 6 to 13 in 112b352
/// This test stuite is clashing with other tests | |
/// Specifically: | |
/// - [BlogJetpackTest testJetpackSetupDoesntReplaceDotcomAccount] | |
/// - CommentServiceTests.testFailingFetchCommentLikesShouldCallFailureBlock() | |
/// | |
/// We weren't able to figure out why but it seems a race condition + Core data | |
/// For now, renaming the suite to change the execution order solves the issue. | |
class ZBlogDashboardServiceTests: XCTestCase { |
You shared a possible fix and I love your initiative. But, I'd say let's focus on the test suite for the context of this trial because we want to keep it time constrained.
IMHO, having a FIXME
comment plus NSLog
is no worse than renaming tests to start with Z
so they run last as a workaround.
|
||
AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:[ContextManager sharedInstance].mainContext]; | ||
WPAccount *wpComAccount = [accountService createOrUpdateAccountWithUsername:@"user" authToken:@"token"]; | ||
[self waitForExpectationsWithTimeout:2.0 handler:nil]; | ||
[self waitForExpectations:@[saveExpectation] timeout:2.0]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed this PR replaces all the waitForExpectations(timeout:, handler:)
with wait(for:, timeout:)
.
What's your rationale for this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this expectation was the only expectation created within this test method. This change is just making that explicit - test method is waiting for main context to be saved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. One could argue that using waitForExpectations(timeout:, handler:)
would make for smaller diffs, because if we add new expectations that method will automatically wait for them.
But, I'm with you on the value of explicitly stating which expectation we're waiting for. 👍
XCTestExpectation *saveExpectation = [self expectationForNotification:NSManagedObjectContextDidSaveNotification object:self.testContextManager.mainContext handler:nil]; | ||
|
||
AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:[ContextManager sharedInstance].mainContext]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 it's surprising to see the expectation using self.textContextManager.mainContext
and this using [ContextManager sharedInstance].mainContext
.
They look like different contexts. Or am I missing something?
For reference, here's where each is instantiated (I think)
For ContextManager
:
WordPress-iOS/WordPress/Classes/Utility/ContextManager.m
Lines 72 to 83 in 112b352
- (NSManagedObjectContext *const)mainContext | |
{ | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; | |
context.parentContext = self.writerContext; | |
self.mainContext = context; | |
[[[NullBlogPropertySanitizer alloc] initWithContext:context] sanitize]; | |
}); | |
return _mainContext; | |
} |
For TestContextManager
, which internally uses ContextManager
:
WordPress-iOS/WordPress/WordPressTest/ContextManagerMock.m
Lines 70 to 80 in 112b352
- (NSManagedObjectContext *)mainContext | |
{ | |
if (_mainContext) { | |
return _mainContext; | |
} | |
_mainContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; | |
_mainContext.persistentStoreCoordinator = self.persistentStoreCoordinator; | |
return _mainContext; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! The code does look like they are referencing different context objects. But at runtime, they are actually the same one, the one in ContextManagerMock
, since TestContextManager overrides the shared instance(the overriding actually happens within ContextManagerMock
). Which is why self.testContextManager.mainContext
is same as [[ContextManager sharedInstance] mainContext]
.
I will update the code to use the same reference for clarity.
BTW, the above overriding behaviour will be updated in my establishing usage pattern PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha! I knew I must have been missing something, else I would have expected the tests to fail 😄
Thank you for making it explicit in c6873f2. 👍
// FIXME: Remove this method to use superclass one instead | ||
// This log magically resolves a deadlock in | ||
// `ZDashboardCardTests.testShouldNotShowQuickStartIfDefaultSectionIsSiteMenu` | ||
NSLog(@"Context save completed"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For what is worth, I can see the deadlock without this line on my end, too. The test you mention just sits there with a spinner next to it in the test navigator.
They are the same object in runtime, since `TestContextManager` overrides `ContextManager.sharedInstance` with a mock instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is now good to merge. Thank you @crazytonyli, and congrats on your first WordPress iOS contribution 😄
I updated #18488 so it will run the tests in CI for your latest changes. If that's green, we should be good to merge the PR 🎉
Actually, as an external contributor, you wouldn't be able to merge the PR. I will enable auto-merge, so if/when the tests pass, this will land on Also notice I added labels and milestones. That's a bit of our process that's not required during the trial but would still be appropriate. Rule of thumb to choose the correct milestone value: if at the start of a sprint, use the current milestone (i.e. the one that will close at the start of the next sprint); if towards the end of a sprint (say only 2-3 days left) use the next milestone. In our case, today is Tuesday, and it's the first week of the sprint. 19.8 is the milestone in code freeze (see the ❄️ in the name), 19.9 is the current milestone (the one that we'll close at the start of next sprint), and 20.0 is the next milestone (the WordPress apps adhere to the WordPress versioning which is not strictly SemVer: once we reach |
🥳 Thanks for merging this PR! I did notice the labels and milestone in other PRs. But I don't think I have the permission to add labels nor milestone. |
Oh, I wasn't expecting that... No worries, then. Someone will add them |
This PR is part of paaHJt-3ky-p2.
Changes
Remove
testExpectation
fromTestContextManager
andContextManagerMock
. This expectation is used to assertContextManager.saveContext
method are called. This logic is split into two areas: created in unit test method, but fulfilled outside of test method. This PR moves all this logic into unit test method, for better readability, it also simplifiesCoreDataStack
mock implementations.Test Instructions
Make sure all unit tests pass.
Regression Notes
N/A. All changes in this PR are in unit test, they don't have any impact on the app.
PR submission checklist:
RELEASE-NOTES.txt
if necessary.