Skip to content

Commit

Permalink
Merge 84d017d into dd266f5
Browse files Browse the repository at this point in the history
  • Loading branch information
philipphofmann authored Dec 1, 2022
2 parents dd266f5 + 84d017d commit be7358c
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ This version adds a dependency on Swift.
- Rename `SentryOptions.enablePreWarmedAppStartTracking` to `enablePreWarmedAppStartTracing`
- Rename `SentryOptions.enableFileIOTracking` to `enableFileIOTracing`
- Rename `SentryOptions.enableCoreDataTracking` to `enableCoreDataTracing`
- SentrySDK.close calls flush, which is a blocking call (#2453)

## 7.31.3

Expand Down
2 changes: 2 additions & 0 deletions Samples/iOS-Swift/iOS-Swift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,13 @@ class ViewController: UIViewController {
SentrySDK.crash()
}

// swiftlint:disable force_unwrapping
@IBAction func unwrapCrash(_ sender: Any) {
let a: String! = nil
let b: String = a!
print(b)
}
// swiftlint:enable force_unwrapping

@IBAction func asyncCrash(_ sender: Any) {
DispatchQueue.main.async {
Expand Down
7 changes: 7 additions & 0 deletions Sources/Sentry/Public/SentryClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ NS_ASSUME_NONNULL_BEGIN
@interface SentryClient : NSObject
SENTRY_NO_INIT

@property (nonatomic, assign, readonly) BOOL isEnabled;

@property (nonatomic, strong) SentryOptions *options;

/**
Expand Down Expand Up @@ -119,6 +121,11 @@ SENTRY_NO_INIT
*/
- (void)flush:(NSTimeInterval)timeout NS_SWIFT_NAME(flush(timeout:));

/**
* Disables the client and calls flush with ``SentryOptions/shutdownTimeInterval``.
*/
- (void)close;

@end

NS_ASSUME_NONNULL_END
5 changes: 5 additions & 0 deletions Sources/Sentry/Public/SentryHub.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ SENTRY_NO_INIT
*/
- (void)flush:(NSTimeInterval)timeout NS_SWIFT_NAME(flush(timeout:));

/**
* Calls flush with ``SentryOptions/shutdownTimeInterval``.
*/
- (void)close;

@end

NS_ASSUME_NONNULL_END
5 changes: 5 additions & 0 deletions Sources/Sentry/Public/SentryOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ NS_SWIFT_NAME(Options)
*/
@property (nonatomic, assign) BOOL enabled;

/**
* Controls the flush duration when calling ``SentrySDK/close``.
*/
@property (nonatomic, assign) NSTimeInterval shutdownTimeInterval;

/**
* When enabled, the SDK sends crashes to Sentry. Default value is YES.
*/
Expand Down
3 changes: 2 additions & 1 deletion Sources/Sentry/Public/SentrySDK.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ SENTRY_NO_INIT
+ (void)flush:(NSTimeInterval)timeout NS_SWIFT_NAME(flush(timeout:));

/**
* Closes the SDK and uninstalls all the integrations.
* Closes the SDK, uninstalls all the integrations, and calls flush with
* ``SentryOptions/shutdownTimeInterval``.
*/
+ (void)close;

Expand Down
9 changes: 8 additions & 1 deletion Sources/Sentry/SentryClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ - (instancetype)initWithOptions:(SentryOptions *)options
timezone:(NSTimeZone *)timezone
{
if (self = [super init]) {
_isEnabled = YES;
self.options = options;
self.transportAdapter = transportAdapter;
self.fileManager = fileManager;
Expand Down Expand Up @@ -501,6 +502,12 @@ - (void)flush:(NSTimeInterval)timeout
[self.transportAdapter flush:timeout];
}

- (void)close
{
_isEnabled = NO;
[self flush:self.options.shutdownTimeInterval];
}

- (SentryEvent *_Nullable)prepareEvent:(SentryEvent *)event
withScope:(SentryScope *)scope
alwaysAttachStacktrace:(BOOL)alwaysAttachStacktrace
Expand Down Expand Up @@ -632,7 +639,7 @@ - (BOOL)isSampled:(NSNumber *)sampleRate

- (BOOL)isDisabled
{
return !self.options.enabled || nil == self.options.parsedDsn;
return !_isEnabled || !self.options.enabled || nil == self.options.parsedDsn;
}

- (void)logDisabledMessage
Expand Down
5 changes: 5 additions & 0 deletions Sources/Sentry/SentryHub.m
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,11 @@ - (void)flush:(NSTimeInterval)timeout
}
}

- (void)close
{
[_client close];
}

@end

NS_ASSUME_NONNULL_END
1 change: 1 addition & 0 deletions Sources/Sentry/SentryOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ - (instancetype)init
{
if (self = [super init]) {
self.enabled = YES;
self.shutdownTimeInterval = 2.0;
self.enableCrashHandler = YES;
self.diagnosticLevel = kSentryLevelDebug;
self.debug = NO;
Expand Down
4 changes: 1 addition & 3 deletions Sources/Sentry/SentrySDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,7 @@ + (void)close
[[SentryDependencyContainer sharedInstance].appStateManager stopWithForce:YES];
#endif

// close the client
SentryClient *client = [hub getClient];
client.options.enabled = NO;
[hub close];
[hub bindClient:nil];

[SentrySDK setCurrentHub:nil];
Expand Down
4 changes: 4 additions & 0 deletions Tests/SentryTests/SentryClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ class SentryClientTest: XCTestCase {
clearTestState()
}

func testClientIsEnabled() {
XCTAssertTrue(fixture.getSut().isEnabled)
}

func testCaptureMessage() {
let eventId = fixture.getSut().capture(message: fixture.messageAsString)

Expand Down
1 change: 1 addition & 0 deletions Tests/SentryTests/SentryOptionsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ - (void)testEmptyConstructorSetsDefaultValues
- (void)assertDefaultValues:(SentryOptions *)options
{
XCTAssertEqual(YES, options.enabled);
XCTAssertEqual(2.0, options.shutdownTimeInterval);
XCTAssertEqual(NO, options.debug);
XCTAssertEqual(kSentryLevelDebug, options.diagnosticLevel);
XCTAssertEqual(options.environment, kSentryDefaultEnvironment);
Expand Down
35 changes: 35 additions & 0 deletions Tests/SentryTests/SentrySDKTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,41 @@ class SentrySDKTests: XCTestCase {
}
#endif

func testClose_SetsClientToNil() {
SentrySDK.start { options in
options.dsn = SentrySDKTests.dsnAsString
}

SentrySDK.close()

XCTAssertNil(SentrySDK.currentHub().client())
}

func testClose_ClosesClient() {
SentrySDK.start { options in
options.dsn = SentrySDKTests.dsnAsString
}

let client = SentrySDK.currentHub().client()
SentrySDK.close()

XCTAssertFalse(client?.isEnabled ?? true)
}

func testClose_CallsFlushCorrectlyOnTransport() {
SentrySDK.start { options in
options.dsn = SentrySDKTests.dsnAsString
}

let transport = TestTransport()
let client = SentryClient(options: fixture.options)
Dynamic(client).transportAdapter = TestTransportAdapter(transport: transport, options: fixture.options)
SentrySDK.currentHub().bindClient(client)
SentrySDK.close()

XCTAssertEqual(Options().shutdownTimeInterval, transport.flushInvocations.first)
}

func testFlush_CallsFlushCorrectlyOnTransport() {
SentrySDK.start { options in
options.dsn = SentrySDKTests.dsnAsString
Expand Down

0 comments on commit be7358c

Please sign in to comment.