         } while true
     }
 
+    @nonobjc var anyUnfinishedTask: (Deferred<Task<Int>.Result>, Task<Int>) {
+        let d = Deferred<Task<Int>.Result>()
+        return (d, Task(d))
+    }
+
+    @nonobjc var anyFinishedTask: Task<Int> { return Task(value: 42) }
+
+    @nonobjc var anyFailedTask: Task<Int> { return Task(error: Error.First) }
+
+    @nonobjc func contrivedNextTask(for result: Int) -> Task<Int> {
+        let d = Deferred<Task<Int>.Result>()
+        let task = Task(d, cancellation: nil)
+        afterDelay(0.5, perform: {
+            d.succeed(result * 2)
+        })
+        return task
+    }
+
 }
 
 class TaskTests: XCTestCase {
 
     func testUponSuccess() {
-        let d = Deferred<Task<Int>.Result>()
-        let task = Task(d, cancellation: {})
+        let (d, task) = anyUnfinishedTask
         let expectation = expectationWithDescription("upon is called")
 
         task.uponSuccess { _ in expectation.fulfill() }

    }

    func testThatMapPassesThroughErrors() {
        let beforeExpectation = expectationWithDescription("original task filled")
        let beforeTask: Task<Int> = Task(error: Error.First)

        let afterExpectation = expectationWithDescription("mapped filled with same error")
        let afterTask: Task<String> = beforeTask.map(impossible)

        beforeTask.upon {
            XCTAssertEqual($0.error as? Error, .First)
            beforeExpectation.fulfill()
        }

        afterTask.upon {
            XCTAssertEqual($0.error as? Error, .First)
            afterExpectation.fulfill()
        }

        waitForExpectationsWithTimeout(TestTimeout, handler: nil)
    }

    func testThatThrowingMapSubstitutesWithError() {
        let beforeExpectation = expectationWithDescription("original task filled")
        let beforeTask: Task<Int> = Task(value: -1)

        let afterExpectation = expectationWithDescription("mapped filled with error")
        let afterTask: Task<String> = beforeTask.map { _ in
            throw Error.Second
        }

        beforeTask.upon {
            XCTAssertEqual($0.value, -1)
            beforeExpectation.fulfill()
        }

        afterTask.upon {
            XCTAssertEqual($0.error as? Error, .Second)
            afterExpectation.fulfill()
        }

        waitForExpectationsWithTimeout(TestTimeout, handler: nil)
    }

    func testThatFlatMapForwardsCancellationToSubsequentTask() {
        let beforeTask = Task<Int>(value: 1)

        let afterExpectation = expectationWithDescription("flatMapped task is cancelled")
        let afterTask: Task<String> = beforeTask.flatMap { _ in
            return Task(future: Future(), cancellation: afterExpectation.fulfill)
        }

        afterTask.cancel()

        waitForExpectationsWithTimeout(TestTimeout, handler: nil)
    }

    func testThatThrowingFlatMapSubstitutesWithError() {
        let beforeTask = Task<Int>(value: 1)

        let afterExpectation = expectationWithDescription("flatMapped task is cancelled")
        let afterTask: Task<String> = beforeTask.flatMap { _ -> Task<String> in
            throw Error.Second
        }

        afterTask.uponFailure {
            XCTAssertEqual($0 as? Error, .Second)
            afterExpectation.fulfill()
        }

        waitForExpectationsWithTimeout(TestTimeout, handler: nil)
    }

    func testThatRecoverPassesThroughValues() {
        let beforeExpectation = expectationWithDescription("original task filled")
        let beforeTask: Task<Int> = Task(value: 1)

        let afterExpectation = expectationWithDescription("mapped filled with same error")
        let afterTask: Task<Int> = beforeTask.recover(impossible)

        beforeTask.upon {
            XCTAssertEqual($0.value, 1)
            beforeExpectation.fulfill()
        }

        afterTask.upon {
            XCTAssertNil($0.error)
            afterExpectation.fulfill()
        }

        waitForExpectationsWithTimeout(TestTimeout, handler: nil)
    }

    func testThatRecoverMapsFailures() {
        let beforeExpectation = expectationWithDescription("original task filled")
        let beforeTask: Task<Int> = Task(error: Error.First)

        let afterExpectation = expectationWithDescription("mapped filled with same error")
        let afterTask: Task<Int> = beforeTask.recover { _ in 42 }

        beforeTask.upon {
            XCTAssertEqual($0.error as? Error, .First)
            beforeExpectation.fulfill()
        }

        afterTask.upon {
            XCTAssertEqual($0.value, 42)
            afterExpectation.fulfill()
        }

        waitForExpectationsWithTimeout(TestTimeout, handler: nil)

    }

    func testThatThrowingMapSubstitutesWithError() {
        let expectation = expectationWithDescription("mapped filled with error")
        let task: Task<String> = anyFinishedTask.map(upon: executor) { _ in
            throw Error.Second
        }

        task.upon(executor) {
            XCTAssertEqual($0.error as? Error, .Second)
            expectation.fulfill()
        }

        waitForExpectationsWithTimeout(TestTimeout, handler: nil)
        assertExecutorCalled(2)
    }

    func testThatFlatMapForwardsCancellationToSubsequentTask() {
        let expectation = expectationWithDescription("flatMapped task is cancelled")
        let task: Task<String> = anyFinishedTask.flatMap(upon: executor) { _ in
            return Task(future: Future(), cancellation: expectation.fulfill)
        }

        task.cancel()

        waitForExpectationsWithTimeout(TestTimeout, handler: nil)
        assertExecutorCalled(1)
    }

    func testThatThrowingFlatMapSubstitutesWithError() {
        let expectation = expectationWithDescription("flatMapped task is cancelled")
        let task = anyFinishedTask.flatMap(upon: executor) { _ -> Task<String> in
            throw Error.Second
        }

        task.uponFailure {
            XCTAssertEqual($0 as? Error, .Second)
            expectation.fulfill()
        }

        waitForExpectationsWithTimeout(TestTimeout, handler: nil)
