Skip to content

Commit

Permalink
test: Improve SentryReachabilityTests (#3399)
Browse files Browse the repository at this point in the history
The SentryReachabilityTests are still sometimes failing in CI. We now
skip registering and unregistering the actual callbacks to
SCNetworkReachability for all tests except one to minimize side effects.
  • Loading branch information
philipphofmann authored Nov 14, 2023
1 parent c5ff7b8 commit 44ce888
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
35 changes: 34 additions & 1 deletion Sources/Sentry/SentryReachability.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,21 @@
static SCNetworkReachabilityFlags sentry_current_reachability_state
= kSCNetworkReachabilityFlagsUninitialized;
static dispatch_queue_t sentry_reachability_queue;
static BOOL sentry_reachability_ignore_actual_callback = NO;

NSString *const SentryConnectivityCellular = @"cellular";
NSString *const SentryConnectivityWiFi = @"wifi";
NSString *const SentryConnectivityNone = @"none";

# if TEST || TESTCI
static BOOL sentry_reachability_ignore_actual_callback = NO;

void
SentrySetReachabilityIgnoreActualCallback(BOOL value)
{
SENTRY_LOG_DEBUG(@"Setting ignore actual callback to %@", value ? @"YES" : @"NO");
sentry_reachability_ignore_actual_callback = value;
}
# endif // TEST || TESTCI

/**
* Check whether the connectivity change should be noted or ignored.
Expand Down Expand Up @@ -132,10 +136,13 @@
{
SENTRY_LOG_DEBUG(
@"SentryConnectivityCallback called with target: %@; flags: %u", target, flags);
# if TEST || TESTCI
if (sentry_reachability_ignore_actual_callback) {
SENTRY_LOG_DEBUG(@"Ignoring actual callback.");
return;
}
# endif // TEST || TESTCI

SentryConnectivityCallback(flags);
}

Expand All @@ -155,6 +162,19 @@ + (void)initialize
}
}

# if TEST || TESTCI

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

return self;
}

# endif // TEST || TESTCI

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

# if TEST || TESTCI
if (self.skipRegisteringActualCallbacks) {
SENTRY_LOG_DEBUG(@"Skip registering actual callbacks");
return;
}
# endif // TEST || TESTCI

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 +241,12 @@ - (void)removeAllObservers

- (void)unsetReachabilityCallback
{
# if TEST || TESTCI
if (self.skipRegisteringActualCallbacks) {
SENTRY_LOG_DEBUG(@"Skip unsetting actual callbacks");
}
# endif // TEST || TESTCI

sentry_current_reachability_state = kSCNetworkReachabilityFlagsUninitialized;

if (_sentry_reachability_ref != nil) {
Expand Down
13 changes: 13 additions & 0 deletions Sources/Sentry/include/SentryReachability.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ NS_ASSUME_NONNULL_BEGIN

void SentryConnectivityCallback(SCNetworkReachabilityFlags flags);

# if TEST || TESTCI
/**
* Needed for testing.
*/
void SentrySetReachabilityIgnoreActualCallback(BOOL value);

# endif // TEST || TESTCI

NSString *SentryConnectivityFlagRepresentation(SCNetworkReachabilityFlags flags);

BOOL SentryConnectivityShouldReportChange(SCNetworkReachabilityFlags flags);
Expand All @@ -65,6 +68,16 @@ SENTRY_EXTERN NSString *const SentryConnectivityNone;
*/
@interface SentryReachability : NSObject

# if TEST || TESTCI

/**
* 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;

# endif // TEST || TESTCI

/**
* 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 44ce888

Please sign in to comment.