Skip to content
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

Merged
merged 2 commits into from
May 3, 2022

Conversation

crazytonyli
Copy link
Contributor

This PR is part of paaHJt-3ky-p2.

Changes

Remove testExpectation from TestContextManager and ContextManagerMock. This expectation is used to assert ContextManager.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 simplifies CoreDataStack 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:

  • I have completed the Regression Notes.
  • I have considered adding unit tests for my changes.
  • I have considered adding accessibility improvements for my changes.
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

// FIXME: Remove this method to use superclass one instead
// This log magically resolves a deadlock in
// `ZDashboardCardTests.testShouldNotShowQuickStartIfDefaultSectionIsSiteMenu`
NSLog(@"Context save completed");
Copy link
Contributor Author

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 👀

Copy link
Contributor

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.

Copy link
Contributor

@mokagio mokagio left a 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:

Also, BlogDashboardServiceTests explicitly mentions CoreData as a possible cause of issues:

/// 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];
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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. 👍

Comment on lines 78 to 80
XCTestExpectation *saveExpectation = [self expectationForNotification:NSManagedObjectContextDidSaveNotification object:self.testContextManager.mainContext handler:nil];

AccountService *accountService = [[AccountService alloc] initWithManagedObjectContext:[ContextManager sharedInstance].mainContext];
Copy link
Contributor

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:

- (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:

- (NSManagedObjectContext *)mainContext
{
if (_mainContext) {
return _mainContext;
}
_mainContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
_mainContext.persistentStoreCoordinator = self.persistentStoreCoordinator;
return _mainContext;
}

Copy link
Contributor Author

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.

Copy link
Contributor

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");
Copy link
Contributor

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.

@crazytonyli crazytonyli marked this pull request as ready for review May 2, 2022 08:39
They are the same object in runtime, since `TestContextManager`
overrides `ContextManager.sharedInstance` with a mock instance.
@crazytonyli crazytonyli requested a review from mokagio May 2, 2022 22:52
Copy link
Contributor

@mokagio mokagio left a 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 🎉

@mokagio mokagio added Testing Unit and UI Tests and Tooling Core Data Issues related to Core Data labels May 3, 2022
@mokagio mokagio added this to the 19.9 milestone May 3, 2022
@mokagio
Copy link
Contributor

mokagio commented May 3, 2022

@crazytonyli

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 trunk.

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 x.9 we roll to (x+1).0

@mokagio mokagio enabled auto-merge May 3, 2022 04:36
@mokagio mokagio merged commit 90cc17e into wordpress-mobile:trunk May 3, 2022
@crazytonyli
Copy link
Contributor Author

🥳 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.

@mokagio
Copy link
Contributor

mokagio commented May 3, 2022

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

@crazytonyli crazytonyli deleted the test-expectation branch May 4, 2022 21:03
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 Testing Unit and UI Tests and Tooling
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants