Repository: apple/swift-nio
File: Tests/NIOEmbeddedTests/AsyncTestingEventLoopTests.swift (Swift)

Code before:
                init(_ deallocated: ConditionLock<Int>) {
                    self.deallocated = deallocated
                }

                deinit {
                    self.deallocated.lock()
                    self.deallocated.unlock(withValue: 1)
                }
            }

            func make(deallocated: ConditionLock<Int>) -> Scheduled<Never> {
                let aThing = Thing(deallocated)
                return eventLoop.next().scheduleTask(in: .hours(1)) {
                    preconditionFailure("this should definitely not run: \(aThing)")
                }
            }

            let deallocated = ConditionLock(value: 0)
            let scheduled = make(deallocated: deallocated)
            scheduled.cancel()
            if deallocated.lock(whenValue: 1, timeoutSeconds: 60) {
                deallocated.unlock()
            } else {
                XCTFail("Timed out waiting for lock")
            }
            XCTAssertThrowsError(try scheduled.futureResult.wait()) { error in
                XCTAssertEqual(EventLoopError.cancelled, error as? EventLoopError)
            }
        }
    }

    func testDrainScheduledTasks() throws {
        guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() }
        XCTAsyncTest {
            let eventLoop = NIOAsyncTestingEventLoop()
            let tasksRun = ManagedAtomic(0)
            let startTime = eventLoop.now

            eventLoop.scheduleTask(in: .nanoseconds(3141592)) {
                XCTAssertEqual(eventLoop.now, startTime + .nanoseconds(3141592))
                tasksRun.wrappingIncrement(ordering: .relaxed)
            }

            eventLoop.scheduleTask(in: .seconds(3141592)) {
                XCTAssertEqual(eventLoop.now, startTime + .seconds(3141592))
                tasksRun.wrappingIncrement(ordering: .relaxed)
            }

            await eventLoop.shutdownGracefully()
            XCTAssertEqual(tasksRun.load(ordering: .relaxed), 2)
        }

Review comment (suggestion):
Prefer the original `await` construction we used here.

Code after:
                init(_ deallocated: ConditionLock<Int>) {
                    self.deallocated = deallocated
                }

                deinit {
                    self.deallocated.lock()
                    self.deallocated.unlock(withValue: 1)
                }
            }

            func make(deallocated: ConditionLock<Int>) -> Scheduled<Never> {
                let aThing = Thing(deallocated)
                return eventLoop.next().scheduleTask(in: .hours(1)) {
                    preconditionFailure("this should definitely not run: \(aThing)")
                }
            }

            let deallocated = ConditionLock(value: 0)
            let scheduled = make(deallocated: deallocated)
            scheduled.cancel()
            if deallocated.lock(whenValue: 1, timeoutSeconds: 60) {
                deallocated.unlock()
            } else {
                XCTFail("Timed out waiting for lock")
            }
            await XCTAssertThrowsError(try await scheduled.futureResult.get()) { error in
                XCTAssertEqual(EventLoopError.cancelled, error as? EventLoopError)
            }
        }
    }

    func testDrainScheduledTasks() throws {
        guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { throw XCTSkip() }
        XCTAsyncTest {
            let eventLoop = NIOAsyncTestingEventLoop()
            let tasksRun = ManagedAtomic(0)
            let startTime = eventLoop.now

            eventLoop.scheduleTask(in: .nanoseconds(3141592)) {
                XCTAssertEqual(eventLoop.now, startTime + .nanoseconds(3141592))
                tasksRun.wrappingIncrement(ordering: .relaxed)
            }

            eventLoop.scheduleTask(in: .seconds(3141592)) {
                XCTAssertEqual(eventLoop.now, startTime + .seconds(3141592))
                tasksRun.wrappingIncrement(ordering: .relaxed)
            }

            await eventLoop.shutdownGracefully()
            XCTAssertEqual(tasksRun.load(ordering: .relaxed), 2)
        }