|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftNIO open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Foundation |
| 16 | +import XCTest |
| 17 | + |
| 18 | +import NIOCore |
| 19 | +import NIOEmbedded |
| 20 | +@testable import NIOExtras |
| 21 | + |
| 22 | +final class SynchronizedFileSinkTests: XCTestCase { |
| 23 | + func testSimpleFileSink() throws { |
| 24 | + try withTemporaryFile { file, path in |
| 25 | + let sink = try NIOWritePCAPHandler.SynchronizedFileSink.fileSinkWritingToFile(path: path, errorHandler: { XCTFail("Caught error \($0)") }) |
| 26 | + |
| 27 | + sink.write(buffer: ByteBuffer(string: "Hello, ")) |
| 28 | + sink.write(buffer: ByteBuffer(string: "world!")) |
| 29 | + try sink.syncClose() |
| 30 | + |
| 31 | + let data = try Data(contentsOf: URL(fileURLWithPath: path)) |
| 32 | + XCTAssertEqual(data, Data(NIOWritePCAPHandler.pcapFileHeader.readableBytesView) + Data("Hello, world!".utf8)) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + func testSimpleFileSinkAsyncShutdown() throws { |
| 37 | + guard #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) else { return } |
| 38 | + XCTAsyncTest { |
| 39 | + try await withTemporaryFile { file, path in |
| 40 | + let sink = try NIOWritePCAPHandler.SynchronizedFileSink.fileSinkWritingToFile(path: path, errorHandler: { XCTFail("Caught error \($0)") }) |
| 41 | + |
| 42 | + sink.write(buffer: ByteBuffer(string: "Hello, ")) |
| 43 | + sink.write(buffer: ByteBuffer(string: "world!")) |
| 44 | + try await sink.close() |
| 45 | + |
| 46 | + let data = try Data(contentsOf: URL(fileURLWithPath: path)) |
| 47 | + XCTAssertEqual(data, Data(NIOWritePCAPHandler.pcapFileHeader.readableBytesView) + Data("Hello, world!".utf8)) |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +fileprivate func withTemporaryFile<T>(content: String? = nil, _ body: (NIOCore.NIOFileHandle, String) throws -> T) throws -> T { |
| 54 | + let temporaryFilePath = "\(temporaryDirectory)/nio_extras_\(UUID())" |
| 55 | + FileManager.default.createFile(atPath: temporaryFilePath, contents: content?.data(using: .utf8)) |
| 56 | + defer { |
| 57 | + XCTAssertNoThrow(try FileManager.default.removeItem(atPath: temporaryFilePath)) |
| 58 | + } |
| 59 | + |
| 60 | + let fileHandle = try NIOFileHandle(path: temporaryFilePath, mode: [.read, .write]) |
| 61 | + defer { |
| 62 | + XCTAssertNoThrow(try fileHandle.close()) |
| 63 | + } |
| 64 | + |
| 65 | + return try body(fileHandle, temporaryFilePath) |
| 66 | +} |
| 67 | + |
| 68 | +fileprivate func withTemporaryFile<T>(content: String? = nil, _ body: (NIOCore.NIOFileHandle, String) async throws -> T) async throws -> T { |
| 69 | + let temporaryFilePath = "\(temporaryDirectory)/nio_extras_\(UUID())" |
| 70 | + FileManager.default.createFile(atPath: temporaryFilePath, contents: content?.data(using: .utf8)) |
| 71 | + defer { |
| 72 | + XCTAssertNoThrow(try FileManager.default.removeItem(atPath: temporaryFilePath)) |
| 73 | + } |
| 74 | + |
| 75 | + let fileHandle = try NIOFileHandle(path: temporaryFilePath, mode: [.read, .write]) |
| 76 | + defer { |
| 77 | + XCTAssertNoThrow(try fileHandle.close()) |
| 78 | + } |
| 79 | + |
| 80 | + return try await body(fileHandle, temporaryFilePath) |
| 81 | +} |
| 82 | + |
| 83 | +fileprivate var temporaryDirectory: String { |
| 84 | +#if os(Linux) |
| 85 | + return "/tmp" |
| 86 | +#else |
| 87 | + if #available(macOS 10.12, iOS 10, tvOS 10, watchOS 3, *) { |
| 88 | + return FileManager.default.temporaryDirectory.path |
| 89 | + } else { |
| 90 | + return "/tmp" |
| 91 | + } |
| 92 | +#endif // os |
| 93 | +} |
| 94 | + |
| 95 | +extension XCTestCase { |
| 96 | + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) |
| 97 | + /// Cross-platform XCTest support for async-await tests. |
| 98 | + /// |
| 99 | + /// Currently the Linux implementation of XCTest doesn't have async-await support. |
| 100 | + /// Until it does, we make use of this shim which uses a detached `Task` along with |
| 101 | + /// `XCTest.wait(for:timeout:)` to wrap the operation. |
| 102 | + /// |
| 103 | + /// - NOTE: Support for Linux is tracked by https://bugs.swift.org/browse/SR-14403. |
| 104 | + /// - NOTE: Implementation currently in progress: https://github.com/apple/swift-corelibs-xctest/pull/326 |
| 105 | + func XCTAsyncTest( |
| 106 | + expectationDescription: String = "Async operation", |
| 107 | + timeout: TimeInterval = 30, |
| 108 | + file: StaticString = #filePath, |
| 109 | + line: UInt = #line, |
| 110 | + function: StaticString = #function, |
| 111 | + operation: @escaping @Sendable () async throws -> Void |
| 112 | + ) { |
| 113 | + let expectation = self.expectation(description: expectationDescription) |
| 114 | + Task { |
| 115 | + do { |
| 116 | + try await operation() |
| 117 | + } catch { |
| 118 | + XCTFail("Error thrown while executing \(function): \(error)", file: file, line: line) |
| 119 | + Thread.callStackSymbols.forEach { print($0) } |
| 120 | + } |
| 121 | + expectation.fulfill() |
| 122 | + } |
| 123 | + self.wait(for: [expectation], timeout: timeout) |
| 124 | + } |
| 125 | +} |
0 commit comments