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

Integration Tests: add MainThreadMonitor to ensure main thread is not blocked #2463

Merged
merged 1 commit into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions RevenueCat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
37E3578711F5FDD5DC6458A8 /* AttributionFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37E3521731D8DC16873F55F3 /* AttributionFetcher.swift */; };
37E35C8515C5E2D01B0AF5C1 /* Strings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37E3507939634ED5A9280544 /* Strings.swift */; };
42F1DF385E3C1F9903A07FBF /* ProductsFetcherSK1.swift in Sources */ = {isa = PBXBuildFile; fileRef = EFB3CBAA73855779FE828CE2 /* ProductsFetcherSK1.swift */; };
4FA696BD2A0020A000D228B1 /* MainThreadMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4FA696BC2A0020A000D228B1 /* MainThreadMonitor.swift */; };
57032ABF28C13CE4004FF47A /* StoreKit2SettingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57032ABE28C13CE4004FF47A /* StoreKit2SettingTests.swift */; };
57045B3829C514A8001A5417 /* ProductEntitlementMappingDecodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57045B3729C514A8001A5417 /* ProductEntitlementMappingDecodingTests.swift */; };
57045B3A29C51751001A5417 /* GetProductEntitlementMappingOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57045B3929C51751001A5417 /* GetProductEntitlementMappingOperation.swift */; };
Expand Down Expand Up @@ -857,6 +858,7 @@
37E35F783903362B65FB7AF3 /* MockProductsRequestFactory.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MockProductsRequestFactory.swift; sourceTree = "<group>"; };
37E35FDA0A44EA03EA12DAA2 /* DateFormatter+ExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "DateFormatter+ExtensionsTests.swift"; sourceTree = "<group>"; };
4FA696A329FC43C600D228B1 /* ReceiptParserTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "ReceiptParserTests-Info.plist"; sourceTree = "<group>"; };
4FA696BC2A0020A000D228B1 /* MainThreadMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainThreadMonitor.swift; sourceTree = "<group>"; };
57032ABE28C13CE4004FF47A /* StoreKit2SettingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoreKit2SettingTests.swift; sourceTree = "<group>"; };
57045B3729C514A8001A5417 /* ProductEntitlementMappingDecodingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductEntitlementMappingDecodingTests.swift; sourceTree = "<group>"; };
57045B3929C51751001A5417 /* GetProductEntitlementMappingOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetProductEntitlementMappingOperation.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1680,6 +1682,7 @@
579189FC28F4966500BF4963 /* OtherIntegrationTests.swift */,
57488AFE29CA58050000EE7E /* LoadShedderIntegrationTests.swift */,
579234E427F779FE00B39C68 /* SubscriberAttributesManagerIntegrationTests.swift */,
4FA696BC2A0020A000D228B1 /* MainThreadMonitor.swift */,
2CD2C541278CE0E0005D1CC2 /* RevenueCat_IntegrationPurchaseTesterConfiguration.storekit */,
2DE61A83264190830021CEA0 /* Constants.swift */,
2DE20B70264087FB004C597D /* Info.plist */,
Expand Down Expand Up @@ -3335,6 +3338,7 @@
2D3BFAD426DEA49200370B11 /* SKProductSubscriptionDurationExtensions.swift in Sources */,
579234E327F7788900B39C68 /* BaseBackendIntegrationTests.swift in Sources */,
2DE20B6F264087FB004C597D /* StoreKitIntegrationTests.swift in Sources */,
4FA696BD2A0020A000D228B1 /* MainThreadMonitor.swift in Sources */,
2D3BFAD126DEA45C00370B11 /* MockSK1Product.swift in Sources */,
57DD426E2926B9A50026DF09 /* StoreKitTestHelpers.swift in Sources */,
57C2932A28BFF89D0054EDFC /* ErrorMatcher.swift in Sources */,
Expand Down
11 changes: 11 additions & 0 deletions Tests/BackendIntegrationTests/BaseBackendIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class BaseBackendIntegrationTests: XCTestCase {
// swiftlint:disable:next weak_delegate
private(set) var purchasesDelegate: TestPurchaseDelegate!

private var mainThreadMonitor: MainThreadMonitor!
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a cool idea. Any thoughts on using this in other integration tests (not only backend integration tests)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the base class for all integration tests.


class var storeKit2Setting: StoreKit2Setting { return .default }
class var observerMode: Bool { return false }
class var responseVerificationMode: Signing.ResponseVerificationMode {
Expand All @@ -56,6 +58,9 @@ class BaseBackendIntegrationTests: XCTestCase {
throw ErrorUtils.configurationError(message: "Must set configuration in `Constants.swift`")
}

self.mainThreadMonitor = .init()
self.mainThreadMonitor.run()

if #available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) {
// Despite calling `SKTestSession.clearTransactions` tests sometimes
// begin with leftover transactions. This ensures that we remove them
Expand All @@ -75,6 +80,12 @@ class BaseBackendIntegrationTests: XCTestCase {
await self.waitForAnonymousUser()
}

override func tearDown() {
super.tearDown()

self.mainThreadMonitor = nil
}

// MARK: - Configuration

var apiKey: String { return Constants.apiKey }
Expand Down
52 changes: 52 additions & 0 deletions Tests/BackendIntegrationTests/MainThreadMonitor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// MainThreadMonitor.swift
//
// Created by Nacho Soto on 5/1/23.

import Foundation
@testable import RevenueCat
import XCTest

final class MainThreadMonitor {

private let queue: DispatchQueue

init() {
self.queue = .init(label: "com.revenuecat.MainThreadMonitor")
Logger.verbose("Initializing \(type(of: self)) with a threshold of \(Self.threshold.seconds) seconds")
}

deinit {
Logger.verbose("Stopping \(type(of: self))")
}

func run() {
self.queue.async { [weak self] in
while self != nil {
let semaphore = DispatchSemaphore(value: 0)
DispatchQueue.main.async {
semaphore.signal()
}

let deadline = DispatchTime.now() + Self.threshold
let result = semaphore.wait(timeout: deadline)

precondition(
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 couldn't make this a XCTFail because it wouldn't be reported if the main thread is completely blocked.

result != .timedOut,
"Main thread was blocked for more than \(Self.threshold.seconds) seconds"
)
}
}
}

private static let threshold: DispatchTimeInterval = .seconds(1)

}