Skip to content

Commit

Permalink
Merge 3419d6c into af1f4dd
Browse files Browse the repository at this point in the history
  • Loading branch information
philipphofmann authored Nov 13, 2023
2 parents af1f4dd + 3419d6c commit f144d27
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Sources/Sentry/SentryReachability.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
void
SentrySetReachabilityIgnoreActualCallback(BOOL value)
{
SENTRY_LOG_DEBUG(@"Setting ignore actual callback to %@", value ? @"YES" : @"NO");
sentry_reachability_ignore_actual_callback = value;
}

Expand Down Expand Up @@ -155,6 +156,15 @@ + (void)initialize
}
}

- (instancetype)init
{
if (self = [super init]) {
self.skipRegisteringActualCallbacks = NO;
}

return self;
}

- (void)addObserver:(id<SentryReachabilityObserver>)observer;
{
SENTRY_LOG_DEBUG(@"Adding observer: %@", observer);
Expand All @@ -171,6 +181,11 @@ - (void)addObserver:(id<SentryReachabilityObserver>)observer;
return;
}

if (self.skipRegisteringActualCallbacks) {
SENTRY_LOG_DEBUG(@"Skip registering actual callbacks");
return;
}

sentry_reachability_queue
= dispatch_queue_create("io.sentry.cocoa.connectivity", DISPATCH_QUEUE_SERIAL);
// Ensure to call CFRelease for the return value of SCNetworkReachabilityCreateWithName, see
Expand Down Expand Up @@ -214,6 +229,10 @@ - (void)removeAllObservers

- (void)unsetReachabilityCallback
{
if (self.skipRegisteringActualCallbacks) {
SENTRY_LOG_DEBUG(@"Skip unsetting actual callbacks");
}

sentry_current_reachability_state = kSCNetworkReachabilityFlagsUninitialized;

if (_sentry_reachability_ref != nil) {
Expand Down
6 changes: 6 additions & 0 deletions Sources/Sentry/include/SentryReachability.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ SENTRY_EXTERN NSString *const SentryConnectivityNone;
*/
@interface SentryReachability : NSObject

/**
* Only needed for testing. Use this flag to skip registering and unregistering the actual callbacks
* to SCNetworkReachability to minimize side effects.
*/
@property (nonatomic, assign) BOOL skipRegisteringActualCallbacks;

/**
* Add an observer which is called each time network connectivity changes.
*/
Expand Down
18 changes: 17 additions & 1 deletion Tests/SentryTests/Networking/SentryReachabilityTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ @implementation SentryReachabilityTest

- (void)setUp
{
self.reachability = [[SentryReachability alloc] init];
// Ignore the actual reachability callbacks, cause we call the callbacks manually.
// Otherwise, the actual reachability callbacks are called during later unrelated tests causing
// flakes.
SentrySetReachabilityIgnoreActualCallback(YES);

self.reachability = [[SentryReachability alloc] init];
self.reachability.skipRegisteringActualCallbacks = YES;
}

- (void)tearDown
Expand Down Expand Up @@ -145,5 +147,19 @@ - (void)testReportSameReachabilityState_OnlyCalledOnce
[self.reachability removeObserver:observer];
}

/**
* We only want to make sure running the actual registering and unregistering callbacks doesn't
* crash.
*/
- (void)testRegisteringActualCallbacks
{
self.reachability.skipRegisteringActualCallbacks = NO;

TestSentryReachabilityObserver *observer = [[TestSentryReachabilityObserver alloc] init];

[self.reachability addObserver:observer];
[self.reachability removeObserver:observer];
}

@end
#endif // !TARGET_OS_WATCH

0 comments on commit f144d27

Please sign in to comment.