Skip to content
This repository has been archived by the owner on Jul 3, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
…into RomanPodymov-master
  • Loading branch information
Thomvis committed Jun 11, 2021
2 parents b5ee6a8 + d4b74eb commit 256de16
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/BrightFutures/Future.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public final class Future<T, E: Error>: Async<Result<T, E>> {
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<A: AsyncType>(other: A) where A.Value == Value {
super.init(other: other)
Expand Down
51 changes: 51 additions & 0 deletions Tests/BrightFuturesTests/BrightFuturesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,29 @@ extension BrightFuturesTests {
}
}

func testFailedAfterFuture() {
let f = Future<Int, TestError>(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<Int, Never>()
Expand Down Expand Up @@ -895,6 +918,34 @@ extension BrightFuturesTests {
self.waitForExpectations(timeout: 2, handler: nil)
}

func testUtilsFirstCompletedWithError() {
let futures: [Future<Int, TestError>] = [
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)
e.fulfill()
}.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) }

Expand Down

0 comments on commit 256de16

Please sign in to comment.