|
| 1 | +import XCTest |
| 2 | +@testable import AlgoliaSearchClient |
| 3 | + |
| 4 | +final class AsyncOperationTests: XCTestCase { |
| 5 | + |
| 6 | + class TestOperation: AsyncOperation { |
| 7 | + override func main() { |
| 8 | + work() |
| 9 | + } |
| 10 | + |
| 11 | + private func work() { |
| 12 | + DispatchQueue.global().asyncAfter(deadline: .now() + DispatchTimeInterval.milliseconds(200)) { |
| 13 | + self.state = .finished |
| 14 | + } |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + func testCancellation_withoutQueue() { |
| 19 | + let operation = TestOperation() |
| 20 | + XCTAssertTrue(operation.isReady) |
| 21 | + operation.cancel() |
| 22 | + XCTAssertTrue(operation.isCancelled) |
| 23 | + operation.start() |
| 24 | + XCTAssertTrue(operation.isFinished) |
| 25 | + } |
| 26 | + |
| 27 | + func testCancellation_updatesIsCancelled_whenCancelledBeforeEnqueuing() { |
| 28 | + let operation = TestOperation() |
| 29 | + let queue = OperationQueue() |
| 30 | + operation.cancel() |
| 31 | + queue.addOperation(operation) |
| 32 | + queue.waitUntilAllOperationsAreFinished() |
| 33 | + XCTAssertTrue(operation.isCancelled) |
| 34 | + XCTAssertTrue(operation.isFinished) |
| 35 | + } |
| 36 | + |
| 37 | + func testCancellation_updatesIsCancelled_whenCancelledAfterEnqueuing() { |
| 38 | + let operation = TestOperation() |
| 39 | + let queue = OperationQueue() |
| 40 | + queue.addOperation(operation) |
| 41 | + operation.cancel() |
| 42 | + queue.waitUntilAllOperationsAreFinished() |
| 43 | + XCTAssertTrue(operation.isCancelled) |
| 44 | + XCTAssertTrue(operation.isFinished) |
| 45 | + } |
| 46 | + |
| 47 | + func testCancellation_updatesIsCancelled_whenOperationIsRunning() { |
| 48 | + let queue = OperationQueue() |
| 49 | + let operation = TestOperation() |
| 50 | + queue.addOperation(operation) |
| 51 | + Thread.sleep(forTimeInterval: 0.1) |
| 52 | + XCTAssertTrue(operation.isExecuting) |
| 53 | + operation.cancel() |
| 54 | + XCTAssertFalse(operation.isFinished) |
| 55 | + queue.waitUntilAllOperationsAreFinished() |
| 56 | + XCTAssertTrue(operation.isCancelled) |
| 57 | + XCTAssertTrue(operation.isFinished) |
| 58 | + } |
| 59 | + |
| 60 | + func testCancellation_ignoresCancellation_whenOperationIsFinished() { |
| 61 | + let queue = OperationQueue() |
| 62 | + let operation = TestOperation() |
| 63 | + queue.addOperation(operation) |
| 64 | + queue.waitUntilAllOperationsAreFinished() |
| 65 | + XCTAssertTrue(operation.isFinished) |
| 66 | + operation.cancel() |
| 67 | + XCTAssertFalse(operation.isCancelled) |
| 68 | + } |
| 69 | + |
| 70 | + // Behavior of standard Operation without subclassing. |
| 71 | + func testCancellation_ofBlockOperation_withoutQueue() { |
| 72 | + let operation = BlockOperation { |
| 73 | + Thread.sleep(forTimeInterval: 0.1) |
| 74 | + } |
| 75 | + XCTAssertTrue(operation.isReady) |
| 76 | + operation.cancel() |
| 77 | + XCTAssertTrue(operation.isCancelled) |
| 78 | + operation.start() |
| 79 | + XCTAssertTrue(operation.isFinished) |
| 80 | + } |
| 81 | + |
| 82 | + func testCancellation_ofBlockOperation_updatesIsCancelled_whenCancelledBeforeEnqueuing() { |
| 83 | + let operation = BlockOperation { |
| 84 | + Thread.sleep(forTimeInterval: 0.1) |
| 85 | + } |
| 86 | + let queue = OperationQueue() |
| 87 | + operation.cancel() |
| 88 | + queue.addOperation(operation) |
| 89 | + queue.waitUntilAllOperationsAreFinished() |
| 90 | + XCTAssertTrue(operation.isCancelled) |
| 91 | + XCTAssertTrue(operation.isFinished) |
| 92 | + } |
| 93 | + |
| 94 | + func testCancellation_ofBlockOperation_updatesIsCancelled_whenCancelledAfterEnqueuing() { |
| 95 | + let operation = BlockOperation { |
| 96 | + Thread.sleep(forTimeInterval: 0.1) |
| 97 | + } |
| 98 | + let queue = OperationQueue() |
| 99 | + queue.addOperation(operation) |
| 100 | + operation.cancel() |
| 101 | + queue.waitUntilAllOperationsAreFinished() |
| 102 | + XCTAssertTrue(operation.isCancelled) |
| 103 | + XCTAssertTrue(operation.isFinished) |
| 104 | + } |
| 105 | + |
| 106 | + func testCancellation_ofBlockOperation_updatesIsCancelled_whenOperationIsRunning() { |
| 107 | + let queue = OperationQueue() |
| 108 | + let operation = BlockOperation { |
| 109 | + Thread.sleep(forTimeInterval: 0.2) |
| 110 | + } |
| 111 | + queue.addOperation(operation) |
| 112 | + Thread.sleep(forTimeInterval: 0.1) |
| 113 | + XCTAssertTrue(operation.isExecuting) |
| 114 | + operation.cancel() |
| 115 | + XCTAssertFalse(operation.isFinished) |
| 116 | + queue.waitUntilAllOperationsAreFinished() |
| 117 | + XCTAssertTrue(operation.isCancelled) |
| 118 | + XCTAssertTrue(operation.isFinished) |
| 119 | + } |
| 120 | + |
| 121 | + func testCancellation_ofBlockOperation_ignoresCancellation_whenOperationIsFinished() { |
| 122 | + let queue = OperationQueue() |
| 123 | + let operation = BlockOperation { |
| 124 | + Thread.sleep(forTimeInterval: 0.1) |
| 125 | + } |
| 126 | + queue.addOperation(operation) |
| 127 | + queue.waitUntilAllOperationsAreFinished() |
| 128 | + XCTAssertTrue(operation.isFinished) |
| 129 | + operation.cancel() |
| 130 | + XCTAssertFalse(operation.isCancelled) |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments