Skip to content

Commit

Permalink
Merge 60723f7 into d8eb419
Browse files Browse the repository at this point in the history
  • Loading branch information
brustolin authored Sep 28, 2023
2 parents d8eb419 + 60723f7 commit 61894d3
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixes

- Start SDK only in the main thread (#3291)
- Remove "duplicate library" warning (#3312)

## 8.13.0
Expand Down
4 changes: 4 additions & 0 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@
D86F419827C8FEFA00490520 /* SentryCoreDataTrackerExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D86F419727C8FEFA00490520 /* SentryCoreDataTrackerExtension.swift */; };
D8751FA5274743710032F4DE /* SentryNSURLSessionTaskSearchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8751FA4274743710032F4DE /* SentryNSURLSessionTaskSearchTests.swift */; };
D875ED0B276CC84700422FAC /* SentryNSDataTrackerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D875ED0A276CC84700422FAC /* SentryNSDataTrackerTests.swift */; };
D87FA2892AB9EB06007DE933 /* MainThreadTestIntegration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D87FA2882AB9EB06007DE933 /* MainThreadTestIntegration.swift */; };
D880E3A728573E87008A90DB /* SentryBaggageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D880E3A628573E87008A90DB /* SentryBaggageTests.swift */; };
D884A20527C80F6300074664 /* SentryCoreDataTrackerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D884A20327C80F2700074664 /* SentryCoreDataTrackerTest.swift */; };
D885266427739D01001269FC /* SentryFileIOTrackingIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D885266327739D01001269FC /* SentryFileIOTrackingIntegrationTests.swift */; };
Expand Down Expand Up @@ -1728,6 +1729,7 @@
D8751FA4274743710032F4DE /* SentryNSURLSessionTaskSearchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryNSURLSessionTaskSearchTests.swift; sourceTree = "<group>"; };
D8757D142A209F7300BFEFCC /* SentrySampleDecision+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "SentrySampleDecision+Private.h"; path = "include/SentrySampleDecision+Private.h"; sourceTree = "<group>"; };
D875ED0A276CC84700422FAC /* SentryNSDataTrackerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SentryNSDataTrackerTests.swift; sourceTree = "<group>"; };
D87FA2882AB9EB06007DE933 /* MainThreadTestIntegration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainThreadTestIntegration.swift; sourceTree = "<group>"; };
D880E3A628573E87008A90DB /* SentryBaggageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryBaggageTests.swift; sourceTree = "<group>"; };
D880E3B02860A5A0008A90DB /* SentryEvent+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "SentryEvent+Private.h"; path = "include/SentryEvent+Private.h"; sourceTree = "<group>"; };
D884A20327C80F2700074664 /* SentryCoreDataTrackerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryCoreDataTrackerTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3182,6 +3184,7 @@
84281C4D2A579A0C00EE88F2 /* SentryProfilerMocksSwiftCompatible.mm */,
84AC61DA29F7654A009EEF61 /* TestDispatchSourceWrapper.swift */,
84A5D75A29D5170700388BFA /* TimeInterval+Sentry.swift */,
D87FA2882AB9EB06007DE933 /* MainThreadTestIntegration.swift */,
7B30B68126527C55006B2752 /* TestDisplayLinkWrapper.swift */,
8E25C97425F8511A00DC215B /* TestRandom.swift */,
7BE3C7762445E50A00A38442 /* TestCurrentDateProvider.swift */,
Expand Down Expand Up @@ -4484,6 +4487,7 @@
84B7FA4529B2926900AD93B1 /* TestDisplayLinkWrapper.swift in Sources */,
84AC61DB29F7654A009EEF61 /* TestDispatchSourceWrapper.swift in Sources */,
8431F01729B2851500D8DC56 /* TestSentrySystemWrapper.swift in Sources */,
D87FA2892AB9EB06007DE933 /* MainThreadTestIntegration.swift in Sources */,
84281C632A579D0700EE88F2 /* SentryProfilerMocks.mm in Sources */,
84B7FA4129B28CD200AD93B1 /* TestSentryDispatchQueueWrapper.swift in Sources */,
84B7FA3E29B28ADD00AD93B1 /* TestClient.swift in Sources */,
Expand Down
16 changes: 16 additions & 0 deletions SentryTestUtils/MainThreadTestIntegration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation
import Sentry

public class MainThreadTestIntegration: NSObject, SentryIntegrationProtocol {

public var installedInTheMainThread = false

public func install(with options: Options) -> Bool {
installedInTheMainThread = Thread.isMainThread
return true
}

public static func replaceOptionIntegrations(_ options: Options) {
options.integrations = [ NSStringFromClass(MainThreadTestIntegration.self) ]
}
}
7 changes: 7 additions & 0 deletions Sources/Sentry/SentryReachability.m
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@
}
}

