1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2019 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
@testable import NIO
class EmbeddedChannelTest: XCTestCase {
func testSingleHandlerInit() {
class Handler: ChannelInboundHandler {
typealias InboundIn = Never
}
let channel = EmbeddedChannel(handler: Handler())
XCTAssertNoThrow(try channel.pipeline.handler(type: Handler.self).wait())
}
func testSingleHandlerInitNil() {
class Handler: ChannelInboundHandler {
typealias InboundIn = Never
}
let channel = EmbeddedChannel(handler: nil)
XCTAssertThrowsError(try channel.pipeline.handler(type: Handler.self).wait()) { e in
XCTAssertEqual(e as? ChannelPipelineError, .notFound)
}
}
func testMultipleHandlerInit() {
class Handler: ChannelInboundHandler, RemovableChannelHandler {
typealias InboundIn = Never
let identifier: String
init(identifier: String) {
self.identifier = identifier
}
}
let channel = EmbeddedChannel(
handlers: [Handler(identifier: "0"), Handler(identifier: "1"), Handler(identifier: "2")]
)
XCTAssertNoThrow(XCTAssertEqual(try channel.pipeline.handler(type: Handler.self).wait().identifier, "0"))
XCTAssertNoThrow(try channel.pipeline.removeHandler(name: "handler0").wait())
XCTAssertNoThrow(XCTAssertEqual(try channel.pipeline.handler(type: Handler.self).wait().identifier, "1"))
XCTAssertNoThrow(try channel.pipeline.removeHandler(name: "handler1").wait())
XCTAssertNoThrow(XCTAssertEqual(try channel.pipeline.handler(type: Handler.self).wait().identifier, "2"))
XCTAssertNoThrow(try channel.pipeline.removeHandler(name: "handler2").wait())
}
func testWriteOutboundByteBuffer() throws {
let channel = EmbeddedChannel()
var buf = channel.allocator.buffer(capacity: 1024)
buf.writeString("hello")
XCTAssertTrue(try channel.writeOutbound(buf).isFull)
XCTAssertTrue(try channel.finish().hasLeftOvers)
XCTAssertNoThrow(XCTAssertEqual(buf, try channel.readOutbound()))
XCTAssertNoThrow(XCTAssertNil(try channel.readOutbound()))
XCTAssertNoThrow(XCTAssertNil(try channel.readInbound()))
}
func testWriteOutboundByteBufferMultipleTimes() throws {
let channel = EmbeddedChannel()
var buf = channel.allocator.buffer(capacity: 1024)
buf.writeString("hello")
XCTAssertTrue(try channel.writeOutbound(buf).isFull)
XCTAssertNoThrow(XCTAssertEqual(buf, try channel.readOutbound()))
XCTAssertNoThrow(XCTAssertNil(try channel.readOutbound()))
XCTAssertNoThrow(XCTAssertNil(try channel.readInbound()))
var bufB = channel.allocator.buffer(capacity: 1024)
bufB.writeString("again")
XCTAssertTrue(try channel.writeOutbound(bufB).isFull)
XCTAssertTrue(try channel.finish().hasLeftOvers)
XCTAssertNoThrow(XCTAssertEqual(bufB, try channel.readOutbound()))
XCTAssertNoThrow(XCTAssertNil(try channel.readOutbound()))
XCTAssertNoThrow(XCTAssertNil(try channel.readInbound()))
}
func testWriteInboundByteBuffer() throws {
let channel = EmbeddedChannel()
var buf = channel.allocator.buffer(capacity: 1024)
buf.writeString("hello")
XCTAssertTrue(try channel.writeInbound(buf).isFull)
XCTAssertTrue(try channel.finish().hasLeftOvers)
XCTAssertNoThrow(XCTAssertEqual(buf, try channel.readInbound()))
XCTAssertNoThrow(XCTAssertNil(try channel.readInbound()))
XCTAssertNoThrow(XCTAssertNil(try channel.readOutbound()))
}
func testWriteInboundByteBufferMultipleTimes() throws {
let channel = EmbeddedChannel()
var buf = channel.allocator.buffer(capacity: 1024)
buf.writeString("hello")
XCTAssertTrue(try channel.writeInbound(buf).isFull)
XCTAssertNoThrow(XCTAssertEqual(buf, try channel.readInbound()))
XCTAssertNoThrow(XCTAssertNil(try channel.readInbound()))
XCTAssertNoThrow(XCTAssertNil(try channel.readOutbound()))
var bufB = channel.allocator.buffer(capacity: 1024)
bufB.writeString("again")
XCTAssertTrue(try channel.writeInbound(bufB).isFull)
XCTAssertTrue(try channel.finish().hasLeftOvers)
XCTAssertNoThrow(XCTAssertEqual(bufB, try channel.readInbound()))
XCTAssertNoThrow(XCTAssertNil(try channel.readInbound()))
XCTAssertNoThrow(XCTAssertNil(try channel.readOutbound()))
}
func testWriteInboundByteBufferReThrow() {
let channel = EmbeddedChannel()
XCTAssertNoThrow(try channel.pipeline.addHandler(ExceptionThrowingInboundHandler()).wait())
XCTAssertThrowsError(try channel.writeInbound("msg")) { error in
XCTAssertEqual(ChannelError.operationUnsupported, error as? ChannelError)
}
XCTAssertNoThrow(XCTAssertTrue(try channel.finish().isClean))
}
func testWriteOutboundByteBufferReThrow() {
let channel = EmbeddedChannel()
XCTAssertNoThrow(try channel.pipeline.addHandler(ExceptionThrowingOutboundHandler()).wait())
XCTAssertThrowsError(try channel.writeOutbound("msg")) { error in
XCTAssertEqual(ChannelError.operationUnsupported, error as? ChannelError)
}
XCTAssertNoThrow(XCTAssertTrue(try channel.finish().isClean))
}
func testReadOutboundWrongTypeThrows() {
let channel = EmbeddedChannel()
XCTAssertTrue(try channel.writeOutbound("hello").isFull)
do {
_ = try channel.readOutbound(as: Int.self)
XCTFail()
} catch let error as EmbeddedChannel.WrongTypeError {
let expectedError = EmbeddedChannel.WrongTypeError(expected: Int.self, actual: String.self)
XCTAssertEqual(error, expectedError)
} catch {
XCTFail()
}
}
func testReadInboundWrongTypeThrows() {
let channel = EmbeddedChannel()
XCTAssertTrue(try channel.writeInbound("hello").isFull)
do {
_ = try channel.readInbound(as: Int.self)
XCTFail()
} catch let error as EmbeddedChannel.WrongTypeError {
let expectedError = EmbeddedChannel.WrongTypeError(expected: Int.self, actual: String.self)
XCTAssertEqual(error, expectedError)
} catch {
XCTFail()
}
}
func testWrongTypesWithFastpathTypes() {
let channel = EmbeddedChannel()
defer {
XCTAssertNoThrow(XCTAssertTrue(try channel.finish().isClean))
}
let buffer = channel.allocator.buffer(capacity: 0)
let ioData = IOData.byteBuffer(buffer)
let fileHandle = NIOFileHandle(descriptor: -1)
let fileRegion = FileRegion(fileHandle: fileHandle, readerIndex: 0, endIndex: 0)
defer {
XCTAssertNoThrow(_ = try fileHandle.takeDescriptorOwnership())
}
XCTAssertTrue(try channel.writeOutbound(buffer).isFull)
XCTAssertTrue(try channel.writeOutbound(ioData).isFull)
XCTAssertTrue(try channel.writeOutbound(fileHandle).isFull)
XCTAssertTrue(try channel.writeOutbound(fileRegion).isFull)
XCTAssertTrue(try channel.writeOutbound(
AddressedEnvelope<ByteBuffer>(remoteAddress: SocketAddress(ipAddress: "1.2.3.4", port: 5678),
data: buffer)).isFull)
XCTAssertTrue(try channel.writeOutbound(buffer).isFull)
XCTAssertTrue(try channel.writeOutbound(ioData).isFull)
XCTAssertTrue(try channel.writeOutbound(fileRegion).isFull)
XCTAssertTrue(try channel.writeInbound(buffer).isFull)
XCTAssertTrue(try channel.writeInbound(ioData).isFull)
XCTAssertTrue(try channel.writeInbound(fileHandle).isFull)
XCTAssertTrue(try channel.writeInbound(fileRegion).isFull)
XCTAssertTrue(try channel.writeInbound(
AddressedEnvelope<ByteBuffer>(remoteAddress: SocketAddress(ipAddress: "1.2.3.4", port: 5678),
data: buffer)).isFull)
XCTAssertTrue(try channel.writeInbound(buffer).isFull)
XCTAssertTrue(try channel.writeInbound(ioData).isFull)
XCTAssertTrue(try channel.writeInbound(fileRegion).isFull)
func check<Expected, Actual>(expected: Expected.Type,
actual: Actual.Type,
file: StaticString = #file,
line: UInt = #line) {
do {
_ = try channel.readOutbound(as: Expected.self)
XCTFail("this should have failed", file: (file), line: line)
} catch let error as EmbeddedChannel.WrongTypeError {
let expectedError = EmbeddedChannel.WrongTypeError(expected: Expected.self, actual: Actual.self)
XCTAssertEqual(error, expectedError, file: (file), line: line)
} catch {
XCTFail("unexpected error: \(error)", file: (file), line: line)
}
do {
_ = try channel.readInbound(as: Expected.self)
XCTFail("this should have failed", file: (file), line: line)
} catch let error as EmbeddedChannel.WrongTypeError {
let expectedError = EmbeddedChannel.WrongTypeError(expected: Expected.self, actual: Actual.self)
XCTAssertEqual(error, expectedError, file: (file), line: line)
} catch {
XCTFail("unexpected error: \(error)", file: (file), line: line)
}
}
check(expected: Never.self, actual: IOData.self)
check(expected: Never.self, actual: IOData.self)
check(expected: Never.self, actual: NIOFileHandle.self)
check(expected: Never.self, actual: IOData.self)
check(expected: Never.self, actual: AddressedEnvelope<ByteBuffer>.self)
check(expected: NIOFileHandle.self, actual: IOData.self)
check(expected: NIOFileHandle.self, actual: IOData.self)
check(expected: ByteBuffer.self, actual: IOData.self)
}
func testCloseMultipleTimesThrows() throws {
let channel = EmbeddedChannel()
XCTAssertTrue(try channel.finish().isClean)
// Close a second time. This must fail.
do {
try channel.close().wait()
XCTFail("Second close succeeded")
} catch ChannelError.alreadyClosed {
// Nothing to do here.
}
}
func testCloseOnInactiveIsOk() throws {
let channel = EmbeddedChannel()
let inactiveHandler = CloseInChannelInactiveHandler()
XCTAssertNoThrow(try channel.pipeline.addHandler(inactiveHandler).wait())
XCTAssertTrue(try channel.finish().isClean)
// channelInactive should fire only once.
XCTAssertEqual(inactiveHandler.inactiveNotifications, 1)
}
func testEmbeddedLifecycle() throws {
let handler = ChannelLifecycleHandler()
XCTAssertEqual(handler.currentState, .unregistered)
let channel = EmbeddedChannel(handler: handler)
XCTAssertEqual(handler.currentState, .registered)
XCTAssertFalse(channel.isActive)
XCTAssertNoThrow(try channel.connect(to: try SocketAddress(unixDomainSocketPath: "/fake")).wait())
XCTAssertEqual(handler.currentState, .active)
XCTAssertTrue(channel.isActive)
XCTAssertTrue(try channel.finish().isClean)
XCTAssertEqual(handler.currentState, .unregistered)
XCTAssertFalse(channel.isActive)
}
private final class ExceptionThrowingInboundHandler : ChannelInboundHandler {
typealias InboundIn = String
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
context.fireErrorCaught(ChannelError.operationUnsupported)
}
}
private final class ExceptionThrowingOutboundHandler : ChannelOutboundHandler {
typealias OutboundIn = String
typealias OutboundOut = Never
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
promise!.fail(ChannelError.operationUnsupported)
}
}
private final class CloseInChannelInactiveHandler: ChannelInboundHandler {
typealias InboundIn = ByteBuffer
public var inactiveNotifications = 0
public func channelInactive(context: ChannelHandlerContext) {
inactiveNotifications += 1
context.close(promise: nil)
}
}
func testEmbeddedChannelAndPipelineAndChannelCoreShareTheEventLoop() {
let channel = EmbeddedChannel()
let pipelineEventLoop = channel.pipeline.eventLoop
XCTAssert(pipelineEventLoop === channel.eventLoop)
XCTAssert(pipelineEventLoop === (channel._channelCore as! EmbeddedChannelCore).eventLoop)
XCTAssertTrue(try channel.finish().isClean)
}
func testSendingAnythingOnEmbeddedChannel() throws {
let channel = EmbeddedChannel()
let buffer = ByteBufferAllocator().buffer(capacity: 5)
let socketAddress = try SocketAddress(unixDomainSocketPath: "path")
let handle = NIOFileHandle(descriptor: 1)
let fileRegion = FileRegion(fileHandle: handle, readerIndex: 1, endIndex: 2)
defer {
// fake descriptor, so shouldn't be closed.
XCTAssertNoThrow(try handle.takeDescriptorOwnership())
}
try channel.writeAndFlush(1).wait()
try channel.writeAndFlush("1").wait()
try channel.writeAndFlush(buffer).wait()
try channel.writeAndFlush(IOData.byteBuffer(buffer)).wait()
try channel.writeAndFlush(IOData.fileRegion(fileRegion)).wait()
try channel.writeAndFlush(AddressedEnvelope(remoteAddress: socketAddress, data: buffer)).wait()
}
func testActiveWhenConnectPromiseFiresAndInactiveWhenClosePromiseFires() throws {
let channel = EmbeddedChannel()
XCTAssertFalse(channel.isActive)
let connectPromise = channel.eventLoop.makePromise(of: Void.self)
connectPromise.futureResult.whenComplete { (_: Result<Void, Error>) in
XCTAssertTrue(channel.isActive)
}
channel.connect(to: try SocketAddress(ipAddress: "127.0.0.1", port: 0), promise: connectPromise)
try connectPromise.futureResult.wait()
let closePromise = channel.eventLoop.makePromise(of: Void.self)
closePromise.futureResult.whenComplete { (_: Result<Void, Error>) in
XCTAssertFalse(channel.isActive)
}
channel.close(promise: closePromise)
try closePromise.futureResult.wait()
}
func testWriteWithoutFlushDoesNotWrite() throws {
let channel = EmbeddedChannel()
let buf = ByteBuffer(bytes: [1])
let writeFuture = channel.write(buf)
XCTAssertNoThrow(XCTAssertNil(try channel.readOutbound()))
XCTAssertFalse(writeFuture.isFulfilled)
channel.flush()
XCTAssertNoThrow(XCTAssertNotNil(try channel.readOutbound(as: ByteBuffer.self)))
XCTAssertTrue(writeFuture.isFulfilled)
XCTAssertNoThrow(XCTAssertTrue(try channel.finish().isClean))
}
func testSetLocalAddressAfterSuccessfulBind() throws {
let channel = EmbeddedChannel()
let bindPromise = channel.eventLoop.makePromise(of: Void.self)
let socketAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
channel.bind(to: socketAddress, promise: bindPromise)
bindPromise.futureResult.whenComplete { _ in
XCTAssertEqual(channel.localAddress, socketAddress)
}
try bindPromise.futureResult.wait()
}
func testSetRemoteAddressAfterSuccessfulConnect() throws {
let channel = EmbeddedChannel()
let connectPromise = channel.eventLoop.makePromise(of: Void.self)
let socketAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
channel.connect(to: socketAddress, promise: connectPromise)
connectPromise.futureResult.whenComplete { _ in
XCTAssertEqual(channel.remoteAddress, socketAddress)
}
try connectPromise.futureResult.wait()
}
func testUnprocessedOutboundUserEventFailsOnEmbeddedChannel() {
let channel = EmbeddedChannel()
XCTAssertThrowsError(try channel.triggerUserOutboundEvent("event").wait()) { (error: Error) in
if let error = error as? ChannelError {
XCTAssertEqual(ChannelError.operationUnsupported, error)
} else {
XCTFail("unexpected error: \(error)")
}
}
}
func testEmbeddedChannelWritabilityIsWritable() {
let channel = EmbeddedChannel()
let opaqueChannel: Channel = channel
XCTAssertTrue(channel.isWritable)
XCTAssertTrue(opaqueChannel.isWritable)
channel.isWritable = false
XCTAssertFalse(channel.isWritable)
XCTAssertFalse(opaqueChannel.isWritable)
}
func testFinishWithRecursivelyScheduledTasks() throws {
let channel = EmbeddedChannel()
var invocations = 0
func recursivelyScheduleAndIncrement() {
channel.pipeline.eventLoop.scheduleTask(deadline: .distantFuture) {
invocations += 1
recursivelyScheduleAndIncrement()
}
}
recursivelyScheduleAndIncrement()
try XCTAssertNoThrow(channel.finish())
XCTAssertEqual(invocations, 1)
}
func testSyncOptionsAreSupported() throws {
let channel = EmbeddedChannel()
let options = channel.syncOptions
XCTAssertNotNil(options)
// Unconditonally returns true.
XCTAssertEqual(try options?.getOption(ChannelOptions.autoRead), true)
// (Setting options isn't supported.)
}
}
|