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

ref: SentrySDK.close calls flush #2453

Merged
merged 11 commits into from
Dec 1, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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 (#2452)

## 7.31.2

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
5 changes: 1 addition & 4 deletions Sources/Sentry/SentrySDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,7 @@ + (void)close
}
}
[hub removeAllIntegrations];

// 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 @@ -493,6 +493,41 @@ class SentrySDKTests: XCTestCase {
assertIntegrationsInstalled(integrations: [])
}

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