Skip to content

Commit 1b9991e

Browse files
test: Remove state in MainThreadTestIntegration (#5687)
Remove the static global state in MainThreadTestIntegration. Also make installedInTheMainThread readonly for public access. Co-authored-by: Itay Brenner <itay.brenner@sentry.io>
1 parent 73c9712 commit 1b9991e

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

Tests/SentryTests/SentrySDKInternalTests.swift

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -506,16 +506,24 @@ class SentrySDKInternalTests: XCTestCase {
506506

507507
/// Ensure to start the UIDeviceWrapper before initializing the hub, so enrich scope sets the correct OS version.
508508
func testStartSDK_ScopeContextContainsOSVersion() throws {
509-
let expectation = expectation(description: "MainThreadTestIntegration install called")
510-
MainThreadTestIntegration.expectation = expectation
509+
let expectation = XCTestExpectation(description: "SentrySDK start called")
511510

512-
DispatchQueue.global(qos: .default).async {
511+
DispatchQueue.global().async {
513512
SentrySDK.start { options in
514513
options.integrations = [ NSStringFromClass(MainThreadTestIntegration.self) ]
515514
}
515+
516+
// Since the SDK uses the dispatchqueue on the main queue, wait until it clears to fulfill the expectation
517+
SentryDependencyContainer.sharedInstance().dispatchQueueWrapper.dispatchAsyncOnMainQueue {
518+
expectation.fulfill()
519+
}
516520
}
517521

518-
wait(for: [expectation], timeout: 1.0)
522+
wait(for: [expectation], timeout: 5.0)
523+
524+
let mainThreadIntegration = try XCTUnwrap(SentrySDKInternal.currentHub().installedIntegrations().first as? MainThreadTestIntegration)
525+
526+
wait(for: [mainThreadIntegration.expectation], timeout: 5.0)
519527

520528
let os = try XCTUnwrap (SentrySDKInternal.currentHub().scope.contextDictionary["os"] as? [String: Any])
521529
#if !targetEnvironment(macCatalyst)
@@ -688,8 +696,8 @@ class SentrySDKInternalTests: XCTestCase {
688696
}
689697

690698
func testStartOnTheMainThread() throws {
691-
let expectation = expectation(description: "MainThreadTestIntegration install called")
692-
MainThreadTestIntegration.expectation = expectation
699+
700+
let expectation = XCTestExpectation(description: "SentrySDK start called")
693701

694702
print("[Sentry] [TEST] [\(#file):\(#line) Dispatching to nonmain queue.")
695703
DispatchQueue.global(qos: .utility).async {
@@ -698,13 +706,18 @@ class SentrySDKInternalTests: XCTestCase {
698706
print("[Sentry] [TEST] [\(#file):\(#line) configuring options.")
699707
options.integrations = [ NSStringFromClass(MainThreadTestIntegration.self) ]
700708
}
709+
710+
// Since the SDK uses the dispatchqueue on the main queue, wait until it clears to fulfill the expectation
711+
SentryDependencyContainer.sharedInstance().dispatchQueueWrapper.dispatchAsyncOnMainQueue {
712+
expectation.fulfill()
713+
}
701714
}
702715

703716
wait(for: [expectation], timeout: 5.0)
704717

705718
let mainThreadIntegration = try XCTUnwrap(SentrySDKInternal.currentHub().installedIntegrations().first as? MainThreadTestIntegration)
706-
XCTAssert(mainThreadIntegration.installedInTheMainThread, "SDK is not being initialized in the main thread")
707719

720+
wait(for: [mainThreadIntegration.expectation], timeout: 5.0)
708721
}
709722

710723
#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
@@ -1042,19 +1055,19 @@ class SentrySDKWithSetupTests: XCTestCase {
10421055

10431056
public class MainThreadTestIntegration: NSObject, SentryIntegrationProtocol {
10441057

1045-
static var expectation: XCTestExpectation?
1046-
1047-
public var installedInTheMainThread = false
1058+
public let expectation = XCTestExpectation(description: "MainThreadTestIntegration installed")
10481059

10491060
public func install(with options: Options) -> Bool {
10501061
print("[Sentry] [TEST] [\(#file):\(#line) starting install.")
1051-
installedInTheMainThread = Thread.isMainThread
1052-
MainThreadTestIntegration.expectation?.fulfill()
1053-
MainThreadTestIntegration.expectation = nil
1062+
dispatchPrecondition(condition: .onQueue(.main))
1063+
1064+
expectation.fulfill()
1065+
10541066
return true
10551067
}
10561068

10571069
public func uninstall() {
1070+
10581071
}
10591072
}
10601073
// swiftlint:enable file_length

0 commit comments

Comments
 (0)