Skip to content

Commit

Permalink
- change CoFuture.FututreError to CoFutureError
Browse files Browse the repository at this point in the history
- update tests
  • Loading branch information
Alex Belozierov committed Jan 10, 2020
1 parent 02e8b4e commit 8edf25a
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Sources/SwiftCoroutine/CoFuture/CoFuture+Await.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftCoroutine/CoFuture/CoFuture+Wait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 2 additions & 6 deletions Sources/SwiftCoroutine/CoFuture/CoFuture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import Foundation

public class CoFuture<Output> {

public enum FutureError: Error {
case cancelled, timeout
}

let mutex: NSRecursiveLock
@RefBox var resultStorage: OutputResult?
@ArcRefBox var subscriptions: [AnyHashable: OutputHandler]?
Expand All @@ -27,7 +23,7 @@ public class CoFuture<Output> {
}

@inlinable public func cancel() {
complete(with: .failure(FutureError.cancelled))
complete(with: .failure(CoFutureError.cancelled))
}

}
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions Sources/SwiftCoroutine/CoFuture/CoFutureError.swift
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 8 additions & 0 deletions Sources/SwiftCoroutine/CoFuture/CoFututre+Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,12 @@ extension CoFuture {
}
}

@inlinable @discardableResult
public func onFutureError(_ futureError: CoFutureError, on dispatcher: Dispatcher = .sync, execute handler: @escaping () -> Void) -> CoFuture<Output> {
onResult(on: dispatcher) {
if case .failure(let error as CoFutureError) = $0,
error == futureError { handler() }
}
}

}
2 changes: 1 addition & 1 deletion Sources/SwiftCoroutine/CoFuture/CoHandleFuture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class CoHandleFuture<Output>: CoFuture<Output> {
}

@inlinable override func cancel() {
unsubscribe(identifier)?(.failure(FutureError.cancelled))
unsubscribe(identifier)?(.failure(CoFutureError.cancelled))
}

}
4 changes: 0 additions & 4 deletions Sources/SwiftCoroutine/Helpers/RefBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,4 @@ struct ArcRefBox<T> {
.init(weak: _weak)
}

@inlinable var isStrong: Bool {
strong == nil
}

}
6 changes: 6 additions & 0 deletions SwiftCoroutine.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -154,6 +156,7 @@
1AB8B30C23BA31510052E72F /* CoGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoGenerator.swift; sourceTree = "<group>"; };
1AB8B31023BA3A7F0052E72F /* CoPublisher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoPublisher.swift; sourceTree = "<group>"; };
1AECF5A823BA0E1D002A0493 /* CoFuture+Wait.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CoFuture+Wait.swift"; sourceTree = "<group>"; };
1AFFF6EC23C8BDEA0079FF73 /* CoFutureError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoFutureError.swift; sourceTree = "<group>"; };
F810EEFA1F8F2B3E00025AD1 /* LICENCE */ = {isa = PBXFileReference; lastKnownFileType = text; path = LICENCE; sourceTree = "<group>"; };
F810EEFB1F8F2B4D00025AD1 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
F810EEFF1F8F2E1C00025AD1 /* CCoroutine.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = CCoroutine.c; sourceTree = "<group>"; };
Expand Down Expand Up @@ -268,6 +271,7 @@
1AECF5A823BA0E1D002A0493 /* CoFuture+Wait.swift */,
1A739A7823ACFAF100165280 /* CoFututre+Operators.swift */,
1A739AB923B0B89600165280 /* CoFututeComposite.swift */,
1AFFF6EC23C8BDEA0079FF73 /* CoFutureError.swift */,
);
path = CoFuture;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down
30 changes: 2 additions & 28 deletions Tests/SwiftCoroutineTests/SwiftCoroutineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -175,14 +172,8 @@ class SwiftCoroutineTests: XCTestCase {
return 5
}
coroutine {
do {
_ = try future.await(timeout: .now() + 1)
XCTFail()
} catch let error as CoFuture<Int>.FutureError {
XCTAssert(error == .timeout)
} catch {
XCTFail()
}
_ = try future.await(timeout: .now() + 1)
}.onFutureError(.timeout) {
XCTAssertDuration(from: date, in: 1..<2)
expectation.fulfill()
}
Expand Down Expand Up @@ -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)
}

}

0 comments on commit 8edf25a

Please sign in to comment.