@@ -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