Skip to content

Commit ce4c2e9

Browse files
committed
Improve tests
1 parent de5344b commit ce4c2e9

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

Sources/Swift/Networking/SentryReachability.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public class SentryReachability: NSObject {
3636
#if DEBUG || SENTRY_TEST || SENTRY_TEST_CI
3737
@objc public var skipRegisteringActualCallbacks = false
3838
private var ignoreActualCallback = false
39+
40+
public var pathMonitorIsNil: Bool {
41+
return pathMonitor == nil
42+
}
3943
#endif // DEBUG || SENTRY_TEST || SENTRY_TEST_CI
4044

4145
@objc(addObserver:)

Tests/SentryTests/Networking/SentryReachabilitySwiftTests.swift

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,8 @@ final class SentryReachabilitySwiftTests: XCTestCase {
116116

117117
func testAddRemoveFromMultipleThreads() throws {
118118
let sut = SentryReachability()
119-
// Calling the methods of SCNetworkReachability in a tight loop from
120-
// multiple threads is not an actual use case, and it leads to flaky test
121-
// results. With this test, we want to test if the adding and removing
122-
// observers are adequately synchronized and not if we call
123-
// SCNetworkReachability correctly.
119+
// With this test, we want to test if the adding and removing
120+
// observers are adequately synchronized.
124121
sut.skipRegisteringActualCallbacks = true
125122
testConcurrentModifications(writeWork: { _ in
126123
sut.add(TestSentryReachabilityObserver())
@@ -129,20 +126,27 @@ final class SentryReachabilitySwiftTests: XCTestCase {
129126
})
130127
}
131128

132-
/// This tests actually test NWPathMonitor response, if it becomes blaky we can disable it
133-
func testRegisteringActualCallbacks_CallbackIsCalled() {
129+
func testAddingAndRemovingObserversCleanTheMonitor() {
134130
reachability.skipRegisteringActualCallbacks = false
135131
reachability.setReachabilityIgnoreActualCallback(false)
136-
137-
let expectation = XCTestExpectation(description: "Callback should be called")
138-
139132
let observer = TestSentryReachabilityObserver()
140-
observer.onReachabilityChanged = { _, _ in
141-
expectation.fulfill()
142-
}
143133

134+
// Ensure starting scenario
135+
XCTAssertTrue(reachability.pathMonitorIsNil)
136+
137+
// Do
144138
reachability.add(observer)
145139

146-
wait(for: [expectation], timeout: 5)
140+
// Verify
141+
// Monitor should not be nil when at least one observer is added
142+
XCTAssertFalse(reachability.pathMonitorIsNil)
143+
144+
// Do again
145+
sleep(1)
146+
reachability.remove(observer)
147+
148+
// Verify
149+
// Ensure when all observers are removed, the monitor is set to nil
150+
XCTAssertTrue(reachability.pathMonitorIsNil)
147151
}
148152
}

0 commit comments

Comments
 (0)