Move tests of NIOCore types from NIOPosixTests to NIOCoreTests (#2093)
* Move tests of NIOCore types from NIOPosixTests to NIOCoreTests * fixup: LinuxTest Signed-off-by: Si Beaumont <beaumont@apple.com> * fixup: Port some test utils to not use NIOPosix Signed-off-by: Si Beaumont <beaumont@apple.com> * fixup: No swift-system, duh Signed-off-by: Si Beaumont <beaumont@apple.com> * fixup: read-write for temporary file in test helper Signed-off-by: Si Beaumont <beaumont@apple.com> Co-authored-by: Cory Benfield <lukasa@apple.com>
This commit is contained in:
parent
b1b44ca67b
commit
cd11cf8202
|
@ -88,7 +88,7 @@ var targets: [PackageDescription.Target] = [
|
|||
.executableTarget(name: "NIOAsyncAwaitDemo",
|
||||
dependencies: ["NIOPosix", "NIOCore", "NIOHTTP1"]),
|
||||
.testTarget(name: "NIOCoreTests",
|
||||
dependencies: ["NIOCore"]),
|
||||
dependencies: ["NIOCore", "NIOEmbedded", "NIOFoundationCompat"]),
|
||||
.testTarget(name: "NIOEmbeddedTests",
|
||||
dependencies: ["NIOConcurrencyHelpers", "NIOCore", "NIOEmbedded"]),
|
||||
.testTarget(name: "NIOPosixTests",
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2021-2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
|
@ -16,9 +16,9 @@ import XCTest
|
|||
@testable import NIOCore
|
||||
|
||||
class LinuxTest: XCTestCase {
|
||||
func testCoreCountQuota() {
|
||||
func testCoreCountQuota() throws {
|
||||
#if os(Linux) || os(Android)
|
||||
[
|
||||
try [
|
||||
("50000", "100000", 1),
|
||||
("100000", "100000", 1),
|
||||
("100000\n", "100000", 1),
|
||||
|
@ -31,8 +31,8 @@ class LinuxTest: XCTestCase {
|
|||
("100000", "", nil),
|
||||
("100000", "0", nil)
|
||||
].forEach { quota, period, count in
|
||||
withTemporaryFile(content: quota) { (_, quotaPath) -> Void in
|
||||
withTemporaryFile(content: period) { (_, periodPath) -> Void in
|
||||
try withTemporaryFile(content: quota) { (_, quotaPath) -> Void in
|
||||
try withTemporaryFile(content: period) { (_, periodPath) -> Void in
|
||||
XCTAssertEqual(Linux.coreCount(quota: quotaPath, period: periodPath), count)
|
||||
}
|
||||
}
|
||||
|
@ -40,9 +40,9 @@ class LinuxTest: XCTestCase {
|
|||
#endif
|
||||
}
|
||||
|
||||
func testCoreCountCpuset() {
|
||||
func testCoreCountCpuset() throws {
|
||||
#if os(Linux) || os(Android)
|
||||
[
|
||||
try [
|
||||
("0", 1),
|
||||
("0,3", 2),
|
||||
("0-3", 4),
|
||||
|
@ -51,7 +51,7 @@ class LinuxTest: XCTestCase {
|
|||
("0,2-4,6,7,9-11", 9),
|
||||
("", nil)
|
||||
].forEach { cpuset, count in
|
||||
withTemporaryFile(content: cpuset) { (_, path) -> Void in
|
||||
try withTemporaryFile(content: cpuset) { (_, path) -> Void in
|
||||
XCTAssertEqual(Linux.coreCount(cpuset: path), count)
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2019-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2019-2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
||||
// Copyright (c) 2017-2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
|
@ -0,0 +1,78 @@
|
|||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 XCTest
|
||||
import NIOCore
|
||||
|
||||
func assert(_ condition: @autoclosure () -> Bool, within time: TimeAmount, testInterval: TimeAmount? = nil, _ message: String = "condition not satisfied in time", file: StaticString = #file, line: UInt = #line) {
|
||||
let testInterval = testInterval ?? TimeAmount.nanoseconds(time.nanoseconds / 5)
|
||||
let endTime = NIODeadline.now() + time
|
||||
|
||||
repeat {
|
||||
if condition() { return }
|
||||
usleep(UInt32(testInterval.nanoseconds / 1000))
|
||||
} while (NIODeadline.now() < endTime)
|
||||
|
||||
if !condition() {
|
||||
XCTFail(message, file: (file), line: line)
|
||||
}
|
||||
}
|
||||
|
||||
func assertNoThrowWithValue<T>(_ body: @autoclosure () throws -> T, defaultValue: T? = nil, message: String? = nil, file: StaticString = #file, line: UInt = #line) throws -> T {
|
||||
do {
|
||||
return try body()
|
||||
} catch {
|
||||
XCTFail("\(message.map { $0 + ": " } ?? "")unexpected error \(error) thrown", file: (file), line: line)
|
||||
if let defaultValue = defaultValue {
|
||||
return defaultValue
|
||||
} else {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func withTemporaryFile<T>(content: String? = nil, _ body: (NIOCore.NIOFileHandle, String) throws -> T) throws -> T {
|
||||
let temporaryFilePath = "\(temporaryDirectory)/nio_\(UUID())"
|
||||
FileManager.default.createFile(atPath: temporaryFilePath, contents: content?.data(using: .utf8))
|
||||
defer {
|
||||
XCTAssertNoThrow(try FileManager.default.removeItem(atPath: temporaryFilePath))
|
||||
}
|
||||
|
||||
let fileHandle = try NIOFileHandle(path: temporaryFilePath, mode: [.read, .write])
|
||||
defer {
|
||||
XCTAssertNoThrow(try fileHandle.close())
|
||||
}
|
||||
|
||||
return try body(fileHandle, temporaryFilePath)
|
||||
}
|
||||
|
||||
fileprivate var temporaryDirectory: String {
|
||||
get {
|
||||
#if targetEnvironment(simulator)
|
||||
// Simulator temp directories are so long (and contain the user name) that they're not usable
|
||||
// for UNIX Domain Socket paths (which are limited to 103 bytes).
|
||||
return "/tmp"
|
||||
#else
|
||||
#if os(Linux)
|
||||
return "/tmp"
|
||||
#else
|
||||
if #available(macOS 10.12, iOS 10, tvOS 10, watchOS 3, *) {
|
||||
return FileManager.default.temporaryDirectory.path
|
||||
} else {
|
||||
return "/tmp"
|
||||
}
|
||||
#endif // os
|
||||
#endif // targetEnvironment
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue