Files
swift-nio/Tests/NIOCoreTests/AsyncChannel/AsyncChannelOutboundWriterTests.swift
T
Franz Busch fd3076131f Add testing interfaces for the stream and writer of the NIOAsyncChannel (#2488)
# Motivation
When using the `NIOAsyncChannel` to build higher level abstractions it is hard to test right now since one cannot drive the `NIOAsyncChannelInputStream` nor the `NIOAsyncChannelOutputWriter`.

# Modification
This PR adds new factory methods to both of those types which allow to either drive the input stream or record the outputs written to the output writer.

# Result
One can now successfully test their code written on top of the input stream and output writer.
2023-07-31 03:29:41 -07:00

41 lines
1.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 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
//
//===----------------------------------------------------------------------===//
@_spi(AsyncChannel) @testable import NIOCore
import XCTest
final class AsyncChannelOutboundWriterTests: XCTestCase {
func testTestingWriter() async throws {
let (writer, sink) = NIOAsyncChannelOutboundWriter<Int>.makeTestingWriter()
try await withThrowingTaskGroup(of: [Int].self) { group in
group.addTask {
var elements = [Int]()
for try await element in sink {
elements.append(element)
}
return elements
}
for element in 0...10 {
try await writer.write(element)
}
writer.finish()
let result = try await group.next()
XCTAssertEqual(result, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
}
}
}