     XCTAssert(transform.state == .Waiting)
 
     try! g.determine()
+    waitForExpectationsWithTimeout(1.0, handler: nil)
+  }
+  
+  func testCancel1()
+  {
+    let d1 = Deferred(qos: QOS_CLASS_UTILITY) {
+      () -> UInt32 in
+      usleep(10_000)
+      return arc4random()
+    }
+
+    if d1.cancel()
+    {
+      XCTAssert(d1.value == nil)
+    }
+    else
+    {
+      XCTFail()
+    }
+
+    // Set before canceling -- cancellation failure
+    let d4 = Deferred(value: arc4random())
+    XCTAssert(d4.cancel("message") == false)
+  }
+
+  func testCancel2()
+  {
+    // Test specific paths for cancellation behaviour
+    let tbd = TBD<Int>()
+
+    let e1 = expectationWithDescription("map cancellation with good input")
+    let d1 = tbd.map { $0 }
+    d1.onError { e in e1.fulfill() }
+    d1.cancel()
+
+    let e2 = expectationWithDescription("map cancellation with error input")
+    let d2 = d1.map { i -> Void in XCTFail() }
+    d2.onError { e in e2.fulfill() }
+    d2.cancel()
+
+    let e3 = expectationWithDescription("flatMap cancellation with good input")
+    let d3 = tbd.flatMap { Deferred(value: $0) }
+    d3.onError { e in e3.fulfill() }
+    d3.cancel()
+
+    let e4 = expectationWithDescription("flatMap cancellation with error input")
+    let d4 = d3.flatMap {
+      i -> Deferred<Void> in
+      XCTFail()
+      return Deferred(value: ())
+    }
+    d4.onError { e in e4.fulfill() }
+    d4.cancel()
+
+    let e5 = expectationWithDescription("flatMap cancellation with Result")
+    let d5 = tbd.flatMap { Result(value: $0) }
+    d5.onError { e in e5.fulfill() }
+    d5.cancel()
+
+    let transform = Deferred(value: { (i: Int) throws -> Int in abs(i) } )
+
+    let e6 = expectationWithDescription("apply cancellation with good inputs")
+    let d6 = tbd.apply(transform)
+    d6.onError { e in e6.fulfill() }
+    d6.cancel()
+
+    let e7 = expectationWithDescription("apply cancellation with error input")
+    let d7 = d6.apply(transform)
+    d7.onError { e in e7.fulfill() }
+    d7.cancel()
+
+    let e8 = expectationWithDescription("delay cancellation with good input")
+    let d8 = tbd.delay(ms: 100)
+    d8.onError { e in e8.fulfill() }
+    d8.cancel()
+
+    let e9 = expectationWithDescription("delay cancellation with error input")
+    let d9 = d6.delay(ms: 100)
+    d9.onError { e in e9.fulfill() }
+    d9.cancel()
+
+    do { try tbd.determine(1) }
+    catch { XCTFail() }
+
+    waitForExpectationsWithTimeout(1.0, handler: nil)
+  }
+
+  func testTimeout()
+  {
+    let value = arc4random()
+    let d = Deferred(value: value)
+
+    let d1 = d.timeout(ms: 50)
+    XCTAssert(d1.value == value)
+
+    let d2 = d.delay(ms: 5000).timeout(µs: 5000)
+    let e2 = expectationWithDescription("Timeout test")
+    d2.onValue { _ in XCTFail() }
+    d2.onError { _ in e2.fulfill() }
+
+    let d3 = d.delay(ms: 100).timeout(seconds: -1)
+    let e3 = expectationWithDescription("Unreasonable timeout test")
+    d3.onValue { _ in XCTFail() }
+    d3.onError { _ in e3.fulfill() }
+
+    let d4 = d.delay(ms: 50).timeout(ms: 100)
+    let e4 = expectationWithDescription("Timeout test 4")
+    d4.onValue { _ in e4.fulfill() }
+    d4.onError { _ in XCTFail() }
+
+    waitForExpectationsWithTimeout(1.0, handler: nil)
+  }
+
+  func testRace()
+  {
+    let count = 100
+    let g = TBD<Void>()
+    let q = dispatch_get_global_queue(qos_class_self(), 0)
+
+    let e = (0..<count).map { i in expectationWithDescription(i.description) }
+
+    dispatch_async(q) {
+      for i in 0..<count
+      {
+        dispatch_async(q) { g.notify { _ in e[i].fulfill() } }
+      }
+    }
+
+    dispatch_async(q) { try! g.determine() }
+
     waitForExpectationsWithTimeout(1.0, handler: nil)
   }
 

    XCTAssert(r5.error as? TestError == TestError.Error(error))
  }

  func testCancel1()
  {
    let d1 = Deferred(qos: QOS_CLASS_UTILITY) {
      () -> UInt32 in
      usleep(10_000)
      return arc4random()
    }

    if d1.cancel()
    {
      XCTAssert(d1.value == nil)
    }
    else
    {
      XCTFail()
    }

    // Set before canceling -- cancellation failure
    let d4 = Deferred(value: arc4random())
    XCTAssert(d4.cancel("message") == false)
  }

  func testCancel2()
  {
    // Test specific paths for cancellation behaviour
    let tbd = TBD<Int>()

    let e1 = expectationWithDescription("map cancellation with good input")
    let d1 = tbd.map { $0 }
    d1.onError { e in e1.fulfill() }
    d1.cancel()

    let e2 = expectationWithDescription("map cancellation with error input")
    let d2 = d1.map { i -> Void in XCTFail() }
    d2.onError { e in e2.fulfill() }
    d2.cancel()

    let e3 = expectationWithDescription("flatMap cancellation with good input")
    let d3 = tbd.flatMap { Deferred(value: $0) }
    d3.onError { e in e3.fulfill() }
    d3.cancel()

    let e4 = expectationWithDescription("flatMap cancellation with error input")
    let d4 = d3.flatMap {
      i -> Deferred<Void> in
      XCTFail()
      return Deferred(value: ())
    }
    d4.onError { e in e4.fulfill() }
    d4.cancel()

    let e5 = expectationWithDescription("flatMap cancellation with Result")
    let d5 = tbd.flatMap { Result(value: $0) }
    d5.onError { e in e5.fulfill() }
    d5.cancel()

    let transform = Deferred(value: { (i: Int) throws -> Int in abs(i) } )

    let e6 = expectationWithDescription("apply cancellation with good inputs")
    let d6 = tbd.apply(transform)
    d6.onError { e in e6.fulfill() }
    d6.cancel()

    let e7 = expectationWithDescription("apply cancellation with error input")
    let d7 = d6.apply(transform)
    d7.onError { e in e7.fulfill() }
    d7.cancel()

    let e8 = expectationWithDescription("delay cancellation with good input")
    let d8 = tbd.delay(ms: 100)
    d8.onError { e in e8.fulfill() }
    d8.cancel()

    let e9 = expectationWithDescription("delay cancellation with error input")
    let d9 = d6.delay(ms: 100)
    d9.onError { e in e9.fulfill() }
    d9.cancel()

    do { try tbd.determine(1) }
    catch { XCTFail() }

    waitForExpectationsWithTimeout(1.0, handler: nil)
  }

  func testTimeout()
  {
    let value = arc4random()
    let d = Deferred(value: value)

    let d1 = d.timeout(ms: 50)
    XCTAssert(d1.value == value)

    let d2 = d.delay(ms: 5000).timeout(µs: 5000)
    let e2 = expectationWithDescription("Timeout test")
    d2.onValue { _ in XCTFail() }
    d2.onError { _ in e2.fulfill() }

    let d3 = d.delay(ms: 100).timeout(seconds: -1)
    let e3 = expectationWithDescription("Unreasonable timeout test")
    d3.onValue { _ in XCTFail() }
    d3.onError { _ in e3.fulfill() }

    let d4 = d.delay(ms: 50).timeout(ms: 100)
    let e4 = expectationWithDescription("Timeout test 4")
    d4.onValue { _ in e4.fulfill() }
    d4.onError { _ in XCTFail() }

    waitForExpectationsWithTimeout(1.0, handler: nil)
  }

  func testRace()
  {
    let count = 100
    let g = TBD<Void>()
    let q = dispatch_get_global_queue(qos_class_self(), 0)

    let e = (0..<count).map { i in expectationWithDescription(i.description) }

    dispatch_async(q) {
      for i in 0..<count
      {
        dispatch_async(q) { g.notify { _ in e[i].fulfill() } }
      }
    }

    dispatch_async(q) { try! g.determine() }

    waitForExpectationsWithTimeout(1.0, handler: nil)
  }

  func testApply1()
  {
    // a silly example curried function.
    func curriedSum(a: Int)(_ b: Int) -> Int

    XCTAssert(deferred.value == value1+value2)
  }

  func testApply3()
  {
    let transform = TBD<(Int)throws->Double>()
    let operand = TBD<Int>()
