mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
Motivation: The syscall abstraction layer tests aren't sendable clean. Modifications: - Factor out the setup/teardown of SAL tests into a SALContext - SALContext a method to run a SAL test with two closues: one on the hooked event loop and another on the calling thread. - The EL bound closure is provided with the EL and locked boxes for the user ot kernel and kernel to user boxes. - The closure on the calling thread is provided with an 'assertions' object which provides a view over the hooked selector. - Note that there are still sendable holes in this code. - Refactored the tests to use these APIs. Results: Safer tests. Co-authored-by: Cory Benfield <lukasa@apple.com>
60 lines
2.3 KiB
Swift
60 lines
2.3 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2021 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import NIOCore
|
|
import XCTest
|
|
|
|
final class SALEventLoopTests: XCTestCase {
|
|
func testSchedulingTaskOnSleepingLoopWakesUpOnce() throws {
|
|
try withSALContext { context in
|
|
try context.runSALOnEventLoopAndWait { thisLoop, _, _ in
|
|
// We're going to execute some tasks on the loop. This will force a single wakeup, as the first task will wedge the loop open.
|
|
// However, we're currently _on_ the loop so the first thing we have to do is give it up.
|
|
let promise = thisLoop.makePromise(of: Void.self)
|
|
|
|
DispatchQueue(label: "background").asyncAfter(deadline: .now() + .milliseconds(100)) {
|
|
let semaphore = DispatchSemaphore(value: 0)
|
|
|
|
thisLoop.execute {
|
|
// Wedge the loop open. This will also _wake_ the loop.
|
|
XCTAssertEqual(semaphore.wait(timeout: .now() + .milliseconds(500)), .success)
|
|
}
|
|
|
|
// Now execute 10 tasks.
|
|
for _ in 0..<10 {
|
|
thisLoop.execute {}
|
|
}
|
|
|
|
// Now enqueue a "last" task.
|
|
thisLoop.execute {
|
|
promise.succeed(())
|
|
}
|
|
|
|
// Now we can unblock the semaphore.
|
|
semaphore.signal()
|
|
}
|
|
|
|
return promise.futureResult
|
|
} syscallAssertions: { assertions in
|
|
try assertions.assertParkedRightNow()
|
|
try assertions.assertWakeup()
|
|
|
|
// We actually need to wait for the inner code to exit, as the optimisation we're testing here will remove a signal that the
|
|
// SAL is actually going to wait for in salWait().
|
|
try assertions.assertParkedRightNow()
|
|
}
|
|
}
|
|
}
|
|
}
|