diff --git a/Package.swift b/Package.swift index 30118fe20..76f270431 100644 --- a/Package.swift +++ b/Package.swift @@ -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", diff --git a/Tests/NIOPosixTests/BaseObjectsTest+XCTest.swift b/Tests/NIOCoreTests/BaseObjectsTest+XCTest.swift similarity index 95% rename from Tests/NIOPosixTests/BaseObjectsTest+XCTest.swift rename to Tests/NIOCoreTests/BaseObjectsTest+XCTest.swift index 60d67c6cd..d03965f2f 100644 --- a/Tests/NIOPosixTests/BaseObjectsTest+XCTest.swift +++ b/Tests/NIOCoreTests/BaseObjectsTest+XCTest.swift @@ -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 diff --git a/Tests/NIOPosixTests/BaseObjectsTest.swift b/Tests/NIOCoreTests/BaseObjectsTest.swift similarity index 100% rename from Tests/NIOPosixTests/BaseObjectsTest.swift rename to Tests/NIOCoreTests/BaseObjectsTest.swift diff --git a/Tests/NIOPosixTests/ByteBufferLengthPrefixTests+XCTest.swift b/Tests/NIOCoreTests/ByteBufferLengthPrefixTests+XCTest.swift similarity index 97% rename from Tests/NIOPosixTests/ByteBufferLengthPrefixTests+XCTest.swift rename to Tests/NIOCoreTests/ByteBufferLengthPrefixTests+XCTest.swift index bcbfb04bb..e81f091b0 100644 --- a/Tests/NIOPosixTests/ByteBufferLengthPrefixTests+XCTest.swift +++ b/Tests/NIOCoreTests/ByteBufferLengthPrefixTests+XCTest.swift @@ -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 diff --git a/Tests/NIOPosixTests/ByteBufferLengthPrefixTests.swift b/Tests/NIOCoreTests/ByteBufferLengthPrefixTests.swift similarity index 100% rename from Tests/NIOPosixTests/ByteBufferLengthPrefixTests.swift rename to Tests/NIOCoreTests/ByteBufferLengthPrefixTests.swift diff --git a/Tests/NIOPosixTests/ByteBufferTest+XCTest.swift b/Tests/NIOCoreTests/ByteBufferTest+XCTest.swift similarity index 100% rename from Tests/NIOPosixTests/ByteBufferTest+XCTest.swift rename to Tests/NIOCoreTests/ByteBufferTest+XCTest.swift diff --git a/Tests/NIOPosixTests/ByteBufferTest.swift b/Tests/NIOCoreTests/ByteBufferTest.swift similarity index 100% rename from Tests/NIOPosixTests/ByteBufferTest.swift rename to Tests/NIOCoreTests/ByteBufferTest.swift diff --git a/Tests/NIOPosixTests/ChannelOptionStorageTest+XCTest.swift b/Tests/NIOCoreTests/ChannelOptionStorageTest+XCTest.swift similarity index 95% rename from Tests/NIOPosixTests/ChannelOptionStorageTest+XCTest.swift rename to Tests/NIOCoreTests/ChannelOptionStorageTest+XCTest.swift index 5d78a9237..c357c8df3 100644 --- a/Tests/NIOPosixTests/ChannelOptionStorageTest+XCTest.swift +++ b/Tests/NIOCoreTests/ChannelOptionStorageTest+XCTest.swift @@ -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 diff --git a/Tests/NIOPosixTests/ChannelOptionStorageTest.swift b/Tests/NIOCoreTests/ChannelOptionStorageTest.swift similarity index 100% rename from Tests/NIOPosixTests/ChannelOptionStorageTest.swift rename to Tests/NIOCoreTests/ChannelOptionStorageTest.swift diff --git a/Tests/NIOPosixTests/CircularBufferTests+XCTest.swift b/Tests/NIOCoreTests/CircularBufferTests+XCTest.swift similarity index 100% rename from Tests/NIOPosixTests/CircularBufferTests+XCTest.swift rename to Tests/NIOCoreTests/CircularBufferTests+XCTest.swift diff --git a/Tests/NIOPosixTests/CircularBufferTests.swift b/Tests/NIOCoreTests/CircularBufferTests.swift similarity index 100% rename from Tests/NIOPosixTests/CircularBufferTests.swift rename to Tests/NIOCoreTests/CircularBufferTests.swift diff --git a/Tests/NIOPosixTests/CustomChannelTests+XCTest.swift b/Tests/NIOCoreTests/CustomChannelTests+XCTest.swift similarity index 94% rename from Tests/NIOPosixTests/CustomChannelTests+XCTest.swift rename to Tests/NIOCoreTests/CustomChannelTests+XCTest.swift index 132678b80..70c014e63 100644 --- a/Tests/NIOPosixTests/CustomChannelTests+XCTest.swift +++ b/Tests/NIOCoreTests/CustomChannelTests+XCTest.swift @@ -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 diff --git a/Tests/NIOPosixTests/CustomChannelTests.swift b/Tests/NIOCoreTests/CustomChannelTests.swift similarity index 100% rename from Tests/NIOPosixTests/CustomChannelTests.swift rename to Tests/NIOCoreTests/CustomChannelTests.swift diff --git a/Tests/NIOPosixTests/DispatchQueue+WithFutureTest+XCTest.swift b/Tests/NIOCoreTests/DispatchQueue+WithFutureTest+XCTest.swift similarity index 94% rename from Tests/NIOPosixTests/DispatchQueue+WithFutureTest+XCTest.swift rename to Tests/NIOCoreTests/DispatchQueue+WithFutureTest+XCTest.swift index 4d17fdc6a..45e30efb8 100644 --- a/Tests/NIOPosixTests/DispatchQueue+WithFutureTest+XCTest.swift +++ b/Tests/NIOCoreTests/DispatchQueue+WithFutureTest+XCTest.swift @@ -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 diff --git a/Tests/NIOPosixTests/DispatchQueue+WithFutureTest.swift b/Tests/NIOCoreTests/DispatchQueue+WithFutureTest.swift similarity index 100% rename from Tests/NIOPosixTests/DispatchQueue+WithFutureTest.swift rename to Tests/NIOCoreTests/DispatchQueue+WithFutureTest.swift diff --git a/Tests/NIOPosixTests/IOErrorTest+XCTest.swift b/Tests/NIOCoreTests/IOErrorTest+XCTest.swift similarity index 94% rename from Tests/NIOPosixTests/IOErrorTest+XCTest.swift rename to Tests/NIOCoreTests/IOErrorTest+XCTest.swift index bcebbe764..e0423e134 100644 --- a/Tests/NIOPosixTests/IOErrorTest+XCTest.swift +++ b/Tests/NIOCoreTests/IOErrorTest+XCTest.swift @@ -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 diff --git a/Tests/NIOPosixTests/IOErrorTest.swift b/Tests/NIOCoreTests/IOErrorTest.swift similarity index 100% rename from Tests/NIOPosixTests/IOErrorTest.swift rename to Tests/NIOCoreTests/IOErrorTest.swift diff --git a/Tests/NIOPosixTests/IntegerTypesTest+XCTest.swift b/Tests/NIOCoreTests/IntegerTypesTest+XCTest.swift similarity index 95% rename from Tests/NIOPosixTests/IntegerTypesTest+XCTest.swift rename to Tests/NIOCoreTests/IntegerTypesTest+XCTest.swift index 7a4654fdc..46e576865 100644 --- a/Tests/NIOPosixTests/IntegerTypesTest+XCTest.swift +++ b/Tests/NIOCoreTests/IntegerTypesTest+XCTest.swift @@ -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 diff --git a/Tests/NIOPosixTests/IntegerTypesTest.swift b/Tests/NIOCoreTests/IntegerTypesTest.swift similarity index 100% rename from Tests/NIOPosixTests/IntegerTypesTest.swift rename to Tests/NIOCoreTests/IntegerTypesTest.swift diff --git a/Tests/NIOPosixTests/LinuxTest+XCTest.swift b/Tests/NIOCoreTests/LinuxTest+XCTest.swift similarity index 94% rename from Tests/NIOPosixTests/LinuxTest+XCTest.swift rename to Tests/NIOCoreTests/LinuxTest+XCTest.swift index 864c0b90f..cfc9be1f9 100644 --- a/Tests/NIOPosixTests/LinuxTest+XCTest.swift +++ b/Tests/NIOCoreTests/LinuxTest+XCTest.swift @@ -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 diff --git a/Tests/NIOPosixTests/LinuxTest.swift b/Tests/NIOCoreTests/LinuxTest.swift similarity index 82% rename from Tests/NIOPosixTests/LinuxTest.swift rename to Tests/NIOCoreTests/LinuxTest.swift index 292b8286a..e64bd75c1 100644 --- a/Tests/NIOPosixTests/LinuxTest.swift +++ b/Tests/NIOCoreTests/LinuxTest.swift @@ -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) } } diff --git a/Tests/NIOPosixTests/MarkedCircularBufferTests+XCTest.swift b/Tests/NIOCoreTests/MarkedCircularBufferTests+XCTest.swift similarity index 96% rename from Tests/NIOPosixTests/MarkedCircularBufferTests+XCTest.swift rename to Tests/NIOCoreTests/MarkedCircularBufferTests+XCTest.swift index 56bbb74be..e037c21c5 100644 --- a/Tests/NIOPosixTests/MarkedCircularBufferTests+XCTest.swift +++ b/Tests/NIOCoreTests/MarkedCircularBufferTests+XCTest.swift @@ -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 diff --git a/Tests/NIOPosixTests/MarkedCircularBufferTests.swift b/Tests/NIOCoreTests/MarkedCircularBufferTests.swift similarity index 100% rename from Tests/NIOPosixTests/MarkedCircularBufferTests.swift rename to Tests/NIOCoreTests/MarkedCircularBufferTests.swift diff --git a/Tests/NIOPosixTests/NIOAnyDebugTest+XCTest.swift b/Tests/NIOCoreTests/NIOAnyDebugTest+XCTest.swift similarity index 94% rename from Tests/NIOPosixTests/NIOAnyDebugTest+XCTest.swift rename to Tests/NIOCoreTests/NIOAnyDebugTest+XCTest.swift index 92fcf7763..810274468 100644 --- a/Tests/NIOPosixTests/NIOAnyDebugTest+XCTest.swift +++ b/Tests/NIOCoreTests/NIOAnyDebugTest+XCTest.swift @@ -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 diff --git a/Tests/NIOPosixTests/NIOAnyDebugTest.swift b/Tests/NIOCoreTests/NIOAnyDebugTest.swift similarity index 100% rename from Tests/NIOPosixTests/NIOAnyDebugTest.swift rename to Tests/NIOCoreTests/NIOAnyDebugTest.swift diff --git a/Tests/NIOPosixTests/NIOCloseOnErrorHandlerTest+XCTest.swift b/Tests/NIOCoreTests/NIOCloseOnErrorHandlerTest+XCTest.swift similarity index 94% rename from Tests/NIOPosixTests/NIOCloseOnErrorHandlerTest+XCTest.swift rename to Tests/NIOCoreTests/NIOCloseOnErrorHandlerTest+XCTest.swift index c412ddcf1..0670d9da2 100644 --- a/Tests/NIOPosixTests/NIOCloseOnErrorHandlerTest+XCTest.swift +++ b/Tests/NIOCoreTests/NIOCloseOnErrorHandlerTest+XCTest.swift @@ -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 diff --git a/Tests/NIOPosixTests/NIOCloseOnErrorHandlerTest.swift b/Tests/NIOCoreTests/NIOCloseOnErrorHandlerTest.swift similarity index 100% rename from Tests/NIOPosixTests/NIOCloseOnErrorHandlerTest.swift rename to Tests/NIOCoreTests/NIOCloseOnErrorHandlerTest.swift diff --git a/Tests/NIOPosixTests/RecvByteBufAllocatorTest+XCTest.swift b/Tests/NIOCoreTests/RecvByteBufAllocatorTest+XCTest.swift similarity index 95% rename from Tests/NIOPosixTests/RecvByteBufAllocatorTest+XCTest.swift rename to Tests/NIOCoreTests/RecvByteBufAllocatorTest+XCTest.swift index 687dbab1f..1d1f1e1d5 100644 --- a/Tests/NIOPosixTests/RecvByteBufAllocatorTest+XCTest.swift +++ b/Tests/NIOCoreTests/RecvByteBufAllocatorTest+XCTest.swift @@ -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 diff --git a/Tests/NIOPosixTests/RecvByteBufAllocatorTest.swift b/Tests/NIOCoreTests/RecvByteBufAllocatorTest.swift similarity index 100% rename from Tests/NIOPosixTests/RecvByteBufAllocatorTest.swift rename to Tests/NIOCoreTests/RecvByteBufAllocatorTest.swift diff --git a/Tests/NIOPosixTests/SingleStepByteToMessageDecoderTest+XCTest.swift b/Tests/NIOCoreTests/SingleStepByteToMessageDecoderTest+XCTest.swift similarity index 96% rename from Tests/NIOPosixTests/SingleStepByteToMessageDecoderTest+XCTest.swift rename to Tests/NIOCoreTests/SingleStepByteToMessageDecoderTest+XCTest.swift index a173e15e5..9bddad8bf 100644 --- a/Tests/NIOPosixTests/SingleStepByteToMessageDecoderTest+XCTest.swift +++ b/Tests/NIOCoreTests/SingleStepByteToMessageDecoderTest+XCTest.swift @@ -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 diff --git a/Tests/NIOPosixTests/SingleStepByteToMessageDecoderTest.swift b/Tests/NIOCoreTests/SingleStepByteToMessageDecoderTest.swift similarity index 100% rename from Tests/NIOPosixTests/SingleStepByteToMessageDecoderTest.swift rename to Tests/NIOCoreTests/SingleStepByteToMessageDecoderTest.swift diff --git a/Tests/NIOPosixTests/TimeAmountTests+XCTest.swift b/Tests/NIOCoreTests/TimeAmountTests+XCTest.swift similarity index 95% rename from Tests/NIOPosixTests/TimeAmountTests+XCTest.swift rename to Tests/NIOCoreTests/TimeAmountTests+XCTest.swift index e87cdc1f9..4b0e43ce3 100644 --- a/Tests/NIOPosixTests/TimeAmountTests+XCTest.swift +++ b/Tests/NIOCoreTests/TimeAmountTests+XCTest.swift @@ -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 diff --git a/Tests/NIOPosixTests/TimeAmountTests.swift b/Tests/NIOCoreTests/TimeAmountTests.swift similarity index 100% rename from Tests/NIOPosixTests/TimeAmountTests.swift rename to Tests/NIOCoreTests/TimeAmountTests.swift diff --git a/Tests/NIOPosixTests/TypeAssistedChannelHandlerTests+XCTest.swift b/Tests/NIOCoreTests/TypeAssistedChannelHandlerTests+XCTest.swift similarity index 94% rename from Tests/NIOPosixTests/TypeAssistedChannelHandlerTests+XCTest.swift rename to Tests/NIOCoreTests/TypeAssistedChannelHandlerTests+XCTest.swift index 436b7e8b9..64be2b36c 100644 --- a/Tests/NIOPosixTests/TypeAssistedChannelHandlerTests+XCTest.swift +++ b/Tests/NIOCoreTests/TypeAssistedChannelHandlerTests+XCTest.swift @@ -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 diff --git a/Tests/NIOPosixTests/TypeAssistedChannelHandlerTests.swift b/Tests/NIOCoreTests/TypeAssistedChannelHandlerTests.swift similarity index 100% rename from Tests/NIOPosixTests/TypeAssistedChannelHandlerTests.swift rename to Tests/NIOCoreTests/TypeAssistedChannelHandlerTests.swift diff --git a/Tests/NIOPosixTests/UtilitiesTest+XCTest.swift b/Tests/NIOCoreTests/UtilitiesTest+XCTest.swift similarity index 94% rename from Tests/NIOPosixTests/UtilitiesTest+XCTest.swift rename to Tests/NIOCoreTests/UtilitiesTest+XCTest.swift index b4ec67fd0..d11f0455a 100644 --- a/Tests/NIOPosixTests/UtilitiesTest+XCTest.swift +++ b/Tests/NIOCoreTests/UtilitiesTest+XCTest.swift @@ -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 diff --git a/Tests/NIOPosixTests/UtilitiesTest.swift b/Tests/NIOCoreTests/UtilitiesTest.swift similarity index 100% rename from Tests/NIOPosixTests/UtilitiesTest.swift rename to Tests/NIOCoreTests/UtilitiesTest.swift diff --git a/Tests/NIOCoreTests/XCTest+Extensions.swift b/Tests/NIOCoreTests/XCTest+Extensions.swift new file mode 100644 index 000000000..94862d29a --- /dev/null +++ b/Tests/NIOCoreTests/XCTest+Extensions.swift @@ -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(_ 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(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 + } +}