Skip to content

Commit

Permalink
Performance: new check to ensure serialization / deserialization is d…
Browse files Browse the repository at this point in the history
…one from background thread (#2496)
  • Loading branch information
NachoSoto authored May 19, 2023
1 parent c0c6893 commit 50ffbfb
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
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 */; };
4F0201C42A13C85500091612 /* Assertions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F0201C32A13C85500091612 /* Assertions.swift */; };
4F2017D52A15587F0061F6EF /* OfflineStoreKitIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F2017D42A15587F0061F6EF /* OfflineStoreKitIntegrationTests.swift */; };
4F2018732A15797D0061F6EF /* TestLogHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57057FF728B0048900995F21 /* TestLogHandler.swift */; };
4F69EB092A14406E00ED6D4B /* Matchers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F69EB082A14406E00ED6D4B /* Matchers.swift */; };
Expand Down Expand Up @@ -873,6 +874,7 @@
37E35EEE7783629CDE41B70C /* SystemInfoTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SystemInfoTests.swift; sourceTree = "<group>"; };
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>"; };
4F0201C32A13C85500091612 /* Assertions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Assertions.swift; sourceTree = "<group>"; };
4F2017D42A15587F0061F6EF /* OfflineStoreKitIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OfflineStoreKitIntegrationTests.swift; sourceTree = "<group>"; };
4F69EB082A14406E00ED6D4B /* Matchers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Matchers.swift; sourceTree = "<group>"; };
4F8A58162A16EE3500EF97AD /* MockOfflineCustomerInfoCreator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockOfflineCustomerInfoCreator.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2294,6 +2296,7 @@
2D971CC02744364C0093F35F /* SKError+Extensions.swift */,
57BD50A927692B7500211D6D /* StoreKitError+Extensions.swift */,
57C2931428BFEF4F0054EDFC /* PurchasesError.swift */,
4F0201C32A13C85500091612 /* Assertions.swift */,
);
path = "Error Handling";
sourceTree = "<group>";
Expand Down Expand Up @@ -3001,6 +3004,7 @@
57069A5428E3918400B86355 /* AsyncExtensions.swift in Sources */,
B34605C5279A6E380031CA74 /* CustomerInfoResponseHandler.swift in Sources */,
5796A3A927D7C43500653165 /* Deprecations.swift in Sources */,
4F0201C42A13C85500091612 /* Assertions.swift in Sources */,
5753ED8E294A662400CBAB54 /* DateFormatter+Extensions.swift in Sources */,
B3AA6238268B926F00894871 /* SystemInfo.swift in Sources */,
5746508E275949F20053AB09 /* DispatchTimeInterval+Extensions.swift in Sources */,
Expand Down
46 changes: 46 additions & 0 deletions Sources/Error Handling/Assertions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// 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
//
// Assertions.swift
//
// Created by Nacho Soto on 5/16/23.

import Foundation

/// Equivalent to `assert`, but will only evaluate condition during RC tests.
/// - Note: this is a no-op in release builds.
@inline(__always)
func RCTestAssert(
_ condition: @autoclosure () -> Bool,
_ message: @autoclosure () -> String,
file: StaticString = #file,
line: UInt = #line
) {
#if DEBUG
guard ProcessInfo.isRunningRevenueCatTests else { return }

precondition(condition(), message(), file: file, line: line)
#endif
}

@inline(__always)
func RCTestAssertNotMainThread(
function: StaticString = #function,
file: StaticString = #file,
line: UInt = #line
) {
#if DEBUG
RCTestAssert(
!Thread.isMainThread,
"\(function) should not be called from the main thread",
file: file,
line: line
)
#endif
}
2 changes: 2 additions & 0 deletions Sources/Networking/HTTPClient/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ private extension HTTPClient {
urlRequest: URLRequest,
data: Data?,
error networkError: Error?) {
RCTestAssertNotMainThread()

let response = self.parse(
urlResponse: urlResponse,
request: request,
Expand Down
22 changes: 9 additions & 13 deletions Sources/Networking/Operations/NetworkOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,15 @@ class NetworkOperation: Operation {
}

deinit {
#if DEBUG
if ProcessInfo.isRunningRevenueCatTests {
precondition(
self.didStart,
"\(type(of: self)) was deallocated but it never started. Did it need to be created?"
)
precondition(
self.isFinished,
"\(type(of: self)) started but never finished. " +
"Did the operation not call `completion` in its `begin` implementation?"
)
}
#endif
RCTestAssert(
self.didStart,
"\(type(of: self)) was deallocated but it never started. Did it need to be created?"
)
RCTestAssert(
self.isFinished,
"\(type(of: self)) started but never finished. " +
"Did the operation not call `completion` in its `begin` implementation?"
)
}

override final func main() {
Expand Down

0 comments on commit 50ffbfb

Please sign in to comment.