Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[local_auth] Fix callback thread handling #3778

Merged

Conversation

stuartmorgan-g
Copy link
Contributor

Ensure that all auth replies, which are sent on an internal framework queue per documentation, are dispatched back to the main thread for handling, as all resulting operations (method channel callbacks, display of UI) are things that must be done on the main thread

In order to test this, sets up local_auth with XCTest-based tests, and adds the ability to inject a mock LAContext. (This does not do full unit test backfill, to limit the scope of the PR.)

Fixes flutter/flutter#47465

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide and the C++, Objective-C, Java style guides.
  • I signed the CLA.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
  • I updated CHANGELOG.md to add a description of the change.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test exempt.
  • All existing and new tests are passing.

@stuartmorgan-g
Copy link
Contributor Author

stuartmorgan-g commented Apr 1, 2021

Before landing this I need to make sure CI is actually running the new unit tests, since I'm not sure if there were extra steps to wire that up that I missed, or if it's automatic.

Copy link
Contributor

@cyanglaz cyanglaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems there are some issues with the test code. I might be missing something tho.

if (_authContextOverride != nil) {
return _authContextOverride;
}
return [[LAContext alloc] init];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each self.authContext is going to call the constructor (when no _authContextOverride is provided)

If we always wants a new instance (which i think that's the case), we should rename it do something doesn't sound too much like a getter. Maybe: - (LAContext *)getNewAuthContext`?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to createAuthContext since create is a standard idiom. Also future-proofed the override by making it better fit the fact that this is really, as you point out, a factory rather than a getter; it now uses an array of overrides, so a test could run a flow with multiple API calls.

[plugin handleMethodCall:call
result:^(id _Nullable result) {
XCTAssertTrue([NSThread isMainThread]);
[expectation fulfill];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this fulfill be the last call of the block? After XCTAssertTrue([result boolValue]);? Unless I'm missing something? Ditto below if make sense.

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 call; done. It's been a long time since I've done async XCTests!

XCTAssertTrue([result isKindOfClass:[NSNumber class]]);
XCTAssertTrue([result boolValue]);
}];
[self waitForExpectationsWithTimeout:1.0 handler:nil];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should increase the timeout as running tests on CI might be slow and we don't want to many false negatives (flake) because of the environment is slow. How about 30s? and set continueAfterFailure to NO in setup

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

@cyanglaz cyanglaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@stuartmorgan-g stuartmorgan-g merged commit 08f8f21 into flutter:master Apr 5, 2021
@stuartmorgan-g stuartmorgan-g deleted the local-auth-ios-thread-fix branch April 5, 2021 23:03
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 6, 2021
fotiDim pushed a commit to fotiDim/plugins that referenced this pull request Sep 13, 2021
Ensure that all auth replies, which are sent on an internal framework queue per documentation, are dispatched back to the main thread for handling, as all resulting operations (method channel callbacks, display of UI) are things that must be done on the main thread

In order to test this, sets up local_auth with XCTest-based tests, and adds the ability to inject a mock LAContext. (This does not do full unit test backfill, to limit the scope of the PR.)

Fixes flutter/flutter#47465
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[local_auth] iOS App crashes after 5 (3+2) invalid input attempts
2 participants