Skip to content

Commit 44ce888

Browse files
test: Improve SentryReachabilityTests (#3399)
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.
1 parent c5ff7b8 commit 44ce888

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

Sources/Sentry/SentryReachability.m

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,21 @@
3535
static SCNetworkReachabilityFlags sentry_current_reachability_state
3636
= kSCNetworkReachabilityFlagsUninitialized;
3737
static dispatch_queue_t sentry_reachability_queue;
38-
static BOOL sentry_reachability_ignore_actual_callback = NO;
3938

4039
NSString *const SentryConnectivityCellular = @"cellular";
4140
NSString *const SentryConnectivityWiFi = @"wifi";
4241
NSString *const SentryConnectivityNone = @"none";
4342

43+
# if TEST || TESTCI
44+
static BOOL sentry_reachability_ignore_actual_callback = NO;
45+
4446
void
4547
SentrySetReachabilityIgnoreActualCallback(BOOL value)
4648
{
49+
SENTRY_LOG_DEBUG(@"Setting ignore actual callback to %@", value ? @"YES" : @"NO");
4750
sentry_reachability_ignore_actual_callback = value;
4851
}
52+
# endif // TEST || TESTCI
4953

5054
/**
5155
* Check whether the connectivity change should be noted or ignored.
@@ -132,10 +136,13 @@
132136
{
133137
SENTRY_LOG_DEBUG(
134138
@"SentryConnectivityCallback called with target: %@; flags: %u", target, flags);
139+
# if TEST || TESTCI
135140
if (sentry_reachability_ignore_actual_callback) {
136141
SENTRY_LOG_DEBUG(@"Ignoring actual callback.");
137142
return;
138143
}
144+
# endif // TEST || TESTCI
145+
139146
SentryConnectivityCallback(flags);
140147
}
141148

@@ -155,6 +162,19 @@ + (void)initialize
155162
}
156163
}
157164

165+
# if TEST || TESTCI
166+
167+
- (instancetype)init
168+
{
169+
if (self = [super init]) {
170+
self.skipRegisteringActualCallbacks = NO;
171+
}
172+
173+
return self;
174+
}
175+
176+
# endif // TEST || TESTCI
177+
158178
- (void)addObserver:(id<SentryReachabilityObserver>)observer;
159179
{
160180
SENTRY_LOG_DEBUG(@"Adding observer: %@", observer);
@@ -171,6 +191,13 @@ - (void)addObserver:(id<SentryReachabilityObserver>)observer;
171191
return;
172192
}
173193

194+
# if TEST || TESTCI
195+
if (self.skipRegisteringActualCallbacks) {
196+
SENTRY_LOG_DEBUG(@"Skip registering actual callbacks");
197+
return;
198+
}
199+
# endif // TEST || TESTCI
200+
174201
sentry_reachability_queue
175202
= dispatch_queue_create("io.sentry.cocoa.connectivity", DISPATCH_QUEUE_SERIAL);
176203
// Ensure to call CFRelease for the return value of SCNetworkReachabilityCreateWithName, see
@@ -214,6 +241,12 @@ - (void)removeAllObservers
214241

215242
- (void)unsetReachabilityCallback
216243
{
244+
# if TEST || TESTCI
245+
if (self.skipRegisteringActualCallbacks) {
246+
SENTRY_LOG_DEBUG(@"Skip unsetting actual callbacks");
247+
}
248+
# endif // TEST || TESTCI
249+
217250
sentry_current_reachability_state = kSCNetworkReachabilityFlagsUninitialized;
218251

219252
if (_sentry_reachability_ref != nil) {

Sources/Sentry/include/SentryReachability.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@ NS_ASSUME_NONNULL_BEGIN
3434

3535
void SentryConnectivityCallback(SCNetworkReachabilityFlags flags);
3636

37+
# if TEST || TESTCI
3738
/**
3839
* Needed for testing.
3940
*/
4041
void SentrySetReachabilityIgnoreActualCallback(BOOL value);
4142

43+
# endif // TEST || TESTCI
44+
4245
NSString *SentryConnectivityFlagRepresentation(SCNetworkReachabilityFlags flags);
4346

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

71+
# if TEST || TESTCI
72+
73+
/**
74+
* Only needed for testing. Use this flag to skip registering and unregistering the actual callbacks
75+
* to SCNetworkReachability to minimize side effects.
76+
*/
77+
@property (nonatomic, assign) BOOL skipRegisteringActualCallbacks;
78+
79+
# endif // TEST || TESTCI
80+
6881
/**
6982
* Add an observer which is called each time network connectivity changes.
7083
*/

Tests/SentryTests/Networking/SentryReachabilityTests.m

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ @implementation SentryReachabilityTest
3535

3636
- (void)setUp
3737
{
38-
self.reachability = [[SentryReachability alloc] init];
3938
// Ignore the actual reachability callbacks, cause we call the callbacks manually.
4039
// Otherwise, the actual reachability callbacks are called during later unrelated tests causing
4140
// flakes.
4241
SentrySetReachabilityIgnoreActualCallback(YES);
42+
43+
self.reachability = [[SentryReachability alloc] init];
44+
self.reachability.skipRegisteringActualCallbacks = YES;
4345
}
4446

4547
- (void)tearDown
@@ -145,5 +147,19 @@ - (void)testReportSameReachabilityState_OnlyCalledOnce
145147
[self.reachability removeObserver:observer];
146148
}
147149

150+
/**
151+
* We only want to make sure running the actual registering and unregistering callbacks doesn't
152+
* crash.
153+
*/
154+
- (void)testRegisteringActualCallbacks
155+
{
156+
self.reachability.skipRegisteringActualCallbacks = NO;
157+
158+
TestSentryReachabilityObserver *observer = [[TestSentryReachabilityObserver alloc] init];
159+
160+
[self.reachability addObserver:observer];
161+
[self.reachability removeObserver:observer];
162+
}
163+
148164
@end
149165
#endif // !TARGET_OS_WATCH

0 commit comments

Comments
 (0)