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

MainThreadMonitor: don't crash if there is no test in progress #2838

Merged
merged 1 commit into from
Jul 18, 2023

Conversation

NachoSoto
Copy link
Contributor

@NachoSoto NachoSoto commented Jul 18, 2023

I was testing this locally, and I noticed that sometimes this would trigger after a test had already finished:

Test Case '-[BackendIntegrationTests.StoreKit2IntegrationTests testCanPurchasePackage]' passed (16.712 seconds).
Test Suite 'StoreKit2IntegrationTests' passed at 2023-07-18 10:27:45.282.
	 Executed 1 test, with 0 failures (0 unexpected) in 16.712 (16.713) seconds
Test Suite 'BackendIntegrationTests.xctest' passed at 2023-07-18 10:27:45.282.
	 Executed 1 test, with 0 failures (0 unexpected) in 16.712 (16.713) seconds
Test Suite 'Selected tests' passed at 2023-07-18 10:27:45.282.
	 Executed 1 test, with 0 failures (0 unexpected) in 16.712 (16.714) seconds
BackendIntegrationTests/MainThreadMonitor.swift:47: Precondition failed: Main thread was blocked for more than 1.0 seconds
2023-07-18 10:27:46.301255-0700 BackendIntegrationTestsHostApp[28299:2377529] BackendIntegrationTests/MainThreadMonitor.swift:47: Precondition failed: Main thread was blocked for more than 1.0 seconds

Looking at what the main thread was doing, I noticed XCTest was blocked waiting for some internal signal, possibly at the end of a test suite.

image

We obviously don't want to consider that a deadlock, since it's not our fault, so I changed the implementation to ignore this if the test isn't in progress.

I'd bet this is the last time (🤞🏻 ) I have to change this. This follows up several PRs trying to avoid false positives in CI:

@NachoSoto NachoSoto added the test label Jul 18, 2023
@NachoSoto NachoSoto requested a review from a team July 18, 2023 17:36
@NachoSoto NachoSoto force-pushed the main-thread-monitor-during-tests branch from dfc1d64 to 1ec112b Compare July 18, 2023 17:37
I was testing this locally, and I noticed that sometimes this would trigger _after_ a test had already finished:
```
Test Case '-[BackendIntegrationTests.StoreKit2IntegrationTests testCanPurchasePackage]' passed (16.712 seconds).
Test Suite 'StoreKit2IntegrationTests' passed at 2023-07-18 10:27:45.282.
	 Executed 1 test, with 0 failures (0 unexpected) in 16.712 (16.713) seconds
Test Suite 'BackendIntegrationTests.xctest' passed at 2023-07-18 10:27:45.282.
	 Executed 1 test, with 0 failures (0 unexpected) in 16.712 (16.713) seconds
Test Suite 'Selected tests' passed at 2023-07-18 10:27:45.282.
	 Executed 1 test, with 0 failures (0 unexpected) in 16.712 (16.714) seconds
BackendIntegrationTests/MainThreadMonitor.swift:47: Precondition failed: Main thread was blocked for more than 1.0 seconds
2023-07-18 10:27:46.301255-0700 BackendIntegrationTestsHostApp[28299:2377529] BackendIntegrationTests/MainThreadMonitor.swift:47: Precondition failed: Main thread was blocked for more than 1.0 seconds
```

Looking at what the main thread was doing, I noticed `XCTest` was blocked waiting for some internal signal, possibly at the end of a test suite.
We obviously don't want to consider that a deadlock, since it's not our fault, so I changed the implementation to ignore this if the test isn't in progress.
@NachoSoto NachoSoto force-pushed the main-thread-monitor-during-tests branch from 1ec112b to 1430994 Compare July 18, 2023 17:37
@codecov
Copy link

codecov bot commented Jul 18, 2023

Codecov Report

