Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions Tests/AsyncQueueTests/FIFOQueueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,15 @@ struct FIFOQueueTests {
func task_executesAfterQueueIsDeallocated() async throws {
var systemUnderTest: FIFOQueue? = FIFOQueue()
let counter = Counter()
let expectation = Expectation()
let semaphore = Semaphore()
try Task(on: #require(systemUnderTest)) {
// Make the queue wait.
await semaphore.wait()
await counter.incrementAndExpectCount(equals: 1)
}
try Task(on: #require(systemUnderTest)) {
let lastTask = try Task(on: #require(systemUnderTest)) {
// This async task should not execute until the semaphore is released.
await counter.incrementAndExpectCount(equals: 2)
expectation.fulfill()
}
weak var queue = systemUnderTest
// Nil out our reference to the queue to show that the enqueued tasks will still complete
Expand All @@ -284,24 +282,23 @@ struct FIFOQueueTests {
// Signal the semaphore to unlock the remaining enqueued tasks.
await semaphore.signal()

await expectation.fulfillment(withinSeconds: 30)
// Await the last enqueued task to deterministically confirm the enqueued work ran after the queue was deallocated.
await lastTask.value
}

@Test
func taskIsolatedTo_executesAfterQueueIsDeallocated() async throws {
var systemUnderTest: FIFOQueue? = FIFOQueue()
let counter = Counter()
let expectation = Expectation()
let semaphore = Semaphore()
try Task(on: #require(systemUnderTest), isolatedTo: counter) { counter in
// Make the queue wait.
await semaphore.wait()
counter.incrementAndExpectCount(equals: 1)
}
try Task(on: #require(systemUnderTest), isolatedTo: counter) { counter in
let lastTask = try Task(on: #require(systemUnderTest), isolatedTo: counter) { counter in
// This async task should not execute until the semaphore is released.
counter.incrementAndExpectCount(equals: 2)
expectation.fulfill()
}
weak var queue = systemUnderTest
// Nil out our reference to the queue to show that the enqueued tasks will still complete
Expand All @@ -310,25 +307,24 @@ struct FIFOQueueTests {
// Signal the semaphore to unlock the remaining enqueued tasks.
await semaphore.signal()

await expectation.fulfillment(withinSeconds: 30)
// Await the last enqueued task to deterministically confirm the enqueued work ran after the queue was deallocated.
await lastTask.value
}

@Test
func throwingTask_executesAfterQueueIsDeallocated() async throws {
var systemUnderTest: FIFOQueue? = FIFOQueue()
let counter = Counter()
let expectation = Expectation()
let semaphore = Semaphore()
try Task(on: #require(systemUnderTest)) {
// Make the queue wait.
await semaphore.wait()
await counter.incrementAndExpectCount(equals: 1)
try doWork()
}
try Task(on: #require(systemUnderTest)) {
let lastTask = try Task(on: #require(systemUnderTest)) {
// This async task should not execute until the semaphore is released.
await counter.incrementAndExpectCount(equals: 2)
expectation.fulfill()
try doWork()
}
weak var queue = systemUnderTest
Expand All @@ -338,25 +334,24 @@ struct FIFOQueueTests {
// Signal the semaphore to unlock the remaining enqueued tasks.
await semaphore.signal()

await expectation.fulfillment(withinSeconds: 30)
// Await the last enqueued task to deterministically confirm the enqueued work ran after the queue was deallocated.
try await lastTask.value
}

@Test
func throwingTaskIsolatedTo_executesAfterQueueIsDeallocated() async throws {
var systemUnderTest: FIFOQueue? = FIFOQueue()
let counter = Counter()
let expectation = Expectation()
let semaphore = Semaphore()
try Task(on: #require(systemUnderTest), isolatedTo: counter) { counter in
// Make the queue wait.
await semaphore.wait()
counter.incrementAndExpectCount(equals: 1)
try doWork()
}
try Task(on: #require(systemUnderTest), isolatedTo: counter) { counter in
let lastTask = try Task(on: #require(systemUnderTest), isolatedTo: counter) { counter in
// This async task should not execute until the semaphore is released.
counter.incrementAndExpectCount(equals: 2)
expectation.fulfill()
try doWork()
}
weak var queue = systemUnderTest
Expand All @@ -366,7 +361,8 @@ struct FIFOQueueTests {
// Signal the semaphore to unlock the remaining enqueued tasks.
await semaphore.signal()

await expectation.fulfillment(withinSeconds: 30)
// Await the last enqueued task to deterministically confirm the enqueued work ran after the queue was deallocated.
try await lastTask.value
}

@Test
Expand Down
Loading