|
| 1 | +import XCTest |
| 2 | +@testable import PromiseKit |
| 3 | + |
| 4 | +class ChainDispatcherTests: XCTestCase { |
| 5 | + |
| 6 | + var defaultBodyDispatcher = RecordingDispatcher() |
| 7 | + var defaultTailDispatcher = RecordingDispatcher() |
| 8 | + |
| 9 | + override func setUp() { |
| 10 | + conf.testMode = true // Allow free resetting of defaults without warnings |
| 11 | + defaultBodyDispatcher.dispatchCount = 0 |
| 12 | + defaultTailDispatcher.dispatchCount = 0 |
| 13 | + conf.setDefaultDispatchers(body: defaultBodyDispatcher, tail: defaultTailDispatcher) |
| 14 | + } |
| 15 | + |
| 16 | + override func tearDown() { |
| 17 | + conf.setDefaultDispatchers(body: .default, tail: .default) |
| 18 | + } |
| 19 | + |
| 20 | + func captureLog(_ body: () -> Void) -> String? { |
| 21 | + |
| 22 | + var logOutput: String? = nil |
| 23 | + |
| 24 | + func captureLogger(_ event: LogEvent) { |
| 25 | + logOutput = "\(event)" |
| 26 | + } |
| 27 | + |
| 28 | + let oldLogger = conf.logHandler |
| 29 | + conf.logHandler = captureLogger |
| 30 | + body() |
| 31 | + conf.logHandler = oldLogger |
| 32 | + return logOutput |
| 33 | + } |
| 34 | + |
| 35 | + func testSimpleChain() { |
| 36 | + let ex = expectation(description: "Simple chain") |
| 37 | + Promise.value(42).cancellize().then { |
| 38 | + Promise.value($0 + 10) |
| 39 | + }.map { |
| 40 | + $0 + 20 |
| 41 | + }.get { _ in |
| 42 | + // NOP - should be body dispatcher |
| 43 | + }.done { |
| 44 | + ex.fulfill() |
| 45 | + XCTAssert($0 == 72) |
| 46 | + }.cauterize() |
| 47 | + waitForExpectations(timeout: 1) |
| 48 | + XCTAssert(defaultBodyDispatcher.dispatchCount == 3) |
| 49 | + XCTAssert(defaultTailDispatcher.dispatchCount == 1) |
| 50 | + } |
| 51 | + |
| 52 | + func testPermanentTail() { |
| 53 | + let ex = expectation(description: "Permanent tail") |
| 54 | + Promise.value(42).cancellize().then { |
| 55 | + Promise.value($0 + 10) |
| 56 | + }.map { |
| 57 | + $0 |
| 58 | + }.get { _ in |
| 59 | + // NOP - should be body dispatcher |
| 60 | + }.done { |
| 61 | + XCTAssert($0 == 52) |
| 62 | + }.get { _ in |
| 63 | + // NOP - should be tail dispatcher |
| 64 | + }.map { |
| 65 | + 123 // Tail |
| 66 | + }.catch { error in |
| 67 | + // NOP - not dispatched |
| 68 | + }.finally { |
| 69 | + ex.fulfill() |
| 70 | + } |
| 71 | + waitForExpectations(timeout: 1) |
| 72 | + XCTAssert(defaultBodyDispatcher.dispatchCount == 3) |
| 73 | + XCTAssert(defaultTailDispatcher.dispatchCount == 4) |
| 74 | + } |
| 75 | + |
| 76 | + func testSimpleChainDispatcher() { |
| 77 | + let ex = expectation(description: "Simple chain dispatcher") |
| 78 | + let dispatcher = RecordingDispatcher() |
| 79 | + let log = captureLog { |
| 80 | + Promise.value(42).cancellize().then { |
| 81 | + Promise.value($0 + 10) |
| 82 | + }.map { |
| 83 | + $0 |
| 84 | + }.dispatch(on: dispatcher).get { _ in |
| 85 | + // NOP - should be body dispatcher |
| 86 | + }.done { |
| 87 | + XCTAssert($0 == 52) |
| 88 | + }.get { _ in |
| 89 | + // NOP - should be tail dispatcher |
| 90 | + }.map { |
| 91 | + 123 // Tail |
| 92 | + }.catch { error in |
| 93 | + // NOP - not dispatched |
| 94 | + }.finally { |
| 95 | + ex.fulfill() |
| 96 | + } |
| 97 | + } |
| 98 | + waitForExpectations(timeout: 1) |
| 99 | + XCTAssert(defaultBodyDispatcher.dispatchCount == 2) |
| 100 | + XCTAssert(defaultTailDispatcher.dispatchCount == 0) |
| 101 | + XCTAssert(dispatcher.dispatchCount == 5) |
| 102 | + XCTAssert(log == "failedToConfirmChainDispatcher") |
| 103 | + } |
| 104 | + |
| 105 | + func testRConfirmedChainDispatcher() { |
| 106 | + let ex = expectation(description: "Simple chain dispatcher") |
| 107 | + let dispatcher = RecordingDispatcher() |
| 108 | + let log = captureLog { |
| 109 | + Promise.value(42).cancellize().then { |
| 110 | + Promise.value($0 + 10) |
| 111 | + }.map { |
| 112 | + $0 |
| 113 | + }.dispatch(on: dispatcher).get { _ in |
| 114 | + // NOP - should be body dispatcher |
| 115 | + }.done(on: .chain) { |
| 116 | + XCTAssert($0 == 52) |
| 117 | + }.get { _ in |
| 118 | + // NOP - should be tail dispatcher |
| 119 | + }.map { |
| 120 | + 123 // Tail |
| 121 | + }.catch { error in |
| 122 | + // NOP - not dispatched |
| 123 | + }.finally { |
| 124 | + ex.fulfill() |
| 125 | + } |
| 126 | + } |
| 127 | + waitForExpectations(timeout: 1) |
| 128 | + XCTAssert(defaultBodyDispatcher.dispatchCount == 2) |
| 129 | + XCTAssert(defaultTailDispatcher.dispatchCount == 0) |
| 130 | + XCTAssert(dispatcher.dispatchCount == 5) |
| 131 | + XCTAssert(log == nil) |
| 132 | + } |
| 133 | + |
| 134 | + func testResetChainDispatcher() { |
| 135 | + let ex = expectation(description: "Simple chain dispatcher") |
| 136 | + let dispatcher = RecordingDispatcher() |
| 137 | + let log = captureLog { |
| 138 | + Promise.value(42).cancellize().dispatch(on: dispatcher).then { |
| 139 | + Promise.value($0 + 10) |
| 140 | + }.map { |
| 141 | + $0 |
| 142 | + }.dispatch(on: .default).get { _ in |
| 143 | + // NOP - should be body dispatcher |
| 144 | + }.done { |
| 145 | + XCTAssert($0 == 52) |
| 146 | + }.get { _ in |
| 147 | + // NOP - should be tail dispatcher |
| 148 | + }.map { |
| 149 | + 123 // Tail |
| 150 | + }.catch { error in |
| 151 | + // NOP - not dispatched |
| 152 | + }.finally { |
| 153 | + ex.fulfill() |
| 154 | + } |
| 155 | + } |
| 156 | + waitForExpectations(timeout: 1) |
| 157 | + XCTAssert(defaultBodyDispatcher.dispatchCount == 1) |
| 158 | + XCTAssert(defaultTailDispatcher.dispatchCount == 4) |
| 159 | + XCTAssert(dispatcher.dispatchCount == 2) |
| 160 | + XCTAssert(log == nil) |
| 161 | + } |
| 162 | + |
| 163 | + func testThresholdChainDispatcher() { |
| 164 | + let ex = expectation(description: "Simple chain dispatcher") |
| 165 | + let dispatcher = RecordingDispatcher() |
| 166 | + let log = captureLog { |
| 167 | + Promise.value(42).cancellize().then { |
| 168 | + Promise.value($0 + 10) |
| 169 | + }.map { |
| 170 | + $0 |
| 171 | + }.dispatch(on: dispatcher).done { |
| 172 | + XCTAssert($0 == 52) |
| 173 | + }.get { _ in |
| 174 | + // NOP - should be tail dispatcher |
| 175 | + }.map { |
| 176 | + 123 // Tail |
| 177 | + }.catch { error in |
| 178 | + // NOP - not dispatched |
| 179 | + }.finally { |
| 180 | + ex.fulfill() |
| 181 | + } |
| 182 | + } |
| 183 | + waitForExpectations(timeout: 1) |
| 184 | + XCTAssert(defaultBodyDispatcher.dispatchCount == 2) |
| 185 | + XCTAssert(defaultTailDispatcher.dispatchCount == 0) |
| 186 | + XCTAssert(dispatcher.dispatchCount == 4) |
| 187 | + XCTAssert(log == nil) |
| 188 | + } |
| 189 | + |
| 190 | + func testIndefinitelyDelayedConfirmationChainDispatcher() { |
| 191 | + let ex = expectation(description: "Simple chain dispatcher") |
| 192 | + let dispatcher = RecordingDispatcher() |
| 193 | + let log = captureLog { |
| 194 | + Promise.value(42).cancellize().dispatch(on: dispatcher).then { |
| 195 | + Promise.value($0 + 10) |
| 196 | + }.map { |
| 197 | + $0 |
| 198 | + }.done(on: defaultTailDispatcher) { |
| 199 | + XCTAssert($0 == 52) |
| 200 | + }.map(on: defaultTailDispatcher) { |
| 201 | + 123 // Tail |
| 202 | + }.catch(on: defaultTailDispatcher) { error in |
| 203 | + // NOP - not dispatched |
| 204 | + }.finally(on: defaultTailDispatcher) { |
| 205 | + ex.fulfill() |
| 206 | + } |
| 207 | + } |
| 208 | + waitForExpectations(timeout: 1) |
| 209 | + XCTAssert(defaultBodyDispatcher.dispatchCount == 0) |
| 210 | + XCTAssert(defaultTailDispatcher.dispatchCount == 3) |
| 211 | + XCTAssert(dispatcher.dispatchCount == 2) |
| 212 | + XCTAssert(log == nil) |
| 213 | + } |
| 214 | + |
| 215 | + func testDelayedConfirmationChainDispatcher() { |
| 216 | + let ex = expectation(description: "Simple chain dispatcher") |
| 217 | + let dispatcher = RecordingDispatcher() |
| 218 | + let log = captureLog { |
| 219 | + Promise.value(42).cancellize().dispatch(on: dispatcher).then { |
| 220 | + Promise.value($0 + 10) |
| 221 | + }.map { |
| 222 | + $0 |
| 223 | + }.done(on: defaultTailDispatcher) { |
| 224 | + XCTAssert($0 == 52) |
| 225 | + }.map(on: defaultTailDispatcher) { |
| 226 | + 123 // Tail |
| 227 | + }.catch { error in |
| 228 | + // NOP - not dispatched |
| 229 | + }.finally { |
| 230 | + ex.fulfill() |
| 231 | + } |
| 232 | + } |
| 233 | + waitForExpectations(timeout: 1) |
| 234 | + XCTAssert(defaultBodyDispatcher.dispatchCount == 0) |
| 235 | + XCTAssert(defaultTailDispatcher.dispatchCount == 2) |
| 236 | + XCTAssert(dispatcher.dispatchCount == 3) |
| 237 | + XCTAssert(log == "failedToConfirmChainDispatcher") |
| 238 | + } |
| 239 | + |
| 240 | + func testStickyChainDispatcher() { |
| 241 | + let ex = expectation(description: "Sticky chain dispatcher") |
| 242 | + let dispatcher = RecordingDispatcher() |
| 243 | + let log = captureLog { |
| 244 | + Promise.value(42).cancellize().dispatch(on: .sticky).then { |
| 245 | + Promise.value($0 + 10) |
| 246 | + }.map { |
| 247 | + $0 |
| 248 | + }.done(on: dispatcher) { |
| 249 | + XCTAssert($0 == 52) |
| 250 | + }.map { |
| 251 | + 123 // Tail |
| 252 | + }.catch { error in |
| 253 | + // NOP - not dispatched |
| 254 | + }.finally { |
| 255 | + ex.fulfill() |
| 256 | + } |
| 257 | + } |
| 258 | + waitForExpectations(timeout: 1) |
| 259 | + XCTAssert(defaultBodyDispatcher.dispatchCount == 2) |
| 260 | + XCTAssert(defaultTailDispatcher.dispatchCount == 0) |
| 261 | + XCTAssert(dispatcher.dispatchCount == 3) |
| 262 | + XCTAssert(log == nil) |
| 263 | + } |
| 264 | + |
| 265 | + func testUnconfirmedStickyChainDispatcher() { |
| 266 | + let ex = expectation(description: "Unconfirmed sticky chain dispatcher") |
| 267 | + let dispatcher = RecordingDispatcher() |
| 268 | + let log = captureLog { |
| 269 | + Promise.value(42).cancellize().dispatch(on: .sticky).then(on: dispatcher) { |
| 270 | + Promise.value($0 + 10) |
| 271 | + }.map { |
| 272 | + $0 |
| 273 | + }.done { |
| 274 | + XCTAssert($0 == 52) |
| 275 | + }.map(on: defaultTailDispatcher) { |
| 276 | + 123 // Tail |
| 277 | + }.catch { error in |
| 278 | + // NOP - not dispatched |
| 279 | + }.finally { |
| 280 | + ex.fulfill() |
| 281 | + } |
| 282 | + } |
| 283 | + waitForExpectations(timeout: 1) |
| 284 | + XCTAssert(defaultBodyDispatcher.dispatchCount == 0) |
| 285 | + XCTAssert(defaultTailDispatcher.dispatchCount == 2) |
| 286 | + XCTAssert(dispatcher.dispatchCount == 3) |
| 287 | + XCTAssert(log == "failedToConfirmChainDispatcher") |
| 288 | + } |
| 289 | + |
| 290 | + func testAdHocStickyDispatchers() { |
| 291 | + let ex = expectation(description: "Ad hoc sticky dispatching") |
| 292 | + let dispatcher = RecordingDispatcher() |
| 293 | + let log = captureLog { |
| 294 | + Promise.value(42).cancellize().then(on: dispatcher) { |
| 295 | + Promise.value($0 + 10) |
| 296 | + }.map(on: .sticky) { |
| 297 | + $0 |
| 298 | + }.done(on: .sticky) { |
| 299 | + XCTAssert($0 == 52) |
| 300 | + }.map(on: defaultBodyDispatcher) { |
| 301 | + 123 // Tail |
| 302 | + }.catch(on: .sticky) { error in |
| 303 | + // NOP - not dispatched |
| 304 | + }.finally { |
| 305 | + ex.fulfill() |
| 306 | + } |
| 307 | + } |
| 308 | + waitForExpectations(timeout: 1) |
| 309 | + XCTAssert(defaultBodyDispatcher.dispatchCount == 1) |
| 310 | + XCTAssert(defaultTailDispatcher.dispatchCount == 1) |
| 311 | + XCTAssert(dispatcher.dispatchCount == 3) |
| 312 | + XCTAssert(log == nil) |
| 313 | + } |
| 314 | +} |
| 315 | + |
| 316 | + |
0 commit comments