|
| 1 | +// |
| 2 | +// CancelTestsSwift.swift |
| 3 | +// BAPromise |
| 4 | +// |
| 5 | +// Created by Ben Allison on 8/23/18. |
| 6 | +// Copyright © 2018 Ben Allison. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import XCTest |
| 10 | + |
| 11 | +class CancelTestsSwift: XCTestCase { |
| 12 | + |
| 13 | + override func setUp() { |
| 14 | + super.setUp() |
| 15 | + // Put setup code here. This method is called before the invocation of each test method in the class. |
| 16 | + } |
| 17 | + |
| 18 | + override func tearDown() { |
| 19 | + // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 20 | + super.tearDown() |
| 21 | + } |
| 22 | + |
| 23 | + func testCancel() { |
| 24 | + let expectation = XCTestExpectation() |
| 25 | + let promise = Promise<Void>() |
| 26 | + promise.cancelled({ expectation.fulfill() }, on: DispatchQueue.main) |
| 27 | + promise.cancel() |
| 28 | + self.wait(for: [expectation], timeout: 0.5) |
| 29 | + } |
| 30 | + |
| 31 | + func testDoneAfterCancel() { |
| 32 | + let expectation = XCTestExpectation() |
| 33 | + let promise = Promise<Void>() |
| 34 | + promise.cancelled({ expectation.fulfill() }, on: DispatchQueue.main) |
| 35 | + |
| 36 | + let token = promise.then({ |
| 37 | + XCTFail("Cancelation should prevent calling of done block") |
| 38 | + }, observed: { |
| 39 | + XCTFail("Cancelation should prevent calling of observed block") |
| 40 | + }, rejected: { (error) in |
| 41 | + XCTFail("Cancelation should prevent calling of rejected block") |
| 42 | + }, always: { |
| 43 | + XCTFail("Cancelation should prevent calling of finally block") |
| 44 | + }, queue: DispatchQueue.main) |
| 45 | + token.cancel() |
| 46 | + |
| 47 | + self.wait(for: [expectation], timeout: 0.5) |
| 48 | + RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.1)) |
| 49 | + } |
| 50 | + |
| 51 | + func testCancelTokenAfterFulfillment() { |
| 52 | + let promise = Promise<Void>() |
| 53 | + promise.fulfill(with: .success) |
| 54 | + promise.cancelled({ XCTFail("Unexpected cancelled callback") }, on: DispatchQueue.main) |
| 55 | + let token = promise.then({ }, queue: DispatchQueue.main) |
| 56 | + token.cancel() |
| 57 | + RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.1)) |
| 58 | + } |
| 59 | + |
| 60 | + func testCancelPromiseAfterFulfilment() { |
| 61 | + let promise = Promise<Void>() |
| 62 | + promise.fulfill(with: .success) |
| 63 | + promise.cancelled({ XCTFail("Unexpected cancelled callback") }, on: DispatchQueue.main) |
| 64 | + promise.cancel() |
| 65 | + RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.1)) |
| 66 | + } |
| 67 | + |
| 68 | + func testCancelPromiseAfterRejection() { |
| 69 | + let promise = Promise<Void>() |
| 70 | + promise.fulfill(with: .failure(NSError())) |
| 71 | + promise.cancelled({ XCTFail("Unexpected cancelled callback") }, on: DispatchQueue.main) |
| 72 | + promise.cancel() |
| 73 | + RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.1)) |
| 74 | + } |
| 75 | + |
| 76 | + func testLateCancelCallback() { |
| 77 | + let expectation = XCTestExpectation() |
| 78 | + let promise = Promise<Void>() |
| 79 | + promise.cancel() |
| 80 | + promise.cancelled({ expectation.fulfill() }, on: DispatchQueue.main) |
| 81 | + |
| 82 | + self.wait(for: [expectation], timeout: 0.5) |
| 83 | + } |
| 84 | + |
| 85 | + func testCancelToken() { |
| 86 | + let expectation = XCTestExpectation() |
| 87 | + let promise = Promise<Void>() |
| 88 | + promise.cancelled({ XCTFail("unepected onCancel") }, on: DispatchQueue.main) |
| 89 | + promise.fulfill(with: .success) |
| 90 | + |
| 91 | + let token = promise.then({ |
| 92 | + XCTFail("Cancelation should prevent calling of done block") |
| 93 | + }, observed: { |
| 94 | + XCTFail("Cancelation should prevent calling of observed block") |
| 95 | + }, rejected: { (error) in |
| 96 | + XCTFail("Cancelation should prevent calling of rejected block") |
| 97 | + }, always: { |
| 98 | + XCTFail("Cancelation should prevent calling of finally block") |
| 99 | + }, queue: DispatchQueue.main) |
| 100 | + token.cancel() |
| 101 | + |
| 102 | + DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) { |
| 103 | + expectation.fulfill() |
| 104 | + } |
| 105 | + self.wait(for: [expectation], timeout: 0.5) |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + func testCancelAsyncARC() { |
| 110 | + let expectation = XCTestExpectation() |
| 111 | + let promise = Promise<Void>() |
| 112 | + promise.cancelled({ XCTFail("unepected onCancel") }, on: DispatchQueue.main) |
| 113 | + |
| 114 | + DispatchQueue.main.async { |
| 115 | + promise.fulfill(with: .success) |
| 116 | + let token = promise.then({ |
| 117 | + XCTFail("Cancelation should prevent calling of done block") |
| 118 | + }, observed: { |
| 119 | + XCTFail("Cancelation should prevent calling of observed block") |
| 120 | + }, rejected: { (error) in |
| 121 | + XCTFail("Cancelation should prevent calling of rejected block") |
| 122 | + }, always: { |
| 123 | + XCTFail("Cancelation should prevent calling of finally block") |
| 124 | + }, queue: DispatchQueue.main) |
| 125 | + token.cancel() |
| 126 | + } |
| 127 | + |
| 128 | + DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) { |
| 129 | + expectation.fulfill() |
| 130 | + } |
| 131 | + self.wait(for: [expectation], timeout: 0.5) |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + func testCancelReject() { |
| 136 | + let expectation = XCTestExpectation() |
| 137 | + let promise = Promise<Void>() |
| 138 | + promise.cancelled({ XCTFail("unepected onCancel") }, on: DispatchQueue.main) |
| 139 | + promise.fulfill(with: .failure(NSError())) |
| 140 | + |
| 141 | + let token = promise.then({ |
| 142 | + XCTFail("Cancelation should prevent calling of done block") |
| 143 | + }, observed: { |
| 144 | + XCTFail("Cancelation should prevent calling of observed block") |
| 145 | + }, rejected: { (error) in |
| 146 | + XCTFail("Cancelation should prevent calling of rejected block") |
| 147 | + }, always: { |
| 148 | + XCTFail("Cancelation should prevent calling of finally block") |
| 149 | + }, queue: DispatchQueue.main) |
| 150 | + token.cancel() |
| 151 | + |
| 152 | + DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) { |
| 153 | + expectation.fulfill() |
| 154 | + } |
| 155 | + self.wait(for: [expectation], timeout: 0.5) |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + func testCancelAsyncARCReject() { |
| 160 | + let expectation = XCTestExpectation() |
| 161 | + let promise = Promise<Void>() |
| 162 | + promise.cancelled({ XCTFail("unepected onCancel") }, on: DispatchQueue.main) |
| 163 | + |
| 164 | + DispatchQueue.main.async { |
| 165 | + promise.fulfill(with: .failure(NSError())) |
| 166 | + let token = promise.then({ |
| 167 | + XCTFail("Cancelation should prevent calling of done block") |
| 168 | + }, observed: { |
| 169 | + XCTFail("Cancelation should prevent calling of observed block") |
| 170 | + }, rejected: { (error) in |
| 171 | + XCTFail("Cancelation should prevent calling of rejected block") |
| 172 | + }, always: { |
| 173 | + XCTFail("Cancelation should prevent calling of finally block") |
| 174 | + }, queue: DispatchQueue.main) |
| 175 | + token.cancel() |
| 176 | + } |
| 177 | + |
| 178 | + DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) { |
| 179 | + expectation.fulfill() |
| 180 | + } |
| 181 | + self.wait(for: [expectation], timeout: 0.5) |
| 182 | + |
| 183 | + } |
| 184 | + |
| 185 | + func testCancelThen() { |
| 186 | + let expectation1 = XCTestExpectation() |
| 187 | + let thenPromise = Promise<Void>() |
| 188 | + let fulfilledPromise = Promise<Void>() |
| 189 | + fulfilledPromise.fulfill(with: .success) |
| 190 | + let cancelToken = fulfilledPromise.then({ () -> PromiseResult<Void> in |
| 191 | + expectation1.fulfill() |
| 192 | + return .promise(thenPromise) |
| 193 | + }, queue: DispatchQueue.main) |
| 194 | + self.wait(for: [expectation1], timeout: 0.5) |
| 195 | + |
| 196 | + let expectation2 = XCTestExpectation() |
| 197 | + thenPromise.cancelled({ |
| 198 | + expectation2.fulfill() |
| 199 | + }, on: DispatchQueue.main) |
| 200 | + |
| 201 | + cancelToken.cancel() |
| 202 | + self.wait(for: [expectation2], timeout: 0.5) |
| 203 | + } |
| 204 | + |
| 205 | + func testCancelThenRaceCondition() { |
| 206 | + let expectation = XCTestExpectation() |
| 207 | + let thenPromise = Promise<Void>() |
| 208 | + thenPromise.cancelled({ |
| 209 | + expectation.fulfill() |
| 210 | + }, on: DispatchQueue.main) |
| 211 | + |
| 212 | + let fulfilledPromise = Promise<Void>() |
| 213 | + fulfilledPromise.fulfill(with: .success) |
| 214 | + |
| 215 | + let cancelToken = fulfilledPromise.then({ () -> PromiseResult<Void> in |
| 216 | + return .promise(thenPromise) |
| 217 | + }, queue: DispatchQueue.main) |
| 218 | + |
| 219 | + cancelToken.cancel() |
| 220 | + self.wait(for: [expectation], timeout: 0.5) |
| 221 | + } |
| 222 | +} |
0 commit comments