From 189472a1a0654f1c4f94941fefa46da76eb05a4a Mon Sep 17 00:00:00 2001 From: Andrew McKnight Date: Mon, 21 Nov 2022 22:06:34 -0900 Subject: [PATCH 1/9] test: shorten some tests (#2422) --- .../Performance/SentryPerformanceTrackerTests.swift | 2 +- .../SentryTests/Performance/SentryTracerTests.swift | 12 +++++++----- Tests/SentryTests/SentryScopeSwiftTests.swift | 4 ++-- Tests/SentryTests/TestClient.swift | 1 - 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Tests/SentryTests/Integrations/Performance/SentryPerformanceTrackerTests.swift b/Tests/SentryTests/Integrations/Performance/SentryPerformanceTrackerTests.swift index dbd2e6a4ace..37bbde91733 100644 --- a/Tests/SentryTests/Integrations/Performance/SentryPerformanceTrackerTests.swift +++ b/Tests/SentryTests/Integrations/Performance/SentryPerformanceTrackerTests.swift @@ -314,7 +314,7 @@ class SentryPerformanceTrackerTests: XCTestCase { let queue = DispatchQueue(label: "SentryPerformanceTrackerTests", attributes: [.concurrent, .initiallyInactive]) let group = DispatchGroup() - for _ in 0 ..< 50_000 { + for _ in 0 ..< 50 { group.enter() queue.async { let childId = self.startSpan(tracker: sut) diff --git a/Tests/SentryTests/Performance/SentryTracerTests.swift b/Tests/SentryTests/Performance/SentryTracerTests.swift index 53be5bcfe32..a1c5f16c09c 100644 --- a/Tests/SentryTests/Performance/SentryTracerTests.swift +++ b/Tests/SentryTests/Performance/SentryTracerTests.swift @@ -697,12 +697,14 @@ class SentryTracerTests: XCTestCase { let queue = DispatchQueue(label: "SentryTracerTests", attributes: [.concurrent, .initiallyInactive]) let group = DispatchGroup() - - for _ in 0 ..< 5_000 { + + let children = 5 + let grandchildren = 10 + for _ in 0 ..< children { group.enter() queue.async { let grandChild = child.startChild(operation: self.fixture.transactionOperation) - for _ in 0 ..< 9 { + for _ in 0 ..< grandchildren { let grandGrandChild = grandChild.startChild(operation: self.fixture.transactionOperation) grandGrandChild.finish() } @@ -721,7 +723,7 @@ class SentryTracerTests: XCTestCase { assertOneTransactionCaptured(sut) let spans = getSerializedTransaction()["spans"]! as! [[String: Any]] - XCTAssertEqual(spans.count, 50_001) + XCTAssertEqual(spans.count, children * (grandchildren + 1) + 1) } // Although we only run this test above the below specified versions, we expect the @@ -735,7 +737,7 @@ class SentryTracerTests: XCTestCase { let queue = DispatchQueue(label: "", qos: .background, attributes: [.concurrent, .initiallyInactive] ) let group = DispatchGroup() - let transactions = 10_000 + let transactions = 5 for _ in 0.. Date: Tue, 22 Nov 2022 09:51:50 +0100 Subject: [PATCH 2/9] Revert "test: shorten some tests (#2422)" (#2427) This reverts commit 189472a1a0654f1c4f94941fefa46da76eb05a4a. --- .../Performance/SentryPerformanceTrackerTests.swift | 2 +- .../SentryTests/Performance/SentryTracerTests.swift | 12 +++++------- Tests/SentryTests/SentryScopeSwiftTests.swift | 4 ++-- Tests/SentryTests/TestClient.swift | 1 + 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Tests/SentryTests/Integrations/Performance/SentryPerformanceTrackerTests.swift b/Tests/SentryTests/Integrations/Performance/SentryPerformanceTrackerTests.swift index 37bbde91733..dbd2e6a4ace 100644 --- a/Tests/SentryTests/Integrations/Performance/SentryPerformanceTrackerTests.swift +++ b/Tests/SentryTests/Integrations/Performance/SentryPerformanceTrackerTests.swift @@ -314,7 +314,7 @@ class SentryPerformanceTrackerTests: XCTestCase { let queue = DispatchQueue(label: "SentryPerformanceTrackerTests", attributes: [.concurrent, .initiallyInactive]) let group = DispatchGroup() - for _ in 0 ..< 50 { + for _ in 0 ..< 50_000 { group.enter() queue.async { let childId = self.startSpan(tracker: sut) diff --git a/Tests/SentryTests/Performance/SentryTracerTests.swift b/Tests/SentryTests/Performance/SentryTracerTests.swift index a1c5f16c09c..53be5bcfe32 100644 --- a/Tests/SentryTests/Performance/SentryTracerTests.swift +++ b/Tests/SentryTests/Performance/SentryTracerTests.swift @@ -697,14 +697,12 @@ class SentryTracerTests: XCTestCase { let queue = DispatchQueue(label: "SentryTracerTests", attributes: [.concurrent, .initiallyInactive]) let group = DispatchGroup() - - let children = 5 - let grandchildren = 10 - for _ in 0 ..< children { + + for _ in 0 ..< 5_000 { group.enter() queue.async { let grandChild = child.startChild(operation: self.fixture.transactionOperation) - for _ in 0 ..< grandchildren { + for _ in 0 ..< 9 { let grandGrandChild = grandChild.startChild(operation: self.fixture.transactionOperation) grandGrandChild.finish() } @@ -723,7 +721,7 @@ class SentryTracerTests: XCTestCase { assertOneTransactionCaptured(sut) let spans = getSerializedTransaction()["spans"]! as! [[String: Any]] - XCTAssertEqual(spans.count, children * (grandchildren + 1) + 1) + XCTAssertEqual(spans.count, 50_001) } // Although we only run this test above the below specified versions, we expect the @@ -737,7 +735,7 @@ class SentryTracerTests: XCTestCase { let queue = DispatchQueue(label: "", qos: .background, attributes: [.concurrent, .initiallyInactive] ) let group = DispatchGroup() - let transactions = 5 + let transactions = 10_000 for _ in 0.. Date: Wed, 23 Nov 2022 03:05:40 -0900 Subject: [PATCH 3/9] meta: disable swiftlint file length check in TBDBClient.swift (#2435) --- Samples/TrendingMovies/TrendingMovies/TMDb/TMDbClient.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Samples/TrendingMovies/TrendingMovies/TMDb/TMDbClient.swift b/Samples/TrendingMovies/TrendingMovies/TMDb/TMDbClient.swift index 17f628aa4b0..49b503fd5d6 100644 --- a/Samples/TrendingMovies/TrendingMovies/TMDb/TMDbClient.swift +++ b/Samples/TrendingMovies/TrendingMovies/TMDb/TMDbClient.swift @@ -1,5 +1,7 @@ import Foundation +// swiftlint:disable type_body_length + class TMDbClient: NSObject, URLSessionDataDelegate, URLSessionTaskDelegate { enum TMDbClientError: Swift.Error { case URLConstructionFailed @@ -608,3 +610,5 @@ private func getRegionQueryItem(locale: Locale) -> URLQueryItem? { } return nil } + +// swiftlint:enable type_body_length From df5d8d7eeb33bc8699b0e6b9f3fdfc0c59848b73 Mon Sep 17 00:00:00 2001 From: Dhiogo Brustolin Date: Tue, 29 Nov 2022 08:46:08 -0300 Subject: [PATCH 4/9] fix: Core data span status with error (#2439) Checks core data request result to set span finish status. --- CHANGELOG.md | 6 +++ Sources/Sentry/SentryCoreDataTracker.m | 5 +- Tests/SentryTests/ClearTestState.swift | 53 +++++++++++-------- .../CoreData/SentryCoreDataTrackerTest.swift | 28 ++++++++++ 4 files changed, 66 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f6ca4b08dc..47db1c614c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Core data span status with error (#2439) + ## 7.31.2 ### Fixes diff --git a/Sources/Sentry/SentryCoreDataTracker.m b/Sources/Sentry/SentryCoreDataTracker.m index ef592ff7842..98567e4a56d 100644 --- a/Sources/Sentry/SentryCoreDataTracker.m +++ b/Sources/Sentry/SentryCoreDataTracker.m @@ -50,7 +50,7 @@ - (NSArray *)managedObjectContext:(NSManagedObjectContext *)context [fetchSpan setDataValue:[NSNumber numberWithInteger:result.count] forKey:@"read_count"]; [fetchSpan - finishWithStatus:error != nil ? kSentrySpanStatusInternalError : kSentrySpanStatusOk]; + finishWithStatus:result == nil ? kSentrySpanStatusInternalError : kSentrySpanStatusOk]; SENTRY_LOG_DEBUG(@"SentryCoreDataTracker automatically finished span with status: %@", error == nil ? @"ok" : @"error"); @@ -91,8 +91,7 @@ - (BOOL)managedObjectContext:(NSManagedObjectContext *)context BOOL result = original(error); if (fetchSpan) { - [fetchSpan - finishWithStatus:*error != nil ? kSentrySpanStatusInternalError : kSentrySpanStatusOk]; + [fetchSpan finishWithStatus:result ? kSentrySpanStatusOk : kSentrySpanStatusInternalError]; SENTRY_LOG_DEBUG(@"SentryCoreDataTracker automatically finished span with status: %@", *error == nil ? @"ok" : @"error"); diff --git a/Tests/SentryTests/ClearTestState.swift b/Tests/SentryTests/ClearTestState.swift index 02db84e1294..60b77d8f4a9 100644 --- a/Tests/SentryTests/ClearTestState.swift +++ b/Tests/SentryTests/ClearTestState.swift @@ -2,27 +2,34 @@ import Foundation import Sentry func clearTestState() { - SentrySDK.close() - SentrySDK.setCurrentHub(nil) - SentrySDK.crashedLastRunCalled = false - SentrySDK.startInvocations = 0 - - PrivateSentrySDKOnly.onAppStartMeasurementAvailable = nil - PrivateSentrySDKOnly.appStartMeasurementHybridSDKMode = false - SentrySDK.setAppStartMeasurement(nil) - CurrentDate.setCurrentDateProvider(nil) - SentryNetworkTracker.sharedInstance.disable() - - #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) - let framesTracker = SentryFramesTracker.sharedInstance() - framesTracker.stop() - framesTracker.resetFrames() - - setenv("ActivePrewarm", "0", 1) - SentryAppStartTracker.load() - #endif - - SentryDependencyContainer.reset() - Dynamic(SentryGlobalEventProcessor.shared()).removeAllProcessors() - SentrySwizzleWrapper.sharedInstance.removeAllCallbacks() + TestCleanup.clearTestState() +} + +@objcMembers +class TestCleanup: NSObject { + static func clearTestState() { + SentrySDK.close() + SentrySDK.setCurrentHub(nil) + SentrySDK.crashedLastRunCalled = false + SentrySDK.startInvocations = 0 + + PrivateSentrySDKOnly.onAppStartMeasurementAvailable = nil + PrivateSentrySDKOnly.appStartMeasurementHybridSDKMode = false + SentrySDK.setAppStartMeasurement(nil) + CurrentDate.setCurrentDateProvider(nil) + SentryNetworkTracker.sharedInstance.disable() + + #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) + let framesTracker = SentryFramesTracker.sharedInstance() + framesTracker.stop() + framesTracker.resetFrames() + + setenv("ActivePrewarm", "0", 1) + SentryAppStartTracker.load() + #endif + + SentryDependencyContainer.reset() + Dynamic(SentryGlobalEventProcessor.shared()).removeAllProcessors() + SentrySwizzleWrapper.sharedInstance.removeAllCallbacks() + } } diff --git a/Tests/SentryTests/Integrations/Performance/CoreData/SentryCoreDataTrackerTest.swift b/Tests/SentryTests/Integrations/Performance/CoreData/SentryCoreDataTrackerTest.swift index 66303f720c2..3a3e0ff8519 100644 --- a/Tests/SentryTests/Integrations/Performance/CoreData/SentryCoreDataTrackerTest.swift +++ b/Tests/SentryTests/Integrations/Performance/CoreData/SentryCoreDataTrackerTest.swift @@ -188,6 +188,34 @@ class SentryCoreDataTrackerTests: XCTestCase { XCTAssertEqual(updated["SecondTestEntity"] as? Int, 2) } + func test_Request_with_Error() { + let fetch = NSFetchRequest(entityName: "TestEntity") + + let transaction = startTransaction() + let sut = fixture.getSut() + + let context = fixture.context + + let _ = try? sut.fetchManagedObjectContext(context, request: fetch) { _, _ in + return nil + } + + XCTAssertEqual(transaction.children.count, 1) + XCTAssertEqual(transaction.children[0].context.status, .internalError) + } + + func test_save_with_Error() { + let transaction = startTransaction() + let sut = fixture.getSut() + fixture.context.inserted = [fixture.testEntity()] + try? sut.saveManagedObjectContext(fixture.context) { _ in + return false + } + + XCTAssertEqual(transaction.children.count, 1) + XCTAssertEqual(transaction.children[0].context.status, .internalError) + } + func test_Save_NoChanges() { let sut = fixture.getSut() From fb8e522ba513851a23d8434193b7afe40f719073 Mon Sep 17 00:00:00 2001 From: Dhiogo Brustolin Date: Tue, 29 Nov 2022 09:18:56 -0300 Subject: [PATCH 5/9] Update integration-tests.yml (#2461) Fixed the web integration test by pinning the version of firewalk --- .github/workflows/integration-tests.yml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml index 6b1b59ff2fc..7d6ef30fa88 100644 --- a/.github/workflows/integration-tests.yml +++ b/.github/workflows/integration-tests.yml @@ -47,11 +47,14 @@ jobs: - name: Download and Apply Patch run: ./apply-patch.sh "${{ github.event.pull_request.head.sha }}" "${{ github.sha }}" add-sentry-to-alamofire - - name: Install Firewalk - run: brew install alamofire/alamofire/firewalk + - name: Download Firewalk + run: curl "https://github.com/Alamofire/Firewalk/releases/download/0.8.1/firewalk.zip" --output firewalk.zip -L + + - name: Unzip Firewalk + run: Unzip "firewalk.zip" - name: Start Firewalk - run: firewalk & + run: ./firewalk & - name: Validate Firewalk is running run: curl http://localhost:8080/ From 9ba19c0b7719b25ffdccb097ed021e5aaadc87dc Mon Sep 17 00:00:00 2001 From: Philipp Hofmann Date: Tue, 29 Nov 2022 15:43:42 +0100 Subject: [PATCH 6/9] fix: Reporting crashes when restarting the SDK (#2440) The SDK did not report crashes after restarting the SDK. This is fixed by restoring the SentryCrashMonitors when installing SentryCrash after uninstalling it. --- CHANGELOG.md | 1 + Samples/iOS-Swift/iOS-Swift/AppDelegate.swift | 10 +- .../iOS-Swift/Base.lproj/Main.storyboard | 62 +++++--- .../iOS-Swift/iOS-Swift/ViewController.swift | 7 + .../iOS-Swift/iOS13-Swift/AppDelegate.swift | 38 +++-- Sentry.xcodeproj/project.pbxproj | 4 + Sources/Sentry/SentryCrashIntegration.m | 4 +- Sources/Sentry/SentryCrashWrapper.m | 9 +- Sources/Sentry/include/SentryCrashWrapper.h | 6 +- .../SentryCrashInstallation+Private.h | 14 ++ .../Installations/SentryCrashInstallation.h | 5 + .../Installations/SentryCrashInstallation.m | 28 ++-- .../Recording/Monitors/SentryCrashMonitor.c | 8 +- .../Recording/Monitors/SentryCrashMonitor.h | 10 +- .../SentryCrashMonitor_MachException.c | 62 ++++++-- .../SentryCrashMonitor_MachException.h | 7 + Sources/SentryCrash/Recording/SentryCrash.h | 21 ++- Sources/SentryCrash/Recording/SentryCrash.m | 120 ++++++++------ Sources/SentryCrash/Recording/SentryCrashC.c | 48 +----- Sources/SentryCrash/Recording/SentryCrashC.h | 9 +- .../Recording/SentryCrashCachedData.c | 6 + .../Recording/SentryCrashCachedData.h | 1 + .../Recording/Tools/SentryCrashLogger.c | 31 ---- .../Recording/Tools/SentryCrashLogger.h | 11 -- .../Tools/SentryCrashMachineContext.c | 36 +---- .../Tools/SentryCrashMachineContext.h | 6 - .../TestNSNotificationCenterWrapper.swift | 16 +- ...SentryNetworkTrackerIntegrationTests.swift | 2 + .../SentryCrashIntegrationTests.swift | 4 +- .../SentryCrashInstallationTests.m | 146 ++++++++++++++++++ .../SentryCrash/SentryCrashLogger_Tests.m | 25 --- .../SentryThreadInspectorTests.swift | 2 +- .../SentryCrash/TestSentryCrashWrapper.h | 2 +- .../SentryCrash/TestSentryCrashWrapper.m | 6 +- 34 files changed, 467 insertions(+), 300 deletions(-) create mode 100644 Tests/SentryTests/SentryCrash/SentryCrashInstallationTests.m diff --git a/CHANGELOG.md b/CHANGELOG.md index 47db1c614c1..84a8c470d38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Fixes +- Reporting crashes when restarting the SDK (#2440) - Core data span status with error (#2439) ## 7.31.2 diff --git a/Samples/iOS-Swift/iOS-Swift/AppDelegate.swift b/Samples/iOS-Swift/iOS-Swift/AppDelegate.swift index 69290edc2b6..203c22620c6 100644 --- a/Samples/iOS-Swift/iOS-Swift/AppDelegate.swift +++ b/Samples/iOS-Swift/iOS-Swift/AppDelegate.swift @@ -7,9 +7,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? static let defaultDSN = "https://6cc9bae94def43cab8444a99e0031c28@o447951.ingest.sentry.io/5428557" - - func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { - + + static func startSentry() { // For testing purposes, we want to be able to change the DSN and store it to disk. In a real app, you shouldn't need this behavior. let dsn = DSNStorage.shared.getDSN() ?? AppDelegate.defaultDSN DSNStorage.shared.saveDSN(dsn: dsn) @@ -45,6 +44,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate { let httpStatusCodeRange = HttpStatusCodeRange(min: 400, max: 599) options.failedRequestStatusCodes = [ httpStatusCodeRange ] } + } + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + + AppDelegate.startSentry() if #available(iOS 14.0, *) { metricKit.receiveReports() diff --git a/Samples/iOS-Swift/iOS-Swift/Base.lproj/Main.storyboard b/Samples/iOS-Swift/iOS-Swift/Base.lproj/Main.storyboard index 0ed14a09aba..029585c65a8 100644 --- a/Samples/iOS-Swift/iOS-Swift/Base.lproj/Main.storyboard +++ b/Samples/iOS-Swift/iOS-Swift/Base.lproj/Main.storyboard @@ -1,9 +1,9 @@ - + - + @@ -18,13 +18,13 @@ - + - + - + + + - + + + - +