Skip to content

Commit

Permalink
fix: improvements resetting Reachability and other test state (#3356)
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight committed Oct 23, 2023
1 parent 6001822 commit 2ce582e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
4 changes: 0 additions & 4 deletions SentryTestUtils/ClearTestState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ class TestCleanup: NSObject {

#endif // os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)

#if !os(watchOS)
SentryDependencyContainer.sharedInstance().reachability.removeAllObservers()
#endif // !os(watchOS)

SentryDependencyContainer.reset()
Dynamic(SentryGlobalEventProcessor.shared()).removeAllProcessors()
SentryPerformanceTracker.shared.clear()
Expand Down
6 changes: 6 additions & 0 deletions Sources/Sentry/SentryDependencyContainer.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ + (instancetype)sharedInstance

+ (void)reset
{
#if !TARGET_OS_WATCH
if (instance) {
[instance->_reachability removeAllObservers];
}
#endif // !TARGET_OS_WATCH

instance = [[SentryDependencyContainer alloc] init];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ class SentrySessionTrackerTests: XCTestCase {
private var fixture: Fixture!
private var sut: SessionTracker!

override func setUp() {
override class func setUp() {
super.setUp()

clearTestState()
}

override func setUp() {
super.setUp()

fixture = Fixture()

Expand Down
17 changes: 8 additions & 9 deletions Tests/SentryTests/Networking/SentryReachabilityTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ - (instancetype)init

- (void)connectivityChanged:(BOOL)connected typeDescription:(nonnull NSString *)typeDescription
{
NSLog(@"Received connectivity notification: %i; type: %@", connected, typeDescription);
NSLog(
@"Received connectivity notification: %i; type: %s", connected, typeDescription.UTF8String);
self.connectivityChangedInvocations++;
}

Expand Down Expand Up @@ -77,26 +78,24 @@ - (void)testMultipleReachabilityObservers
NSLog(@"[Sentry] [TEST] throwaway reachability callback, setting to reachable");
SentryConnectivityCallback(self.reachability.sentry_reachability_ref,
kSCNetworkReachabilityFlagsReachable, nil); // ignored, as it's the first callback
NSLog(@"[Sentry] [TEST] reachability callback to set to intervention required");
SentryConnectivityCallback(self.reachability.sentry_reachability_ref,
kSCNetworkReachabilityFlagsInterventionRequired, nil);
NSLog(@"[Sentry] [TEST] reachability callback set to unreachable");
SentryConnectivityCallback(self.reachability.sentry_reachability_ref, 0, nil);

NSLog(@"[Sentry] [TEST] creating observer B");
TestSentryReachabilityObserver *observerB = [[TestSentryReachabilityObserver alloc] init];
NSLog(@"[Sentry] [TEST] adding observer B as reachability observer");
[self.reachability addObserver:observerB];

NSLog(@"[Sentry] [TEST] reachability callback to set to back to reachable");
NSLog(@"[Sentry] [TEST] reachability callback set back to reachable");
SentryConnectivityCallback(
self.reachability.sentry_reachability_ref, kSCNetworkReachabilityFlagsReachable, nil);
NSLog(@"[Sentry] [TEST] reachability callback to set to back to intervention required");
SentryConnectivityCallback(self.reachability.sentry_reachability_ref,
kSCNetworkReachabilityFlagsInterventionRequired, nil);
NSLog(@"[Sentry] [TEST] reachability callback set back to unreachable");
SentryConnectivityCallback(self.reachability.sentry_reachability_ref, 0, nil);

NSLog(@"[Sentry] [TEST] removing observer B as reachability observer");
[self.reachability removeObserver:observerB];

NSLog(@"[Sentry] [TEST] reachability callback to set to back to reachable");
NSLog(@"[Sentry] [TEST] reachability callback set back to reachable");
SentryConnectivityCallback(
self.reachability.sentry_reachability_ref, kSCNetworkReachabilityFlagsReachable, nil);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ class SentryStacktraceBuilderTests: XCTestCase {
let queue = DispatchQueue(label: "SentryStacktraceBuilderTests")

var sut: SentryStacktraceBuilder {
SentryDependencyContainer.sharedInstance().reachability = TestSentryReachability()
let res = SentryStacktraceBuilder(crashStackEntryMapper: SentryCrashStackEntryMapper(inAppLogic: SentryInAppLogic(inAppIncludes: [], inAppExcludes: [])))
res.symbolicate = true
return res
}
}

private var fixture: Fixture!

override class func setUp() {
super.setUp()
clearTestState()
}

override func setUp() {
super.setUp()
fixture = Fixture()
clearTestState()
}

override func tearDown() {
Expand Down

0 comments on commit 2ce582e

Please sign in to comment.