diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2021-10-05 15:33:11 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2021-10-05 15:33:11 +0200 |
| commit | d6a10edc854e5b63be110c52bbbb68111c31deab (patch) | |
| tree | 2696f9925075317ffdb7d1e000ded4c140fe34d3 | |
| parent | 10dac5e931ccb60b4627f7243bec8c6a49849a81 (diff) | |
| parent | c87f3d68701125d03da1fc9170b9728ab96dd0b6 (diff) | |
| download | mullvadvpn-d6a10edc854e5b63be110c52bbbb68111c31deab.tar.xz mullvadvpn-d6a10edc854e5b63be110c52bbbb68111c31deab.zip | |
Merge branch 'promise-cancel-propagation'
| -rw-r--r-- | ios/MullvadVPN/Promise/Promise.swift | 19 | ||||
| -rw-r--r-- | ios/MullvadVPN/TunnelManager/TunnelManager.swift | 7 | ||||
| -rw-r--r-- | ios/MullvadVPNTests/PromiseTests.swift | 34 |
3 files changed, 14 insertions, 46 deletions
diff --git a/ios/MullvadVPN/Promise/Promise.swift b/ios/MullvadVPN/Promise/Promise.swift index bda24bf1bd..55091360bf 100644 --- a/ios/MullvadVPN/Promise/Promise.swift +++ b/ios/MullvadVPN/Promise/Promise.swift @@ -35,9 +35,6 @@ final class Promise<Value>: Cancellable { /// Cancellation handler. private var cancelHandler: (() -> Void)? - /// Whether to propagate cancellation to the parent promise. - private var shouldPropagateCancellation = true - /// Returns Promise resolved with the given value. class func resolved(_ value: Value) -> Self { return Self.init(value: value) @@ -108,10 +105,7 @@ final class Promise<Value>: Cancellable { case .executing: state = .cancelling - if shouldPropagateCancellation { - parent?.cancel() - } - + parent?.cancel() triggerCancelHandler() case .cancelling, .cancelled, .resolved: @@ -161,11 +155,12 @@ final class Promise<Value>: Cancellable { return self } - /// Switch the cancellation propagation behaviour - func setShouldPropagateCancellation(_ propagateCancellation: Bool) -> Self { - return lock.withCriticalBlock { - shouldPropagateCancellation = propagateCancellation - return self + /// Returns a Promise that does not propagate cancellation to the parent. + func doNotPropagateCancellation() -> Promise<Value> { + return Promise { resolver in + self.observe { completion in + resolver.resolve(completion: completion) + } } } diff --git a/ios/MullvadVPN/TunnelManager/TunnelManager.swift b/ios/MullvadVPN/TunnelManager/TunnelManager.swift index 4fc87b3a2f..c39232e942 100644 --- a/ios/MullvadVPN/TunnelManager/TunnelManager.swift +++ b/ios/MullvadVPN/TunnelManager/TunnelManager.swift @@ -215,6 +215,7 @@ class TunnelManager { .schedule(on: stateQueue) .run(on: operationQueue, categories: [OperationCategory.manageTunnelProvider, OperationCategory.changeTunnelSettings]) .requestBackgroundTime(taskName: "TunnelManager.loadAccount") + .doNotPropagateCancellation() } func startTunnel() { @@ -342,6 +343,7 @@ class TunnelManager { .schedule(on: stateQueue) .run(on: operationQueue, categories: [OperationCategory.manageTunnelProvider, OperationCategory.changeTunnelSettings]) .requestBackgroundTime(taskName: "TunnelManager.setAccount") + .doNotPropagateCancellation() } /// Remove the account token and remove the active tunnel @@ -404,6 +406,7 @@ class TunnelManager { .schedule(on: stateQueue) .run(on: operationQueue, categories: [OperationCategory.manageTunnelProvider, OperationCategory.changeTunnelSettings]) .requestBackgroundTime(taskName: "TunnelManager.unsetAccount") + .doNotPropagateCancellation() } func regeneratePrivateKey() -> Result<(), TunnelManager.Error>.Promise { @@ -430,6 +433,7 @@ class TunnelManager { .schedule(on: stateQueue) .run(on: operationQueue, categories: [OperationCategory.changeTunnelSettings]) .requestBackgroundTime(taskName: "TunnelManager.regeneratePrivateKey") + .doNotPropagateCancellation() } func rotatePrivateKey() -> Result<KeyRotationResult, TunnelManager.Error>.Promise { @@ -461,6 +465,7 @@ class TunnelManager { .schedule(on: stateQueue) .run(on: operationQueue, categories: [OperationCategory.changeTunnelSettings]) .requestBackgroundTime(taskName: "TunnelManager.rotatePrivateKey") + .doNotPropagateCancellation() } func setRelayConstraints(_ newConstraints: RelayConstraints) -> Result<(), TunnelManager.Error>.Promise { @@ -479,6 +484,7 @@ class TunnelManager { .schedule(on: stateQueue) .run(on: operationQueue, categories: [OperationCategory.changeTunnelSettings]) .requestBackgroundTime(taskName: "TunnelManager.setRelayConstraints") + .doNotPropagateCancellation() } func setDNSSettings(_ newDNSSettings: DNSSettings) -> Result<(), TunnelManager.Error>.Promise { @@ -497,6 +503,7 @@ class TunnelManager { .schedule(on: stateQueue) .run(on: operationQueue, categories: [OperationCategory.changeTunnelSettings]) .requestBackgroundTime(taskName: "TunnelManager.setDNSSettings") + .doNotPropagateCancellation() } // MARK: - Tunnel observeration diff --git a/ios/MullvadVPNTests/PromiseTests.swift b/ios/MullvadVPNTests/PromiseTests.swift index 6be0535730..4adcb0e4e6 100644 --- a/ios/MullvadVPNTests/PromiseTests.swift +++ b/ios/MullvadVPNTests/PromiseTests.swift @@ -295,38 +295,4 @@ class PromiseTests: XCTestCase { wait(for: [expectCancelHandler, expectResolve, expectObserve], timeout: 1, enforceOrder: true) } - func testShouldNotPropagateCancellation() { - let expectParentCancel = expectation(description: "Parent cancellation handler should never trigger") - expectParentCancel.isInverted = true - - let expectChildCompletion = expectation(description: "Wait for child to complete") - - let parent = Promise<Int> { resolver in - resolver.setCancelHandler { - expectParentCancel.fulfill() - } - - DispatchQueue.main.async { - resolver.resolve(value: 1) - } - } - - let child = Promise<Int>(parent: parent) { resolver in - parent.observe { completion in - resolver.resolve(completion: completion) - } - } - - _ = child.setShouldPropagateCancellation(false) - - child.observe { completion in - XCTAssertEqual(completion, .cancelled) - expectChildCompletion.fulfill() - } - - child.cancel() - - wait(for: [expectParentCancel, expectChildCompletion], timeout: 1) - } - } |
