From 8edf25ae9f3299634c06c292859f86e2eded1cc9 Mon Sep 17 00:00:00 2001 From: Alex Belozierov Date: Fri, 10 Jan 2020 16:34:26 +0200 Subject: [PATCH] - change CoFuture.FututreError to CoFutureError - update tests --- .../CoFuture/CoFuture+Await.swift | 2 +- .../CoFuture/CoFuture+Wait.swift | 2 +- .../SwiftCoroutine/CoFuture/CoFuture.swift | 8 ++--- .../CoFuture/CoFutureError.swift | 11 +++++++ .../CoFuture/CoFututre+Operators.swift | 8 +++++ .../CoFuture/CoHandleFuture.swift | 2 +- Sources/SwiftCoroutine/Helpers/RefBox.swift | 4 --- SwiftCoroutine.xcodeproj/project.pbxproj | 6 ++++ .../SwiftCoroutineTests.swift | 30 ++----------------- 9 files changed, 32 insertions(+), 41 deletions(-) create mode 100644 Sources/SwiftCoroutine/CoFuture/CoFutureError.swift diff --git a/Sources/SwiftCoroutine/CoFuture/CoFuture+Await.swift b/Sources/SwiftCoroutine/CoFuture/CoFuture+Await.swift index 29d16b3..7637e2c 100644 --- a/Sources/SwiftCoroutine/CoFuture/CoFuture+Await.swift +++ b/Sources/SwiftCoroutine/CoFuture/CoFuture+Await.swift @@ -37,7 +37,7 @@ extension CoFuture { defer { timer.cancel() } while true { mutex.lock() - if isTimeOut { throw FutureError.timeout } + if isTimeOut { throw CoFutureError.timeout } if let result = result { return try result.get() } coroutine.suspend { self.subscribe(with: coroutine) { diff --git a/Sources/SwiftCoroutine/CoFuture/CoFuture+Wait.swift b/Sources/SwiftCoroutine/CoFuture/CoFuture+Wait.swift index bece0a5..86c94e2 100644 --- a/Sources/SwiftCoroutine/CoFuture/CoFuture+Wait.swift +++ b/Sources/SwiftCoroutine/CoFuture/CoFuture+Wait.swift @@ -27,7 +27,7 @@ extension CoFuture { mutex.unlock() if let timeout = timeout { if group.wait(timeout: timeout) == .timedOut { - throw FutureError.timeout + throw CoFutureError.timeout } } else { group.wait() diff --git a/Sources/SwiftCoroutine/CoFuture/CoFuture.swift b/Sources/SwiftCoroutine/CoFuture/CoFuture.swift index 2f37e21..c76892a 100644 --- a/Sources/SwiftCoroutine/CoFuture/CoFuture.swift +++ b/Sources/SwiftCoroutine/CoFuture/CoFuture.swift @@ -10,10 +10,6 @@ import Foundation public class CoFuture { - public enum FutureError: Error { - case cancelled, timeout - } - let mutex: NSRecursiveLock @RefBox var resultStorage: OutputResult? @ArcRefBox var subscriptions: [AnyHashable: OutputHandler]? @@ -27,7 +23,7 @@ public class CoFuture { } @inlinable public func cancel() { - complete(with: .failure(FutureError.cancelled)) + complete(with: .failure(CoFutureError.cancelled)) } } @@ -43,7 +39,7 @@ extension CoFuture { } @inlinable public var isCancelled: Bool { - if case .failure(let error as FutureError) = result { + if case .failure(let error as CoFutureError) = result { return error == .cancelled } return false diff --git a/Sources/SwiftCoroutine/CoFuture/CoFutureError.swift b/Sources/SwiftCoroutine/CoFuture/CoFutureError.swift new file mode 100644 index 0000000..ce6a993 --- /dev/null +++ b/Sources/SwiftCoroutine/CoFuture/CoFutureError.swift @@ -0,0 +1,11 @@ +// +// CoFutureError.swift +// SwiftCoroutine +// +// Created by Alex Belozierov on 10.01.2020. +// Copyright © 2020 Alex Belozierov. All rights reserved. +// + +public enum CoFutureError: Error { + case cancelled, timeout +} diff --git a/Sources/SwiftCoroutine/CoFuture/CoFututre+Operators.swift b/Sources/SwiftCoroutine/CoFuture/CoFututre+Operators.swift index 0b98363..add0b03 100644 --- a/Sources/SwiftCoroutine/CoFuture/CoFututre+Operators.swift +++ b/Sources/SwiftCoroutine/CoFuture/CoFututre+Operators.swift @@ -52,4 +52,12 @@ extension CoFuture { } } + @inlinable @discardableResult + public func onFutureError(_ futureError: CoFutureError, on dispatcher: Dispatcher = .sync, execute handler: @escaping () -> Void) -> CoFuture { + onResult(on: dispatcher) { + if case .failure(let error as CoFutureError) = $0, + error == futureError { handler() } + } + } + } diff --git a/Sources/SwiftCoroutine/CoFuture/CoHandleFuture.swift b/Sources/SwiftCoroutine/CoFuture/CoHandleFuture.swift index cf2ce5b..37fe85e 100644 --- a/Sources/SwiftCoroutine/CoFuture/CoHandleFuture.swift +++ b/Sources/SwiftCoroutine/CoFuture/CoHandleFuture.swift @@ -16,7 +16,7 @@ final class CoHandleFuture: CoFuture { } @inlinable override func cancel() { - unsubscribe(identifier)?(.failure(FutureError.cancelled)) + unsubscribe(identifier)?(.failure(CoFutureError.cancelled)) } } diff --git a/Sources/SwiftCoroutine/Helpers/RefBox.swift b/Sources/SwiftCoroutine/Helpers/RefBox.swift index 731fbd0..a6aeaa6 100644 --- a/Sources/SwiftCoroutine/Helpers/RefBox.swift +++ b/Sources/SwiftCoroutine/Helpers/RefBox.swift @@ -54,8 +54,4 @@ struct ArcRefBox { .init(weak: _weak) } - @inlinable var isStrong: Bool { - strong == nil - } - } diff --git a/SwiftCoroutine.xcodeproj/project.pbxproj b/SwiftCoroutine.xcodeproj/project.pbxproj index 1f4fa3f..121d030 100644 --- a/SwiftCoroutine.xcodeproj/project.pbxproj +++ b/SwiftCoroutine.xcodeproj/project.pbxproj @@ -86,6 +86,8 @@ 1AB8B31223BA3A7F0052E72F /* CoPublisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AB8B31023BA3A7F0052E72F /* CoPublisher.swift */; }; 1AECF5A923BA0E1D002A0493 /* CoFuture+Wait.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AECF5A823BA0E1D002A0493 /* CoFuture+Wait.swift */; }; 1AECF5AA23BA0E1D002A0493 /* CoFuture+Wait.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AECF5A823BA0E1D002A0493 /* CoFuture+Wait.swift */; }; + 1AFFF6ED23C8BDEA0079FF73 /* CoFutureError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AFFF6EC23C8BDEA0079FF73 /* CoFutureError.swift */; }; + 1AFFF6EE23C8BDEA0079FF73 /* CoFutureError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AFFF6EC23C8BDEA0079FF73 /* CoFutureError.swift */; }; F8CD258F20199CF700952299 /* SwiftCoroutine.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8CD258620199CF700952299 /* SwiftCoroutine.framework */; }; F8CD25AB20199D1000952299 /* SwiftCoroutine.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8CD25A220199D1000952299 /* SwiftCoroutine.framework */; }; F8CD25E92019A01600952299 /* CCoroutine.c in Sources */ = {isa = PBXBuildFile; fileRef = F810EEFF1F8F2E1C00025AD1 /* CCoroutine.c */; }; @@ -154,6 +156,7 @@ 1AB8B30C23BA31510052E72F /* CoGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoGenerator.swift; sourceTree = ""; }; 1AB8B31023BA3A7F0052E72F /* CoPublisher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoPublisher.swift; sourceTree = ""; }; 1AECF5A823BA0E1D002A0493 /* CoFuture+Wait.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CoFuture+Wait.swift"; sourceTree = ""; }; + 1AFFF6EC23C8BDEA0079FF73 /* CoFutureError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoFutureError.swift; sourceTree = ""; }; F810EEFA1F8F2B3E00025AD1 /* LICENCE */ = {isa = PBXFileReference; lastKnownFileType = text; path = LICENCE; sourceTree = ""; }; F810EEFB1F8F2B4D00025AD1 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; F810EEFF1F8F2E1C00025AD1 /* CCoroutine.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = CCoroutine.c; sourceTree = ""; }; @@ -268,6 +271,7 @@ 1AECF5A823BA0E1D002A0493 /* CoFuture+Wait.swift */, 1A739A7823ACFAF100165280 /* CoFututre+Operators.swift */, 1A739AB923B0B89600165280 /* CoFututeComposite.swift */, + 1AFFF6EC23C8BDEA0079FF73 /* CoFutureError.swift */, ); path = CoFuture; sourceTree = ""; @@ -593,6 +597,7 @@ 1AA2016523C774D100F71894 /* RefBox.swift in Sources */, 1A934FC92390491D00155B71 /* CoPromise+Combine.swift in Sources */, 1A739A6B23AC141500165280 /* Coroutine+Debug.swift in Sources */, + 1AFFF6ED23C8BDEA0079FF73 /* CoFutureError.swift in Sources */, 1A934FA223901BC400155B71 /* Publisher+Extensions.swift in Sources */, 1A61C02E23BB985B007F488C /* CoTransformFuture.swift in Sources */, 1A739AC423B107F800165280 /* Dispatcher+DispatchQueue.swift in Sources */, @@ -648,6 +653,7 @@ 1AA2016623C774D100F71894 /* RefBox.swift in Sources */, 1A934FAD2390284B00155B71 /* CoSubscription.swift in Sources */, 1A934FCA2390491F00155B71 /* CoPromise+Combine.swift in Sources */, + 1AFFF6EE23C8BDEA0079FF73 /* CoFutureError.swift in Sources */, 1A65BAC2239CEFED004C1716 /* CoroutineContext.swift in Sources */, 1A61C02F23BB985B007F488C /* CoTransformFuture.swift in Sources */, 1A739A6C23AC141500165280 /* Coroutine+Debug.swift in Sources */, diff --git a/Tests/SwiftCoroutineTests/SwiftCoroutineTests.swift b/Tests/SwiftCoroutineTests/SwiftCoroutineTests.swift index ac8289d..bd9c289 100644 --- a/Tests/SwiftCoroutineTests/SwiftCoroutineTests.swift +++ b/Tests/SwiftCoroutineTests/SwiftCoroutineTests.swift @@ -93,9 +93,6 @@ class SwiftCoroutineTests: XCTestCase { try future.await() XCTAssertTrue(current.isCurrent) XCTAssertDuration(from: date, in: 4..<5) - try future.wait() - XCTAssertTrue(current.isCurrent) - XCTAssertDuration(from: date, in: 4..<5) try Coroutine.delay(1) XCTAssertDuration(from: date, in: 5..<6) expectation.fulfill() @@ -175,14 +172,8 @@ class SwiftCoroutineTests: XCTestCase { return 5 } coroutine { - do { - _ = try future.await(timeout: .now() + 1) - XCTFail() - } catch let error as CoFuture.FutureError { - XCTAssert(error == .timeout) - } catch { - XCTFail() - } + _ = try future.await(timeout: .now() + 1) + }.onFutureError(.timeout) { XCTAssertDuration(from: date, in: 1..<2) expectation.fulfill() } @@ -228,21 +219,4 @@ class SwiftCoroutineTests: XCTestCase { XCTAssertEqual(cor.state, .prepared) } - func testCoroutineError() { - let expectation = XCTestExpectation(description: "Coroutine error test") - expectation.expectedFulfillmentCount = 2 - coroutine { - try Coroutine.delay(1) - throw Coroutine.CoroutineError.mustBeCalledInsideCoroutine - }.onError { - if let error = $0 as? Coroutine.CoroutineError { - XCTAssertEqual(error, .mustBeCalledInsideCoroutine) - } else { - XCTFail() - } - expectation.fulfill() - }.onResult(execute: expectation.fulfill) - wait(for: [expectation], timeout: 5) - } - }