void
SentryConnectivityReset(void)
{
[sentry_reachability_change_blocks removeAllObjects];
sentry_current_reachability_state = kSCNetworkReachabilityFlagsUninitialized;
}

@implementation SentryReachability

+ (void)initialize
Expand Down
40 changes: 22 additions & 18 deletions Sources/Sentry/SentrySDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
#import "SentryCrash.h"
#import "SentryCrashWrapper.h"
#import "SentryDependencyContainer.h"
#import "SentryDispatchQueueWrapper.h"
#import "SentryFileManager.h"
#import "SentryHub+Private.h"
#import "SentryLog.h"
#import "SentryMeta.h"
#import "SentryOptions+Private.h"
#import "SentryScope.h"
#import "SentryThreadWrapper.h"
#import "SentryUIDeviceWrapper.h"

@interface
Expand Down Expand Up @@ -135,27 +137,29 @@ + (void)setStartInvocations:(NSUInteger)value

+ (void)startWithOptions:(SentryOptions *)options
{
startInvocations++;

[SentryLog configure:options.debug diagnosticLevel:options.diagnosticLevel];

SentryClient *newClient = [[SentryClient alloc] initWithOptions:options];
[newClient.fileManager moveAppStateToPreviousAppState];
[newClient.fileManager moveBreadcrumbsToPreviousBreadcrumbs];

SentryScope *scope
[SentryThreadWrapper onMainThread:^{
startInvocations++;

[SentryLog configure:options.debug diagnosticLevel:options.diagnosticLevel];

SentryClient *newClient = [[SentryClient alloc] initWithOptions:options];
[newClient.fileManager moveAppStateToPreviousAppState];
[newClient.fileManager moveBreadcrumbsToPreviousBreadcrumbs];

SentryScope *scope
= options.initialScope([[SentryScope alloc] initWithMaxBreadcrumbs:options.maxBreadcrumbs]);
// The Hub needs to be initialized with a client so that closing a session
// can happen.
[SentrySDK setCurrentHub:[[SentryHub alloc] initWithClient:newClient andScope:scope]];
SENTRY_LOG_DEBUG(@"SDK initialized! Version: %@", SentryMeta.versionString);
[SentrySDK installIntegrations];

[SentryCrashWrapper.sharedInstance startBinaryImageCache];
[SentryDependencyContainer.sharedInstance.binaryImageCache start];
// The Hub needs to be initialized with a client so that closing a session
// can happen.
[SentrySDK setCurrentHub:[[SentryHub alloc] initWithClient:newClient andScope:scope]];
SENTRY_LOG_DEBUG(@"SDK initialized! Version: %@", SentryMeta.versionString);
[SentrySDK installIntegrations];
[SentryCrashWrapper.sharedInstance startBinaryImageCache];
[SentryDependencyContainer.sharedInstance.binaryImageCache start];
#if TARGET_OS_IOS
[SentryDependencyContainer.sharedInstance.uiDeviceWrapper start];
[SentryDependencyContainer.sharedInstance.uiDeviceWrapper start];
#endif
}];
}

+ (void)startWithConfigureOptions:(void (^)(SentryOptions *options))configureOptions
Expand Down
3 changes: 3 additions & 0 deletions Tests/SentryTests/Networking/SentryReachabilityTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#import "SentryReachability.h"
#import <XCTest/XCTest.h>

void SentryConnectivityReset(void);

@interface TestSentryReachabilityObserver : NSObject <SentryReachabilityObserver>
@end
@implementation TestSentryReachabilityObserver
Expand All @@ -22,6 +24,7 @@ - (void)setUp
- (void)tearDown
{
self.reachability = nil;
SentryConnectivityReset();
}

- (void)testConnectivityRepresentations
Expand Down
16 changes: 15 additions & 1 deletion Tests/SentryTests/SentrySDKTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,21 @@ class SentrySDKTests: XCTestCase {

XCTAssertEqual(flushTimeout, transport.flushInvocations.first)
}


// func testStartInTheMainThread() {
// let expect = expectation(description: "SDK Initialization")
// DispatchQueue.global(qos: .background).async {
// SentrySDK.start { options in
// MainThreadTestIntegration.replaceOptionIntegrations(options)
// }
// expect.fulfill()
// }
// wait(for: [expect], timeout: 0.5)
//
// let mainThreadIntegration = SentrySDK.currentHub().installedIntegrations().first { integration in integration is MainThreadTestIntegration } as? MainThreadTestIntegration
// XCTAssertEqual(mainThreadIntegration?.installedInTheMainThread, true, "SDK is not being initialized in the main thread")
// }

#if SENTRY_HAS_UIKIT

func testSetAppStartMeasurementConcurrently() {
Expand Down

0 comments on commit 61894d3

Please sign in to comment.