From 0fca748540244e047710e3c0b5fbefa517bb7505 Mon Sep 17 00:00:00 2001 From: Roman Podymov Date: Sat, 23 May 2020 22:02:09 +0200 Subject: [PATCH 1/3] init(error: E, delay: DispatchTimeInterval) --- Sources/BrightFutures/Future.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/BrightFutures/Future.swift b/Sources/BrightFutures/Future.swift index 4a5f5ff..855352a 100644 --- a/Sources/BrightFutures/Future.swift +++ b/Sources/BrightFutures/Future.swift @@ -47,6 +47,10 @@ public final class Future: Async> { public init(value: T, delay: DispatchTimeInterval) { super.init(result: .success(value), delay: delay) } + + public init(error: E, delay: DispatchTimeInterval) { + super.init(result: .failure(error), delay: delay) + } public required init(other: A) where A.Value == Value { super.init(other: other) From 57a53f7db101e34c96402a947c1511b97aea0e31 Mon Sep 17 00:00:00 2001 From: Roman Podymov Date: Sat, 23 May 2020 22:07:27 +0200 Subject: [PATCH 2/3] init(error: E, delay: DispatchTimeInterval) tests --- .../BrightFuturesTests.swift | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/Tests/BrightFuturesTests/BrightFuturesTests.swift b/Tests/BrightFuturesTests/BrightFuturesTests.swift index 7c8f17e..5a7eaa1 100644 --- a/Tests/BrightFuturesTests/BrightFuturesTests.swift +++ b/Tests/BrightFuturesTests/BrightFuturesTests.swift @@ -115,6 +115,29 @@ extension BrightFuturesTests { } } + func testFailedAfterFuture() { + let f = Future(error: .justAnError, delay: 1.second) + + XCTAssertFalse(f.isCompleted) + + Thread.sleep(forTimeInterval: 0.2) + + XCTAssertFalse(f.isCompleted) + + Thread.sleep(forTimeInterval: 1.0) + + XCTAssert(f.isCompleted) + + if let error = f.error { + switch error { + case .justAnError: + XCTAssert(true) + case .justAnotherError: + XCTAssert(false) + } + } + } + // this is inherently impossible to test, but we'll give it a try func testNeverCompletingFuture() { let f = Future() @@ -902,6 +925,33 @@ extension BrightFuturesTests { self.waitForExpectations(timeout: 2, handler: nil) } + func testUtilsFirstCompletedWithError() { + let futures: [Future] = [ + Future(value: 3, delay: 500.milliseconds), + Future(error: .justAnotherError, delay: 300.milliseconds), + Future(value: 23, delay: 400.milliseconds), + Future(value: 4, delay: 300.milliseconds), + Future(error: .justAnError, delay: 100.milliseconds), + Future(value: 83, delay: 400.milliseconds), + ] + + let e = self.expectation() + + futures.firstCompleted().onSuccess { _ in + XCTAssert(false) + }.onFailure { error in + switch error { + case .justAnError: + XCTAssert(true) + case .justAnotherError: + XCTAssert(false) + } + e.fulfill() + } + + self.waitForExpectations(timeout: 2, handler: nil) + } + func testUtilsSequence() { let futures = (1...10).map { fibonacciFuture($0) } From d4b74ebc848c9c5ed81eeb17d29df509df9f29cd Mon Sep 17 00:00:00 2001 From: Roman Podymov Date: Sat, 23 May 2020 22:13:12 +0200 Subject: [PATCH 3/3] Added missing e.fulfill() --- Tests/BrightFuturesTests/BrightFuturesTests.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/BrightFuturesTests/BrightFuturesTests.swift b/Tests/BrightFuturesTests/BrightFuturesTests.swift index 5a7eaa1..c2a009d 100644 --- a/Tests/BrightFuturesTests/BrightFuturesTests.swift +++ b/Tests/BrightFuturesTests/BrightFuturesTests.swift @@ -939,6 +939,7 @@ extension BrightFuturesTests { futures.firstCompleted().onSuccess { _ in XCTAssert(false) + e.fulfill() }.onFailure { error in switch error { case .justAnError: