Remove EmbeddedChannel restriction to IOData (#763)

Remove EmbeddedChannel restriction to IOData

Motivation:

You should be able to read any type from the outbound buffer.

Modifications:

Change Embedded channel outboundBuffer and pendingOutboundBuffer to store NIOAny instead of IOData and refactor test code accordingly.

Result:

- Users can test what went through the pipeline without serializing everything and using "if case let" syntax everywhere.
- fixes #551
This commit is contained in:
JovanMilenkovic authored and Johannes Weiss committed 2019-01-30 19:24:59 +00:00
1 parent 70c714a54d
commit cf56bfd119
15 files changed
+89 -123

No files matched your search

+4 -16
View File
@@ -163,10 +163,10 @@ class EmbeddedChannelCore: ChannelCore {
}
/// Contains the flushed items that went into the `Channel` (and on a regular channel would have hit the network).
var outboundBuffer: [IOData] = []
var outboundBuffer: [NIOAny] = []
/// Contains the unflushed items that went into the `Channel`
var pendingOutboundBuffer: [(IOData, EventLoopPromise<Void>?)] = []
var pendingOutboundBuffer: [(NIOAny, EventLoopPromise<Void>?)] = []
/// Contains the items that travelled the `ChannelPipeline` all the way and hit the tail channel handler. On a
/// regular `Channel` these items would be lost.
@@ -222,11 +222,6 @@ class EmbeddedChannelCore: ChannelCore {
}
func write0(_ data: NIOAny, promise: EventLoopPromise<Void>?) {
guard let data = data.tryAsIOData() else {
promise?.fail(ChannelError.writeDataUnsupported)
return
}
self.pendingOutboundBuffer.append((data, promise))
}
@@ -329,11 +324,11 @@ public class EmbeddedChannel: Channel {
// Embedded channels never have parents.
public let parent: Channel? = nil
public func readOutbound() -> IOData? {
public func readOutbound<T>(as type: T.Type = T.self) -> T? {
return readFromBuffer(buffer: &channelcore.outboundBuffer)
}
public func readInbound<T>() -> T? {
public func readInbound<T>(as type: T.Type = T.self) -> T? {
return readFromBuffer(buffer: &channelcore.inboundBuffer)
}
@@ -363,13 +358,6 @@ public class EmbeddedChannel: Channel {
}
}
private func readFromBuffer(buffer: inout [IOData]) -> IOData? {
if buffer.isEmpty {
return nil
}
return buffer.removeFirst()
}
private func readFromBuffer<T>(buffer: inout [NIOAny]) -> T? {
if buffer.isEmpty {
return nil
@@ -133,7 +133,7 @@ class HTTPDecoderLengthTest: XCTestCase {
// Prime the decoder with a GET and consume it.
XCTAssertTrue(try channel.writeOutbound(HTTPClientRequestPart.head(HTTPRequestHead(version: version, method: .GET, uri: "/"))))
XCTAssertNotNil(channel.readOutbound())
XCTAssertNotNil(channel.readOutbound(as: ByteBuffer.self))
// We now want to send a HTTP/1.1 response. This response has no content-length, no transfer-encoding,
// is not a response to a HEAD request, is not a 2XX response to CONNECT, and is not 1XX, 204, or 304.
@@ -198,7 +198,7 @@ class HTTPDecoderLengthTest: XCTestCase {
XCTAssertTrue(try channel.writeOutbound(HTTPClientRequestPart.head(HTTPRequestHead(version: .init(major: 1, minor: 1),
method: requestMethod,
uri: "/"))))
XCTAssertNotNil(channel.readOutbound())
XCTAssertNotNil(channel.readOutbound(as: ByteBuffer.self))
// We now want to send a HTTP/1.1 response. This response may contain some length framing fields that RFC 7230 says MUST
// be ignored.
@@ -337,7 +337,7 @@ class HTTPDecoderLengthTest: XCTestCase {
XCTAssertTrue(try channel.writeOutbound(HTTPClientRequestPart.head(HTTPRequestHead(version: .init(major: 1, minor: 1),
method: .GET,
uri: "/"))))
XCTAssertNotNil(channel.readOutbound())
XCTAssertNotNil(channel.readOutbound(as: ByteBuffer.self))
// Send a 200 with the appropriate Transfer Encoding header. We should see the request,
// but no body or end.
@@ -33,7 +33,7 @@ class HTTPRequestEncoderTests: XCTestCase {
var request = HTTPRequestHead(version: HTTPVersion(major: 1, minor:1), method: method, uri: "/uri")
request.headers = headers
try channel.writeOutbound(HTTPClientRequestPart.head(request))
if case .some(.byteBuffer(let buffer)) = channel.readOutbound() {
if let buffer = channel.readOutbound(as: ByteBuffer.self) {
return buffer
} else {
fatalError("Could not read ByteBuffer from channel")
@@ -83,15 +83,9 @@ class HTTPRequestEncoderTests: XCTestCase {
// This request contains neither Transfer-Encoding: chunked or Content-Length.
let request = HTTPRequestHead(version: HTTPVersion(major: 1, minor:0), method: .GET, uri: "/uri")
XCTAssertNoThrow(try channel.writeOutbound(HTTPClientRequestPart.head(request)))
let writtenData: IOData = channel.readOutbound()!
switch writtenData {
case .byteBuffer(let b):
let writtenResponse = b.getString(at: b.readerIndex, length: b.readableBytes)!
XCTAssertEqual(writtenResponse, "GET /uri HTTP/1.0\r\n\r\n")
case .fileRegion:
XCTFail("Unexpected file region")
}
let writtenData = channel.readOutbound(as: ByteBuffer.self)!
let writtenResponse = writtenData.getString(at: writtenData.readerIndex, length: writtenData.readableBytes)!
XCTAssertEqual(writtenResponse, "GET /uri HTTP/1.0\r\n\r\n")
}
func testBody() throws {
@@ -135,7 +129,7 @@ class HTTPRequestEncoderTests: XCTestCase {
}
private func assertOutboundContainsOnly(_ channel: EmbeddedChannel, _ expected: String) {
if case .some(.byteBuffer(let buffer)) = channel.readOutbound() {
if let buffer = channel.readOutbound(as: ByteBuffer.self) {
buffer.assertContainsOnly(expected)
} else {
fatalError("Could not read ByteBuffer from channel")
@@ -199,12 +199,8 @@ class HTTPResponseCompressorTest: XCTestCase {
clientChannel.write(NIOAny(HTTPClientRequestPart.head(requestHead)), promise: nil)
clientChannel.write(NIOAny(HTTPClientRequestPart.end(nil)), promise: nil)
while let nextPart = channel.readOutbound() {
if case .byteBuffer(let b) = nextPart {
try clientChannel.writeInbound(b)
} else {
fatalError("We always write bytes")
}
while let b = channel.readOutbound(as: ByteBuffer.self) {
try clientChannel.writeInbound(b)
}
// The first inbound datum will be the response head. The rest will be byte buffers, until
@@ -33,7 +33,7 @@ class HTTPResponseEncoderTests: XCTestCase {
var switchingResponse = HTTPResponseHead(version: HTTPVersion(major: 1, minor:1), status: status)
switchingResponse.headers = headers
XCTAssertNoThrow(try channel.writeOutbound(HTTPServerResponsePart.head(switchingResponse)))
if case .some(.byteBuffer(let buffer)) = channel.readOutbound() {
if let buffer = channel.readOutbound(as: ByteBuffer.self) {
return buffer
} else {
fatalError("Could not read ByteBuffer from channel")
@@ -103,14 +103,11 @@ class HTTPResponseEncoderTests: XCTestCase {
// This response contains neither Transfer-Encoding: chunked or Content-Length.
let response = HTTPResponseHead(version: HTTPVersion(major: 1, minor:0), status: .ok)
XCTAssertNoThrow(try channel.writeOutbound(HTTPServerResponsePart.head(response)))
let writtenData: IOData = channel.readOutbound()!
switch writtenData {
case .byteBuffer(let b):
let writtenResponse = b.getString(at: b.readerIndex, length: b.readableBytes)!
XCTAssertEqual(writtenResponse, "HTTP/1.0 200 OK\r\n\r\n")
case .fileRegion:
XCTFail("Unexpected file region")
guard let b = channel.readOutbound(as: ByteBuffer.self) else {
XCTFail("Could not read byte buffer")
return
}
let writtenResponse = b.getString(at: b.readerIndex, length: b.readableBytes)!
XCTAssertEqual(writtenResponse, "HTTP/1.0 200 OK\r\n\r\n")
}
}
@@ -34,7 +34,7 @@ class HTTPServerProtocolErrorHandlerTest: XCTestCase {
XCTAssertNoThrow(try channel.closeFuture.wait())
// We expect exactly one ByteBuffer in the output.
guard case .some(.byteBuffer(var written)) = channel.readOutbound() else {
guard var written = channel.readOutbound(as: ByteBuffer.self) else {
XCTFail("No writes")
return
}
@@ -127,7 +127,7 @@ class HTTPServerProtocolErrorHandlerTest: XCTestCase {
XCTAssertNoThrow(try channel.closeFuture.wait())
// We expect exactly one ByteBuffer in the output.
guard case .some(.byteBuffer(var written)) = channel.readOutbound() else {
guard var written = channel.readOutbound(as: ByteBuffer.self) else {
XCTFail("No writes")
return
}
+1 -1
View File
@@ -64,7 +64,7 @@ extension ChannelPipeline {
extension EmbeddedChannel {
func readAllOutboundBuffers() -> ByteBuffer {
var buffer = self.allocator.buffer(capacity: 100)
while case .some(.byteBuffer(var writtenData)) = self.readOutbound() {
while var writtenData = self.readOutbound(as: ByteBuffer.self) {
buffer.write(buffer: &writtenData)
}
+2 -2
View File
@@ -330,7 +330,7 @@ class SNIHandlerTest: XCTestCase {
// The callback should have fired, but the handler should not have
// sent on any data and should still be in the pipeline.
XCTAssertTrue(called)
XCTAssertNil(channel.readInbound() as ByteBuffer?)
XCTAssertNil(channel.readInbound(as: ByteBuffer.self))
try channel.pipeline.assertContains(handler: handler)
// Now we're going to complete the promise and run the loop. This should cause the complete
@@ -369,7 +369,7 @@ class SNIHandlerTest: XCTestCase {
// The callback should not have fired, the handler should still be in the pipeline,
// and no data should have been written.
XCTAssertNil(channel.readInbound() as ByteBuffer?)
XCTAssertNil(channel.readInbound(as: ByteBuffer.self))
try channel.pipeline.assertContains(handler: handler)
XCTAssertNoThrow(try channel.finish())
+10 -22
View File
@@ -50,7 +50,7 @@ private extension EmbeddedChannel {
func assertWriteIndexOrder(_ order: [UInt8]) {
XCTAssertTrue(try self.writeOutbound(self.allocator.buffer(capacity: 32)))
guard case .some(.byteBuffer(var outBuffer2)) = self.readOutbound() else {
guard var outBuffer2 = self.readOutbound(as: ByteBuffer.self) else {
XCTFail("Could not read byte buffer")
return
}
@@ -112,8 +112,8 @@ class ChannelPipelineTest: XCTestCase {
}).wait()
XCTAssertNoThrow(try channel.writeAndFlush(NIOAny("msg")).wait() as Void)
if let data = channel.readOutbound() {
XCTAssertEqual(IOData.byteBuffer(buf), data)
if let data = channel.readOutbound(as: ByteBuffer.self) {
XCTAssertEqual(buf, data)
} else {
XCTFail("couldn't read from channel")
}
@@ -300,8 +300,8 @@ class ChannelPipelineTest: XCTestCase {
XCTAssertEqual([2, 6], channel.readInbound()!)
/* the first thing, we should receive is `[-2]` as it shouldn't hit any `MarkingOutboundHandler`s (`4`) */
var outbound = channel.readOutbound()
if case .some(.byteBuffer(var buf)) = outbound {
var outbound = channel.readOutbound(as: ByteBuffer.self)
if var buf = outbound {
XCTAssertEqual("[-2]", buf.readString(length: buf.readableBytes))
} else {
XCTFail("wrong contents: \(outbound.debugDescription)")
@@ -309,7 +309,7 @@ class ChannelPipelineTest: XCTestCase {
/* the next thing we should receive is `[-2, 4]` as the first `WriteOnReadHandler` (receiving `[2]`) is behind the `MarkingOutboundHandler` (`4`) */
outbound = channel.readOutbound()
if case .some(.byteBuffer(var buf)) = outbound {
if var buf = outbound {
XCTAssertEqual("[-2, 4]", buf.readString(length: buf.readableBytes))
} else {
XCTFail("wrong contents: \(outbound.debugDescription)")
@@ -317,7 +317,7 @@ class ChannelPipelineTest: XCTestCase {
/* and finally, we're waiting for `[-2, -6, 4]` as the second `WriteOnReadHandler`s (receiving `[2, 4]`) is behind the `MarkingOutboundHandler` (`4`) */
outbound = channel.readOutbound()
if case .some(.byteBuffer(var buf)) = outbound {
if var buf = outbound {
XCTAssertEqual("[-2, -6, 4]", buf.readString(length: buf.readableBytes))
} else {
XCTFail("wrong contents: \(outbound.debugDescription)")
@@ -708,11 +708,7 @@ class ChannelPipelineTest: XCTestCase {
XCTAssertNoThrow(try channel.throwIfErrorCaught())
channel.pipeline.remove(ctx: context, promise: removalPromise)
guard case .some(.byteBuffer(let receivedBuffer)) = channel.readOutbound() else {
XCTFail("No buffer")
return
}
XCTAssertEqual(receivedBuffer, buffer)
XCTAssertEqual(channel.readOutbound(), buffer)
do {
try channel.throwIfErrorCaught()
@@ -782,11 +778,7 @@ class ChannelPipelineTest: XCTestCase {
XCTAssertNoThrow(try channel.throwIfErrorCaught())
channel.pipeline.remove(name: "TestHandler", promise: removalPromise)
guard case .some(.byteBuffer(let receivedBuffer)) = channel.readOutbound() else {
XCTFail("No buffer")
return
}
XCTAssertEqual(receivedBuffer, buffer)
XCTAssertEqual(channel.readOutbound(), buffer)
do {
try channel.throwIfErrorCaught()
@@ -857,11 +849,7 @@ class ChannelPipelineTest: XCTestCase {
XCTAssertNoThrow(try channel.throwIfErrorCaught())
channel.pipeline.remove(handler: handler, promise: removalPromise)
guard case .some(.byteBuffer(let receivedBuffer)) = channel.readOutbound() else {
XCTFail("No buffer")
return
}
XCTAssertEqual(receivedBuffer, buffer)
XCTAssertEqual(channel.readOutbound(), buffer)
do {
try channel.throwIfErrorCaught()
+28 -28
View File
@@ -285,7 +285,7 @@ public class ByteToMessageDecoderTest: XCTestCase {
inputBuffer.clear()
func readOneInboundString() -> String {
switch channel.readInbound() as ByteBuffer? {
switch channel.readInbound(as: ByteBuffer.self) {
case .some(let buffer):
return String(decoding: buffer.readableBytesView, as: Unicode.UTF8.self)
case .none:
@@ -300,7 +300,7 @@ public class ByteToMessageDecoderTest: XCTestCase {
XCTAssertEqual("3", readOneInboundString())
XCTAssertEqual("4", readOneInboundString())
XCTAssertEqual("5", readOneInboundString())
XCTAssertNil(channel.readInbound() as IOData?)
XCTAssertNil(channel.readInbound(as: IOData.self))
XCTAssertTrue(testDecoder.hasReentranced)
}
@@ -338,9 +338,9 @@ public class ByteToMessageDecoderTest: XCTestCase {
XCTAssertNoThrow(try channel.writeInbound(buffer))
XCTAssertFalse(channel.isActive)
XCTAssertEqual("1", (channel.readInbound() as ByteBuffer?).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("23", (channel.readInbound() as ByteBuffer?).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("4567890", (channel.readInbound() as ByteBuffer?).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("1", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("23", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("4567890", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertNil(channel.readInbound())
}
@@ -361,11 +361,11 @@ public class ByteToMessageDecoderTest: XCTestCase {
XCTAssertNoThrow(try channel.close().wait())
XCTAssertFalse(channel.isActive)
XCTAssertEqual("12", (channel.readInbound() as ByteBuffer?).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("34", (channel.readInbound() as ByteBuffer?).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("56", (channel.readInbound() as ByteBuffer?).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("78", (channel.readInbound() as ByteBuffer?).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("90", (channel.readInbound() as ByteBuffer?).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("12", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("34", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("56", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("78", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("90", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertNil(channel.readInbound())
XCTAssertNoThrow(XCTAssertEqual("x", String(decoding: try lastPromise.futureResult.wait().readableBytesView,
@@ -398,11 +398,11 @@ public class ByteToMessageDecoderTest: XCTestCase {
XCTFail("unexpected error: \(error)")
}
XCTAssertEqual("12", (channel.readInbound() as ByteBuffer?).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("34", (channel.readInbound() as ByteBuffer?).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("56", (channel.readInbound() as ByteBuffer?).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("78", (channel.readInbound() as ByteBuffer?).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("90", (channel.readInbound() as ByteBuffer?).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("12", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("34", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("56", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("78", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertEqual("90", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) })
XCTAssertNil(channel.readInbound())
XCTAssertNoThrow(XCTAssertEqual("x", String(decoding: try lastPromise.futureResult.wait().readableBytesView,
@@ -445,16 +445,16 @@ public class ByteToMessageDecoderTest: XCTestCase {
buffer.write(staticString: "4567890qwer")
XCTAssertNoThrow(try channel.writeInbound(buffer))
XCTAssertEqual(1, (channel.readInbound() as Int?))
XCTAssertEqual(2, (channel.readInbound() as Int?))
XCTAssertEqual(3, (channel.readInbound() as Int?))
XCTAssertEqual(4, (channel.readInbound() as Int?))
XCTAssertEqual(1, channel.readInbound())
XCTAssertEqual(2, channel.readInbound())
XCTAssertEqual(3, channel.readInbound())
XCTAssertEqual(4, channel.readInbound())
XCTAssertNil(channel.readInbound())
XCTAssertNoThrow(try channel.close().wait())
XCTAssertFalse(channel.isActive)
XCTAssertEqual(-4, (channel.readInbound() as Int?))
XCTAssertEqual(-4, channel.readInbound())
XCTAssertNil(channel.readInbound())
}
@@ -482,11 +482,11 @@ public class ByteToMessageDecoderTest: XCTestCase {
buffer.write(staticString: "0123456789abcdef")
XCTAssertNoThrow(try channel.writeInbound(buffer))
XCTAssertEqual("0123456789abcdef", (channel.readInbound() as String?))
XCTAssertEqual("01234567", (channel.readInbound() as String?))
XCTAssertEqual("0123", (channel.readInbound() as String?))
XCTAssertEqual("01", (channel.readInbound() as String?))
XCTAssertEqual("0", (channel.readInbound() as String?))
XCTAssertEqual("0123456789abcdef", channel.readInbound())
XCTAssertEqual("01234567", channel.readInbound())
XCTAssertEqual("0123", channel.readInbound())
XCTAssertEqual("01", channel.readInbound())
XCTAssertEqual("0", channel.readInbound())
XCTAssertNil(channel.readInbound())
}
@@ -516,8 +516,8 @@ public class ByteToMessageDecoderTest: XCTestCase {
buffer.write(staticString: "0123456789abcdefQWER")
XCTAssertNoThrow(try channel.writeInbound(buffer))
XCTAssertEqual("0123456789abcdef", (channel.readInbound() as ByteBuffer?).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self)})
XCTAssertEqual("QWER", (channel.readInbound() as ByteBuffer?).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self)})
XCTAssertEqual("0123456789abcdef", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self)})
XCTAssertEqual("QWER", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self)})
XCTAssertNil(channel.readInbound())
}
@@ -564,7 +564,7 @@ public class MessageToByteEncoderTest: XCTestCase {
_ = try channel.writeAndFlush(NIOAny(Int32(5))).wait()
if case .some(.byteBuffer(var buffer)) = channel.readOutbound() {
if var buffer = channel.readOutbound(as: ByteBuffer.self) {
XCTAssertEqual(Int32(5), buffer.readInteger())
XCTAssertEqual(0, buffer.readableBytes)
} else {
@@ -34,7 +34,7 @@ extension EmbeddedChannelTest {
("testCloseOnInactiveIsOk", testCloseOnInactiveIsOk),
("testEmbeddedLifecycle", testEmbeddedLifecycle),
("testEmbeddedChannelAndPipelineAndChannelCoreShareTheEventLoop", testEmbeddedChannelAndPipelineAndChannelCoreShareTheEventLoop),
("testSendingIncorrectDataOnEmbeddedChannel", testSendingIncorrectDataOnEmbeddedChannel),
("testSendingAnythingOnEmbeddedChannel", testSendingAnythingOnEmbeddedChannel),
("testActiveWhenConnectPromiseFiresAndInactiveWhenClosePromiseFires", testActiveWhenConnectPromiseFiresAndInactiveWhenClosePromiseFires),
("testWriteWithoutFlushDoesNotWrite", testWriteWithoutFlushDoesNotWrite),
]
+18 -15
View File
@@ -20,10 +20,10 @@ class EmbeddedChannelTest: XCTestCase {
let channel = EmbeddedChannel()
var buf = channel.allocator.buffer(capacity: 1024)
buf.write(string: "hello")
XCTAssertTrue(try channel.writeOutbound(buf))
XCTAssertTrue(try channel.finish())
XCTAssertEqual(.byteBuffer(buf), channel.readOutbound())
XCTAssertEqual(buf, channel.readOutbound())
XCTAssertNil(channel.readOutbound())
XCTAssertNil(channel.readInbound())
}
@@ -140,20 +140,23 @@ class EmbeddedChannelTest: XCTestCase {
XCTAssert(pipelineEventLoop === (channel._unsafe as! EmbeddedChannelCore).eventLoop)
XCTAssertFalse(try channel.finish())
}
func testSendingIncorrectDataOnEmbeddedChannel() {
func testSendingAnythingOnEmbeddedChannel() throws {
let channel = EmbeddedChannel()
do {
try channel.writeAndFlush(NIOAny(5)).wait()
XCTFail("Did not throw")
} catch ChannelError.writeDataUnsupported {
// All good
} catch {
XCTFail("Got \(error)")
let buffer = ByteBufferAllocator().buffer(capacity: 5)
let socketAddress = try SocketAddress(unixDomainSocketPath: "path")
let handle = FileHandle(descriptor: 1)
let fileRegion = FileRegion(fileHandle: handle, readerIndex: 1, endIndex: 2)
defer {
// fake descriptor, so shouldn't be closed.
XCTAssertNoThrow(try handle.takeDescriptorOwnership())
}
XCTAssertFalse(try channel.finish())
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 {
@@ -184,7 +187,7 @@ class EmbeddedChannelTest: XCTestCase {
XCTAssertNil(channel.readOutbound())
XCTAssertFalse(writeFuture.isFulfilled)
channel.flush()
XCTAssertNotNil(channel.readOutbound())
XCTAssertNotNil(channel.readOutbound(as: ByteBuffer.self))
XCTAssertTrue(writeFuture.isFulfilled)
XCTAssertNoThrow(try XCTAssertFalse(channel.finish()))
}
+2 -2
View File
@@ -57,11 +57,11 @@ private func interactInMemory(_ first: EmbeddedChannel, _ second: EmbeddedChanne
repeat {
operated = false
if case .some(.byteBuffer(let data)) = first.readOutbound() {
if let data = first.readOutbound(as: ByteBuffer.self) {
operated = true
try second.writeInbound(data)
}
if case .some(.byteBuffer(let data)) = second.readOutbound() {
if let data = second.readOutbound(as: ByteBuffer.self) {
operated = true
try first.writeInbound(data)
}
@@ -79,7 +79,7 @@ public class WebSocketFrameDecoderTest: XCTestCase {
private func frameForFrame(_ frame: WebSocketFrame) -> WebSocketFrame? {
self.encoderChannel.writeAndFlush(frame, promise: nil)
while case .some(.byteBuffer(let d)) = self.encoderChannel.readOutbound() {
while let d = self.encoderChannel.readOutbound(as: ByteBuffer.self) {
XCTAssertNoThrow(try self.decoderChannel.writeInbound(d))
}
@@ -88,7 +88,7 @@ public class WebSocketFrameDecoderTest: XCTestCase {
return nil
}
// Should only have gotten one frame!
XCTAssertNil(self.decoderChannel.readInbound() as WebSocketFrame?)
XCTAssertNil(self.decoderChannel.readInbound(as: WebSocketFrame.self))
return producedFrame
}
@@ -331,14 +331,14 @@ public class WebSocketFrameDecoderTest: XCTestCase {
// a double-parse edge case.
self.encoderChannel.write(frame, promise: nil)
var frameBuffer = self.decoderChannel.allocator.buffer(capacity: 10)
while case .some(.byteBuffer(var d)) = self.encoderChannel.readOutbound() {
while var d = self.encoderChannel.readOutbound(as: ByteBuffer.self) {
frameBuffer.write(buffer: &d)
}
XCTAssertNoThrow(try self.decoderChannel.writeInbound(frameBuffer))
// No data should have been sent or received.
XCTAssertNil(self.decoderChannel.readOutbound())
XCTAssertNil(self.decoderChannel.readInbound() as WebSocketFrame?)
XCTAssertNil(self.decoderChannel.readInbound(as: WebSocketFrame.self))
}
public func testDecoderRejectsOverlongFramesWithNoAutomaticErrorHandling() throws {
@@ -19,7 +19,7 @@ import NIOWebSocket
extension EmbeddedChannel {
func readAllOutboundBuffers() -> ByteBuffer {
var buffer = self.allocator.buffer(capacity: 100)
while case .some(.byteBuffer(var writtenData)) = self.readOutbound() {
while var writtenData = self.readOutbound(as: ByteBuffer.self) {
buffer.write(buffer: &writtenData)
}