Merging #2838 (1430994) into main (598a76f) will increase coverage by 0.01%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##             main    #2838      +/-   ##
==========================================
+ Coverage   86.57%   86.58%   +0.01%     
==========================================
  Files         217      217              
  Lines       15513    15513              
==========================================
+ Hits        13430    13432       +2     
+ Misses       2083     2081       -2     

see 2 files with indirect coverage changes

@NachoSoto NachoSoto merged commit e65912c into main Jul 18, 2023
2 checks passed
@NachoSoto NachoSoto deleted the main-thread-monitor-during-tests branch July 18, 2023 20:57
NachoSoto added a commit that referenced this pull request Jul 19, 2023
**This is an automatic release.**

### Dependency Updates
* Bump fastlane from 2.213.0 to 2.214.0 (#2824) via dependabot[bot]
(@dependabot[bot])
### Other Changes
* `MainThreadMonitor`: don't crash if there is no test in progress
(#2838) via NachoSoto (@NachoSoto)
* `CI`: fixed Fastlane APITester lanes (#2836) via NachoSoto
(@NachoSoto)
* `Integration Tests`: workaround Swift runtime crash (#2826) via
NachoSoto (@NachoSoto)
* `@EnsureNonEmptyArrayDecodable` (#2831) via NachoSoto (@NachoSoto)
* `iOS 17`: added tests for simulating cancellations (#2597) via
NachoSoto (@NachoSoto)
* `CI`: make all `Codecov` jobs `informational` (#2828) via NachoSoto
(@NachoSoto)
* `MainThreadMonitor`: check deadlocks only ever N seconds (#2820) via
NachoSoto (@NachoSoto)
* New `@NonEmptyStringDecodable` (#2819) via NachoSoto (@NachoSoto)
* `MockDeviceCache`: avoid using real `UserDefaults` (#2814) via
NachoSoto (@NachoSoto)
* `throwAssertion`: fixed Xcode 15 compilation (#2813) via NachoSoto
(@NachoSoto)
* `CustomEntitlementsComputation`: fixed API testers (#2815) via
NachoSoto (@NachoSoto)
* `PackageTypeTests`: fixed iOS 12 (#2807) via NachoSoto (@NachoSoto)
* `Tests`: avoid race-condition in leak detection (#2806) via NachoSoto
(@NachoSoto)
* Revert "`Unit Tests`: removed leak detection" (#2805) via NachoSoto
(@NachoSoto)
* `PackageType: Codable` implementation (#2797) via NachoSoto
(@NachoSoto)
* `SystemInfo.init` no longer `throws` (#2803) via NachoSoto
(@NachoSoto)
* `Trusted Entitlements`: add support for signing `POST` body (#2753)
via NachoSoto (@NachoSoto)
* `Tests`: unified default timeouts (#2801) via NachoSoto (@NachoSoto)
* `Tests`: removed forced-unwrap (#2799) via NachoSoto (@NachoSoto)
* `Tests`: added missing `super.setUp()` (#2804) via NachoSoto
(@NachoSoto)
* Replaced `FatalErrorUtil` with `Nimble` (#2802) via NachoSoto
(@NachoSoto)
* `Tests`: fixed another flaky test (#2795) via NachoSoto (@NachoSoto)
* `TimingUtil`: improved tests by using `Clock` (#2794) via NachoSoto
(@NachoSoto)
* `IgnoreDecodeErrors`: log decoding error (#2778) via NachoSoto
(@NachoSoto)
* `TestLogHandler`: changed all tests to explicitly deinitialize it
(#2784) via NachoSoto (@NachoSoto)
* `LocalReceiptParserStoreKitTests`: fixed flaky test failure (#2785)
via NachoSoto (@NachoSoto)
* `Unit Tests`: removed leak detection (#2792) via NachoSoto
(@NachoSoto)
* `Tests`: fixed another flaky failure with asynchronous check (#2786)
via NachoSoto (@NachoSoto)

---------

Co-authored-by: NachoSoto <ignaciosoto90@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants