Skip to content

Commit

Permalink
Merge bb98549 into de46f06
Browse files Browse the repository at this point in the history
  • Loading branch information
philipphofmann authored Nov 13, 2023
2 parents de46f06 + bb98549 commit d6116d1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,16 @@ class SentryBreadcrumbTrackerTests: XCTestCase {
sut.start(with: delegate)
sut.startSwizzle()

// Using UINavigationController as a parent doesn't work on tvOS 17.0
// for an unknown reason. Therefore, we manually set the parent.
class ParentUIViewController: UIViewController {

}
let parentController = ParentUIViewController()
let viewController = UIViewController()
_ = UINavigationController(rootViewController: viewController)
parentController.addChild(viewController)
viewController.title = "test title"

print("delegate: \(String(describing: delegate))")
print("tracker: \(sut); SentryBreadcrumbTracker.delegate: \(String(describing: Dynamic(sut).delegate.asObject))")
viewController.viewDidAppear(false)
Expand All @@ -97,7 +104,7 @@ class SentryBreadcrumbTrackerTests: XCTestCase {
XCTAssertEqual("UIViewController", lifeCycleCrumb.data?["screen"] as? String)
XCTAssertEqual("test title", lifeCycleCrumb.data?["title"] as? String)
XCTAssertEqual("false", lifeCycleCrumb.data?["beingPresented"] as? String)
XCTAssertEqual("UINavigationController", lifeCycleCrumb.data?["parentViewController"] as? String)
XCTAssertEqual("ParentUIViewController", lifeCycleCrumb.data?["parentViewController"] as? String)

clearTestState()
}
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 d6116d1

Please sign in to comment.