|
| 1 | +// MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2023 Dan Federman |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +import XCTest |
| 24 | + |
| 25 | +@testable import AsyncQueue |
| 26 | + |
| 27 | +final class MainActorQueueTests: XCTestCase { |
| 28 | + |
| 29 | + // MARK: XCTestCase |
| 30 | + |
| 31 | + override func setUp() async throws { |
| 32 | + try await super.setUp() |
| 33 | + |
| 34 | + systemUnderTest = MainActorQueue() |
| 35 | + counter = Counter() |
| 36 | + } |
| 37 | + |
| 38 | + // MARK: Behavior Tests |
| 39 | + |
| 40 | + func test_shared_returnsSameInstance() async { |
| 41 | + XCTAssertTrue(MainActorQueue.shared === MainActorQueue.shared) |
| 42 | + } |
| 43 | + |
| 44 | + func test_enqueue_executesOnMainThread() async { |
| 45 | + systemUnderTest.enqueue { |
| 46 | + XCTAssertTrue(Thread.isMainThread) |
| 47 | + } |
| 48 | + await systemUnderTest.enqueueAndWait { /* Drain the queue */ } |
| 49 | + } |
| 50 | + |
| 51 | + func test_enqueue_sendsEventsInOrder() async { |
| 52 | + for iteration in 1...1_000 { |
| 53 | + systemUnderTest.enqueue { [counter] in |
| 54 | + await counter.incrementAndExpectCount(equals: iteration) |
| 55 | + } |
| 56 | + } |
| 57 | + await systemUnderTest.enqueueAndWait { /* Drain the queue */ } |
| 58 | + } |
| 59 | + |
| 60 | + func test_enqueue_startsExecutionOfNextTaskAfterSuspension() async { |
| 61 | + let semaphore = Semaphore() |
| 62 | + |
| 63 | + systemUnderTest.enqueue { |
| 64 | + await semaphore.wait() |
| 65 | + } |
| 66 | + systemUnderTest.enqueue { |
| 67 | + // Signal the semaphore from the actor queue. |
| 68 | + // If the actor queue were FIFO, this test would hang since this code would never execute: |
| 69 | + // we'd still be waiting for the prior `wait()` tasks to finish. |
| 70 | + await semaphore.signal() |
| 71 | + } |
| 72 | + await systemUnderTest.enqueueAndWait { /* Drain the queue */ } |
| 73 | + } |
| 74 | + |
| 75 | + func test_enqueueAndWait_executesOnMainThread() async { |
| 76 | + await systemUnderTest.enqueueAndWait { |
| 77 | + XCTAssertTrue(Thread.isMainThread) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + func test_enqueueAndWait_allowsReentrancy() async { |
| 82 | + await systemUnderTest.enqueueAndWait { [systemUnderTest, counter] in |
| 83 | + await systemUnderTest.enqueueAndWait { [counter] in |
| 84 | + await counter.incrementAndExpectCount(equals: 1) |
| 85 | + } |
| 86 | + await counter.incrementAndExpectCount(equals: 2) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + func test_enqueue_doesNotRetainTaskAfterExecution() async { |
| 91 | + final class Reference: Sendable {} |
| 92 | + final class ReferenceHolder: @unchecked Sendable { |
| 93 | + init() { |
| 94 | + reference = Reference() |
| 95 | + weakReference = reference |
| 96 | + } |
| 97 | + private(set) var reference: Reference? |
| 98 | + private(set) weak var weakReference: Reference? |
| 99 | + |
| 100 | + func clearReference() { |
| 101 | + reference = nil |
| 102 | + } |
| 103 | + } |
| 104 | + let referenceHolder = ReferenceHolder() |
| 105 | + let asyncSemaphore = Semaphore() |
| 106 | + let syncSemaphore = Semaphore() |
| 107 | + let systemUnderTest = ActorQueue<Semaphore>() |
| 108 | + systemUnderTest.adoptExecutionContext(of: syncSemaphore) |
| 109 | + |
| 110 | + let expectation = self.expectation(description: #function) |
| 111 | + systemUnderTest.enqueue { [reference = referenceHolder.reference] syncSemaphore in |
| 112 | + // Now that we've started the task and captured the reference, release the synchronous code. |
| 113 | + syncSemaphore.signal() |
| 114 | + // Wait for the synchronous setup to complete and the reference to be nil'd out. |
| 115 | + await asyncSemaphore.wait() |
| 116 | + // Retain the unsafe counter until the task is completed. |
| 117 | + _ = reference |
| 118 | + systemUnderTest.enqueue { _ in |
| 119 | + // Signal that this task has cleaned up. |
| 120 | + // This closure will not execute until the prior closure completes. |
| 121 | + expectation.fulfill() |
| 122 | + } |
| 123 | + } |
| 124 | + // Wait for the asynchronous task to start. |
| 125 | + await syncSemaphore.wait() |
| 126 | + referenceHolder.clearReference() |
| 127 | + XCTAssertNotNil(referenceHolder.weakReference) |
| 128 | + // Allow the enqueued task to complete. |
| 129 | + await asyncSemaphore.signal() |
| 130 | + // Make sure the task has completed. |
| 131 | + await waitForExpectations(timeout: 1.0) |
| 132 | + |
| 133 | + XCTAssertNil(referenceHolder.weakReference) |
| 134 | + } |
| 135 | + |
| 136 | + func test_enqueueAndWait_sendsEventsInOrder() async { |
| 137 | + for iteration in 1...1_000 { |
| 138 | + systemUnderTest.enqueue { [counter] in |
| 139 | + await counter.incrementAndExpectCount(equals: iteration) |
| 140 | + } |
| 141 | + |
| 142 | + guard iteration % 25 == 0 else { |
| 143 | + // Keep sending async events to the queue. |
| 144 | + continue |
| 145 | + } |
| 146 | + |
| 147 | + await systemUnderTest.enqueueAndWait { [counter] in |
| 148 | + let count = await counter.count |
| 149 | + XCTAssertEqual(count, iteration) |
| 150 | + } |
| 151 | + } |
| 152 | + await systemUnderTest.enqueueAndWait { /* Drain the queue */ } |
| 153 | + } |
| 154 | + |
| 155 | + func test_enqueueAndWait_canReturn() async { |
| 156 | + let expectedValue = UUID() |
| 157 | + let returnedValue = await systemUnderTest.enqueueAndWait { expectedValue } |
| 158 | + XCTAssertEqual(expectedValue, returnedValue) |
| 159 | + } |
| 160 | + |
| 161 | + func test_enqueueAndWait_canThrow() async { |
| 162 | + struct TestError: Error, Equatable { |
| 163 | + private let identifier = UUID() |
| 164 | + } |
| 165 | + let expectedError = TestError() |
| 166 | + do { |
| 167 | + try await systemUnderTest.enqueueAndWait { throw expectedError } |
| 168 | + } catch { |
| 169 | + XCTAssertEqual(error as? TestError, expectedError) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // MARK: Private |
| 174 | + |
| 175 | + private var systemUnderTest = MainActorQueue() |
| 176 | + private var counter = Counter() |
| 177 | +} |
0 commit comments