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

test: Improve SentryReachabilityTests #3399

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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