mirror of
https://github.com/apple/swift-nio
synced 2026-11-25 04:47:49 +00:00
Motivation: With our earlier big refactors, NIOCore is now currently strict concurrency clean. Let's lock in the win by adopting the relevant Swift settings and fixing up the tests. Modifications: - Adopt our strict concurrency settings in NIOCore and NIOCoreTests - Clean up the tests Result: One step closer to a world without warnings.
81 lines
2.7 KiB
Swift
81 lines
2.7 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-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 Atomics
|
|
import Dispatch
|
|
import NIOCore
|
|
import NIOEmbedded
|
|
import NIOPosix
|
|
import XCTest
|
|
|
|
enum DispatchQueueTestError: Error {
|
|
case example
|
|
}
|
|
|
|
class DispatchQueueWithFutureTest: XCTestCase {
|
|
func testDispatchQueueAsyncWithFuture() {
|
|
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
|
defer {
|
|
XCTAssertNoThrow(try group.syncShutdownGracefully())
|
|
}
|
|
let eventLoop = group.next()
|
|
let sem = DispatchSemaphore(value: 0)
|
|
let nonBlockingRan = ManagedAtomic(false)
|
|
let futureResult: EventLoopFuture<String> = DispatchQueue.global().asyncWithFuture(eventLoop: eventLoop) {
|
|
() -> String in
|
|
sem.wait() // Block in callback
|
|
return "hello"
|
|
}
|
|
futureResult.whenSuccess { value in
|
|
XCTAssertEqual(value, "hello")
|
|
XCTAssertTrue(nonBlockingRan.load(ordering: .sequentiallyConsistent))
|
|
}
|
|
|
|
let p2 = eventLoop.makePromise(of: Bool.self)
|
|
p2.futureResult.whenSuccess { _ in
|
|
nonBlockingRan.store(true, ordering: .sequentiallyConsistent)
|
|
}
|
|
p2.succeed(true)
|
|
|
|
sem.signal()
|
|
}
|
|
|
|
func testDispatchQueueAsyncWithFutureThrows() {
|
|
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
|
defer {
|
|
XCTAssertNoThrow(try group.syncShutdownGracefully())
|
|
}
|
|
let eventLoop = group.next()
|
|
let sem = DispatchSemaphore(value: 0)
|
|
let nonBlockingRan = ManagedAtomic(false)
|
|
let futureResult: EventLoopFuture<String> = DispatchQueue.global().asyncWithFuture(eventLoop: eventLoop) {
|
|
() -> String in
|
|
sem.wait() // Block in callback
|
|
throw DispatchQueueTestError.example
|
|
}
|
|
futureResult.whenFailure { err in
|
|
XCTAssertEqual(err as! DispatchQueueTestError, DispatchQueueTestError.example)
|
|
XCTAssertTrue(nonBlockingRan.load(ordering: .sequentiallyConsistent))
|
|
}
|
|
|
|
let p2 = eventLoop.makePromise(of: Bool.self)
|
|
p2.futureResult.whenSuccess { _ in
|
|
nonBlockingRan.store(true, ordering: .sequentiallyConsistent)
|
|
}
|
|
p2.succeed(true)
|
|
|
|
sem.signal()
|
|
}
|
|
}
|