EmbeddedChannel: remove mystery bools (#917)

Motivation:

EmbeddedChannel.finish/writeInbound/writeOutbound returned some mystery
bools. I always had to check the code to remember what they actually
meaned.

Whilst this is technically a breaking change, I couldn't find any users
of the return values on Github that are using the convergence releases.

Modifications:

Replace them by enums giving you all the information.

Result:

- fixes #916
- clearer APIs
This commit is contained in:
Johannes Weiss authored and GitHub committed 2019-03-22 12:34:16 +00:00
1 parent 169f34adfc
commit a8fbcf080b
14 files changed
+168 -106

No files matched your search

+71 -10
View File
@@ -289,6 +289,39 @@ class EmbeddedChannelCore: ChannelCore {
/// unsynchronized fashion. `EmbeddedEventLoop`s notes also apply as
/// `EmbeddedChannel` uses an `EmbeddedEventLoop` as its `EventLoop`.
public class EmbeddedChannel: Channel {
public enum LeftOverState {
case clean
case leftOvers(inbound: [NIOAny], outbound: [NIOAny], pendingOutbound: [NIOAny])
public var isClean: Bool {
if case .clean = self {
return true
} else {
return false
}
}
public var hasLeftOvers: Bool {
return !self.isClean
}
}
public enum BufferState {
case empty
case full([NIOAny])
public var isEmpty: Bool {
if case .empty = self {
return true
} else {
return false
}
}
public var isFull: Bool {
return !self.isEmpty
}
}
public var isActive: Bool { return channelcore.isActive }
public var closeFuture: EventLoopFuture<Void> { return channelcore.closePromise.futureResult }
@@ -307,11 +340,27 @@ public class EmbeddedChannel: Channel {
return true
}
public func finish() throws -> Bool {
/// Synchronously closes the `EmbeddedChannel`.
///
/// This method will throw if the `Channel` hit any unconsumed errors or if the `close` fails. Errors in the
/// `EmbeddedChannel` can be consumed using `throwIfErrorCaught`.
///
/// - returns: The `LeftOverState` of the `EmbeddedChannel`. If all the inbound and outbound events have been
// consumed (using `readInbound` / `readOutbound`) and there are no pending outbound events (unflushed
// writes) this will be `.clean`. If there are any unconsumed inbound, outbound, or pending outbound
// events, the `EmbeddedChannel` will returns those as `.leftOvers(inbound:outbound:pendingOutbound:)`.
public func finish() throws -> LeftOverState {
try close().wait()
self.embeddedEventLoop.run()
try throwIfErrorCaught()
return !channelcore.outboundBuffer.isEmpty || !channelcore.inboundBuffer.isEmpty || !channelcore.pendingOutboundBuffer.isEmpty
let c = self.channelcore
if c.outboundBuffer.isEmpty && c.inboundBuffer.isEmpty && c.pendingOutboundBuffer.isEmpty {
return .clean
} else {
return .leftOvers(inbound: c.inboundBuffer,
outbound: c.outboundBuffer,
pendingOutbound: c.pendingOutboundBuffer.map { $0.0 })
}
}
private var _pipeline: ChannelPipeline!
@@ -345,23 +394,35 @@ public class EmbeddedChannel: Channel {
return try readFromBuffer(buffer: &channelcore.inboundBuffer)
}
/// Writes `data` into the `EmbeddedChannel`'s pipeline. This will result in a `channelRead` and a
/// `channelReadComplete` event for the first `ChannelHandler`.
/// Sends an inbound `channelRead` event followed by a `channelReadComplete` event through the `ChannelPipeline`.
///
/// The immediate effect being that the first `ChannelInboundHandler` will get its `channelRead` method called
/// with the data you provide.
///
/// - parameters:
/// - data: The data to fire through the pipeline.
/// - returns: If the `inboundBuffer` now contains items. The `inboundBuffer` will be empty until some item
/// travels the `ChannelPipeline` all the way and hits the tail channel handler.
@discardableResult public func writeInbound<T>(_ data: T) throws -> Bool {
/// - returns: The state of the inbound buffer which contains all the events that travelled the `ChannelPipeline`
// all the way.
@discardableResult public func writeInbound<T>(_ data: T) throws -> BufferState {
pipeline.fireChannelRead(NIOAny(data))
pipeline.fireChannelReadComplete()
try throwIfErrorCaught()
return !channelcore.inboundBuffer.isEmpty
return self.channelcore.inboundBuffer.isEmpty ? .empty : .full(self.channelcore.inboundBuffer)
}
@discardableResult public func writeOutbound<T>(_ data: T) throws -> Bool {
/// Sends an outbound `writeAndFlush` event through the `ChannelPipeline`.
///
/// The immediate effect being that the first `ChannelOutboundHandler` will get its `write` method called
/// with the data you provide. Note that the first `ChannelOutboundHandler` in the pipeline is the _last_ handler
/// because outbound events travel the pipeline from back to front.
///
/// - parameters:
/// - data: The data to fire through the pipeline.
/// - returns: The state of the outbound buffer which contains all the events that travelled the `ChannelPipeline`
// all the way.
@discardableResult public func writeOutbound<T>(_ data: T) throws -> BufferState {
try writeAndFlush(NIOAny(data)).wait()
return !channelcore.outboundBuffer.isEmpty
return self.channelcore.outboundBuffer.isEmpty ? .empty : .full(self.channelcore.outboundBuffer)
}
public func throwIfErrorCaught() throws {
@@ -132,7 +132,7 @@ class HTTPDecoderLengthTest: XCTestCase {
XCTAssertNoThrow(try channel.pipeline.addHandler(handler).wait())
// Prime the decoder with a GET and consume it.
XCTAssertTrue(try channel.writeOutbound(HTTPClientRequestPart.head(HTTPRequestHead(version: version, method: .GET, uri: "/"))))
XCTAssertTrue(try channel.writeOutbound(HTTPClientRequestPart.head(HTTPRequestHead(version: version, method: .GET, uri: "/"))).isFull)
XCTAssertNoThrow(XCTAssertNotNil(try channel.readOutbound(as: ByteBuffer.self)))
// We now want to send a HTTP/1.1 response. This response has no content-length, no transfer-encoding,
@@ -166,7 +166,7 @@ class HTTPDecoderLengthTest: XCTestCase {
XCTAssertTrue(handler.receivedEnd)
XCTAssertTrue(handler.eof)
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func testHTTP11SemanticEOFOnChannelInactive() throws {
@@ -197,7 +197,7 @@ class HTTPDecoderLengthTest: XCTestCase {
// Prime the decoder with a request and consume it.
XCTAssertTrue(try channel.writeOutbound(HTTPClientRequestPart.head(HTTPRequestHead(version: .init(major: 1, minor: 1),
method: requestMethod,
uri: "/"))))
uri: "/"))).isFull)
XCTAssertNoThrow(XCTAssertNotNil(try 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
@@ -222,7 +222,7 @@ class HTTPDecoderLengthTest: XCTestCase {
XCTAssertFalse(handler.seenBody)
XCTAssert(handler.seenEnd)
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func testIgnoresTransferEncodingFieldOnCONNECTResponses() throws {
@@ -305,7 +305,7 @@ class HTTPDecoderLengthTest: XCTestCase {
XCTAssertFalse(handler.seenBody)
XCTAssert(handler.seenEnd)
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func testMultipleTEWithChunkedLastHasNoBodyOnRequest() throws {
@@ -336,7 +336,7 @@ class HTTPDecoderLengthTest: XCTestCase {
// Prime the decoder with a request and consume it.
XCTAssertTrue(try channel.writeOutbound(HTTPClientRequestPart.head(HTTPRequestHead(version: .init(major: 1, minor: 1),
method: .GET,
uri: "/"))))
uri: "/"))).isFull)
XCTAssertNoThrow(XCTAssertNotNil(try channel.readOutbound(as: ByteBuffer.self)))
// Send a 200 with the appropriate Transfer Encoding header. We should see the request,
@@ -362,7 +362,7 @@ class HTTPDecoderLengthTest: XCTestCase {
XCTAssert(handler.seenBody)
XCTAssert(handler.seenEnd)
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func testMultipleTEWithChunkedLastHasEOFBodyOnResponseWithChannelInactive() throws {
@@ -422,7 +422,7 @@ class HTTPDecoderLengthTest: XCTestCase {
// Prime the decoder with a request.
XCTAssertTrue(try channel.writeOutbound(HTTPClientRequestPart.head(HTTPRequestHead(version: .init(major: 1, minor: 1),
method: .GET,
uri: "/"))))
uri: "/"))).isFull)
// Send a 200 OK with the invalid headers.
do {
@@ -524,6 +524,6 @@ class HTTPDecoderLengthTest: XCTestCase {
XCTAssertFalse(handler.seenBody)
XCTAssert(handler.seenEnd)
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
}
+1 -1
View File
@@ -102,7 +102,7 @@ class HTTPHeadersTest : XCTestCase {
XCTAssertTrue(writtenBytes.contains("SET-COOKIE: foo=bar\r\n"))
XCTAssertTrue(writtenBytes.contains("Set-Cookie: buz=cux\r\n"))
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func testRevealHeadersSeparately() {
@@ -26,7 +26,7 @@ class HTTPRequestEncoderTests: XCTestCase {
private func sendRequest(withMethod method: HTTPMethod, andHeaders headers: HTTPHeaders) throws -> ByteBuffer {
let channel = EmbeddedChannel()
defer {
XCTAssertEqual(.some(false), try? channel.finish())
XCTAssertEqual(true, try? channel.finish().isClean)
}
try channel.pipeline.addHandler(HTTPRequestEncoder()).wait()
@@ -75,7 +75,7 @@ class HTTPRequestEncoderTests: XCTestCase {
func testNoChunkedEncodingForHTTP10() throws {
let channel = EmbeddedChannel()
defer {
XCTAssertEqual(.some(false), try? channel.finish())
XCTAssertEqual(true, try? channel.finish().isClean)
}
XCTAssertNoThrow(try channel.pipeline.addHandler(HTTPRequestEncoder()).wait())
@@ -91,7 +91,7 @@ class HTTPRequestEncoderTests: XCTestCase {
func testBody() throws {
let channel = EmbeddedChannel()
defer {
XCTAssertEqual(.some(false), try? channel.finish())
XCTAssertEqual(true, try? channel.finish().isClean)
}
try channel.pipeline.addHandler(HTTPRequestEncoder()).wait()
@@ -113,7 +113,7 @@ class HTTPRequestEncoderTests: XCTestCase {
func testCONNECT() throws {
let channel = EmbeddedChannel()
defer {
XCTAssertEqual(.some(false), try? channel.finish())
XCTAssertEqual(true, try? channel.finish().isClean)
}
let uri = "server.example.com:80"
@@ -26,7 +26,7 @@ class HTTPResponseEncoderTests: XCTestCase {
private func sendResponse(withStatus status: HTTPResponseStatus, andHeaders headers: HTTPHeaders) -> ByteBuffer {
let channel = EmbeddedChannel()
defer {
XCTAssertEqual(.some(false), try? channel.finish())
XCTAssertEqual(true, try? channel.finish().isClean)
}
XCTAssertNoThrow(try channel.pipeline.addHandler(HTTPResponseEncoder()).wait())
@@ -102,7 +102,7 @@ class HTTPResponseEncoderTests: XCTestCase {
func testNoChunkedEncodingForHTTP10() throws {
let channel = EmbeddedChannel()
defer {
XCTAssertEqual(.some(false), try? channel.finish())
XCTAssertEqual(true, try? channel.finish().isClean)
}
XCTAssertNoThrow(try channel.pipeline.addHandler(HTTPResponseEncoder()).wait())
+6 -6
View File
@@ -376,7 +376,7 @@ class HTTPUpgradeTestCase: XCTestCase {
func testUpgradeHandlerBarfsOnUnexpectedOrdering() throws {
let channel = EmbeddedChannel()
defer {
XCTAssertEqual(.some(false), try? channel.finish())
XCTAssertEqual(true, try? channel.finish().isClean)
}
let handler = HTTPServerUpgradeHandler(upgraders: [ExplodingUpgrader(forProtocol: "myproto")],
@@ -844,7 +844,7 @@ class HTTPUpgradeTestCase: XCTestCase {
defer {
do {
let complete = try channel.finish()
XCTAssertFalse(complete)
XCTAssertTrue(complete.isClean)
} catch No.no {
// ok
} catch {
@@ -911,8 +911,8 @@ class HTTPUpgradeTestCase: XCTestCase {
let channel = EmbeddedChannel()
defer {
do {
let complete = try channel.finish()
XCTAssertFalse(complete)
let isCleanOnFinish = try channel.finish().isClean
XCTAssertTrue(isCleanOnFinish)
} catch No.no {
// ok
} catch {
@@ -982,8 +982,8 @@ class HTTPUpgradeTestCase: XCTestCase {
let channel = EmbeddedChannel()
defer {
do {
let complete = try channel.finish()
XCTAssertFalse(complete)
let isCleanOnFinish = try channel.finish().isClean
XCTAssertTrue(isCleanOnFinish)
} catch No.no {
// ok
} catch {
@@ -52,7 +52,7 @@ class ApplicationProtocolNegotiationHandlerTests: XCTestCase {
// The channel handler should still be in the pipeline.
try channel.pipeline.assertContains(handler: handler)
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
private func negotiateTest(event: TLSUserEvent, expectedResult: ALPNResult) throws {
@@ -85,7 +85,7 @@ class ApplicationProtocolNegotiationHandlerTests: XCTestCase {
// Now the handler should have removed itself from the pipeline.
try channel.pipeline.assertDoesNotContain(handler: handler)
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func testCallbackReflectsNotificationResult() throws {
@@ -111,7 +111,7 @@ class ApplicationProtocolNegotiationHandlerTests: XCTestCase {
try channel.writeInbound("hello")
XCTAssertNoThrow(XCTAssertEqual(try channel.readInbound()!, "hello"))
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func testBufferingWhileWaitingForFuture() throws {
@@ -142,7 +142,7 @@ class ApplicationProtocolNegotiationHandlerTests: XCTestCase {
XCTAssertNoThrow(XCTAssertEqual(try channel.readInbound()!, "are"))
XCTAssertNoThrow(XCTAssertEqual(try channel.readInbound()!, "buffered"))
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func testNothingBufferedDoesNotFireReadCompleted() throws {
@@ -169,7 +169,7 @@ class ApplicationProtocolNegotiationHandlerTests: XCTestCase {
continuePromise.succeed(())
XCTAssertEqual(readCompleteHandler.readCompleteCount, 0)
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func testUnbufferingFiresReadCompleted() throws {
@@ -200,6 +200,6 @@ class ApplicationProtocolNegotiationHandlerTests: XCTestCase {
XCTAssertEqual(readCompleteHandler.readCompleteCount, 2)
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
}
+2 -2
View File
@@ -303,7 +303,7 @@ class SNIHandlerTest: XCTestCase {
try channel.pipeline.assertDoesNotContain(handler: handler)
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
/// Blasts the client hello in as a single string. This is not expected to reveal bugs
@@ -348,7 +348,7 @@ class SNIHandlerTest: XCTestCase {
}
try channel.pipeline.assertDoesNotContain(handler: handler)
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func assertIncompleteInput(clientHello: String) throws {
+17 -17
View File
@@ -43,7 +43,7 @@ private final class IndexWritingHandler: ChannelDuplexHandler {
private extension EmbeddedChannel {
func assertReadIndexOrder(_ order: [UInt8]) {
XCTAssertTrue(try self.writeInbound(self.allocator.buffer(capacity: 32)))
XCTAssertTrue(try self.writeInbound(self.allocator.buffer(capacity: 32)).isFull)
do {
var outBuffer: ByteBuffer = try self.readInbound()!
XCTAssertEqual(outBuffer.readBytes(length: outBuffer.readableBytes)!, order)
@@ -53,7 +53,7 @@ private extension EmbeddedChannel {
}
func assertWriteIndexOrder(_ order: [UInt8]) {
XCTAssertTrue(try self.writeOutbound(self.allocator.buffer(capacity: 32)))
XCTAssertTrue(try self.writeOutbound(self.allocator.buffer(capacity: 32)).isFull)
do {
guard var outBuffer2 = try self.readOutbound(as: ByteBuffer.self) else {
XCTFail("Could not read byte buffer")
@@ -127,13 +127,13 @@ class ChannelPipelineTest: XCTestCase {
}
XCTAssertNoThrow(XCTAssertNil(try channel.readOutbound()))
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func testConnectingDoesntCallBind() throws {
let channel = EmbeddedChannel()
defer {
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
var ipv4SocketAddress = sockaddr_in()
ipv4SocketAddress.sin_port = (12345 as in_port_t).bigEndian
@@ -197,14 +197,14 @@ class ChannelPipelineTest: XCTestCase {
}.wait()
XCTAssertNoThrow(XCTAssertEqual(Optional<Int>.some(1), try channel.readInbound()))
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func testEmptyPipelineWorks() throws {
let channel = EmbeddedChannel()
XCTAssertTrue(try assertNoThrowWithValue(channel.writeInbound(2)))
XCTAssertTrue(try assertNoThrowWithValue(channel.writeInbound(2)).isFull)
XCTAssertNoThrow(XCTAssertEqual(Optional<Int>.some(2), try channel.readInbound()))
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func testWriteAfterClose() throws {
@@ -334,7 +334,7 @@ class ChannelPipelineTest: XCTestCase {
XCTAssertNoThrow(XCTAssertNil(try channel.readInbound()))
XCTAssertNoThrow(XCTAssertNil(try channel.readOutbound()))
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func testChannelInfrastructureIsNotLeaked() throws {
@@ -388,7 +388,7 @@ class ChannelPipelineTest: XCTestCase {
XCTAssertNil(weakHandlerContext1)
XCTAssertNotNil(weakHandlerContext2)
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
XCTAssertNil(weakHandler1)
XCTAssertNil(weakHandler2)
@@ -444,13 +444,13 @@ class ChannelPipelineTest: XCTestCase {
buffer.writeStaticString("hello, world")
XCTAssertNoThrow(try channel.pipeline.addHandler(countHandler).wait())
XCTAssertFalse(try channel.writeInbound(buffer))
XCTAssertTrue(try channel.writeInbound(buffer).isEmpty)
XCTAssertEqual(countHandler.intReadCount, 0)
try channel.pipeline.addHandlers(TransformByteBufferToStringHandler(),
TransformStringToIntHandler(),
position: .first).wait()
XCTAssertFalse(try channel.writeInbound(buffer))
XCTAssertTrue(try channel.writeInbound(buffer).isEmpty)
XCTAssertEqual(countHandler.intReadCount, 1)
}
@@ -637,7 +637,7 @@ class ChannelPipelineTest: XCTestCase {
let channel = EmbeddedChannel()
defer {
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
do {
@@ -673,7 +673,7 @@ class ChannelPipelineTest: XCTestCase {
let channel = EmbeddedChannel()
defer {
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
do {
@@ -746,7 +746,7 @@ class ChannelPipelineTest: XCTestCase {
let channel = EmbeddedChannel()
defer {
// This will definitely throw.
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
XCTAssertNoThrow(try channel.pipeline.addHandler(NoOpHandler()).wait())
@@ -818,7 +818,7 @@ class ChannelPipelineTest: XCTestCase {
let channel = EmbeddedChannel()
defer {
// This will definitely throw.
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
XCTAssertNoThrow(try channel.pipeline.addHandler(NoOpHandler(), name: "TestHandler").wait())
@@ -889,7 +889,7 @@ class ChannelPipelineTest: XCTestCase {
let channel = EmbeddedChannel()
defer {
// This will definitely throw.
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
let handler = NoOpHandler()
@@ -978,7 +978,7 @@ class ChannelPipelineTest: XCTestCase {
XCTAssertNoThrow(try removalTokenPromise.futureResult.map { removalToken in
// we know that the removal process has been started, so let's tear down the pipeline
func workaroundSR9815withAUselessFunction() {
XCTAssertNoThrow(XCTAssertFalse(try channel.finish()))
XCTAssertNoThrow(XCTAssertTrue(try channel.finish().isClean))
}
workaroundSR9815withAUselessFunction()
}.wait())
+16 -16
View File
@@ -213,14 +213,14 @@ public class ByteToMessageDecoderTest: XCTestCase {
// a 512-byte empty region in a 513 byte buffer. This will not cause a shrink.
var buffer = channel.allocator.buffer(capacity: 513)
buffer.writeBytes(Array(repeating: 0x04, count: 513))
XCTAssertTrue(try channel.writeInbound(buffer))
XCTAssertTrue(try channel.writeInbound(buffer).isFull)
XCTAssertEqual(decoder.cumulationBuffer!.readableBytes, 1)
XCTAssertEqual(decoder.cumulationBuffer!.readerIndex, 512)
// Now we're going to send in another 513 bytes. This will cause another chunk to be passed in,
// but now we'll shrink the buffer.
XCTAssertTrue(try channel.writeInbound(buffer))
XCTAssertTrue(try channel.writeInbound(buffer).isFull)
XCTAssertEqual(decoder.cumulationBuffer!.readableBytes, 2)
XCTAssertEqual(decoder.cumulationBuffer!.readerIndex, 0)
@@ -238,14 +238,14 @@ public class ByteToMessageDecoderTest: XCTestCase {
// We're going to send in 5119 bytes. This will be held.
var buffer = channel.allocator.buffer(capacity: 5119)
buffer.writeBytes(Array(repeating: 0x04, count: 5119))
XCTAssertFalse(try channel.writeInbound(buffer))
XCTAssertTrue(try channel.writeInbound(buffer).isEmpty)
XCTAssertEqual(decoder.cumulationBuffer!.readableBytes, 5119)
XCTAssertEqual(decoder.cumulationBuffer!.readerIndex, 0)
// Now we're going to send in one more byte. This will cause a chunk to be passed on,
// shrinking the held memory to 3072 bytes. However, memory will be reclaimed.
XCTAssertTrue(try channel.writeInbound(buffer.getSlice(at: 0, length: 1)))
XCTAssertTrue(try channel.writeInbound(buffer.getSlice(at: 0, length: 1)).isFull)
XCTAssertEqual(decoder.cumulationBuffer!.readableBytes, 3072)
XCTAssertEqual(decoder.cumulationBuffer!.readerIndex, 0)
}
@@ -296,17 +296,17 @@ public class ByteToMessageDecoderTest: XCTestCase {
var inputBuffer = channel.allocator.buffer(capacity: 4)
/* 1 */
inputBuffer.writeStaticString("1")
XCTAssertTrue(try channel.writeInbound(inputBuffer))
XCTAssertTrue(try channel.writeInbound(inputBuffer).isFull)
inputBuffer.clear()
/* 2 */
inputBuffer.writeStaticString("2")
XCTAssertTrue(try channel.writeInbound(inputBuffer))
XCTAssertTrue(try channel.writeInbound(inputBuffer).isFull)
inputBuffer.clear()
/* 3 */
inputBuffer.writeStaticString("5")
XCTAssertTrue(try channel.writeInbound(inputBuffer))
XCTAssertTrue(try channel.writeInbound(inputBuffer).isFull)
inputBuffer.clear()
func readOneInboundString() -> String {
@@ -433,7 +433,7 @@ public class ByteToMessageDecoderTest: XCTestCase {
let decoder = PairOfBytesDecoder(lastPromise: lastPromise)
let channel = EmbeddedChannel(handler: ByteToMessageHandler(decoder))
defer {
XCTAssertNoThrow(XCTAssertFalse(try channel.finish()))
XCTAssertNoThrow(XCTAssertTrue(try channel.finish().isClean))
}
var buffer = channel.allocator.buffer(capacity: 16)
@@ -708,7 +708,7 @@ public class ByteToMessageDecoderTest: XCTestCase {
let handler = ByteToMessageHandler(Decoder())
let channel = EmbeddedChannel(handler: handler)
defer {
XCTAssertNoThrow(XCTAssertFalse(try channel.finish()))
XCTAssertNoThrow(XCTAssertTrue(try channel.finish().isClean))
}
var buffer = channel.allocator.buffer(capacity: 9)
@@ -790,7 +790,7 @@ public class ByteToMessageDecoderTest: XCTestCase {
XCTAssertEqual(1, decoder.decodeLastCalls)
XCTAssertNoThrow(XCTAssertFalse(try channel.finish()))
XCTAssertNoThrow(XCTAssertTrue(try channel.finish().isClean))
XCTAssertNoThrow(XCTAssertNil(try channel.readInbound()))
XCTAssertEqual(1, decoder.decodeLastCalls)
@@ -884,7 +884,7 @@ public class ByteToMessageDecoderTest: XCTestCase {
var buffer = channel.allocator.buffer(capacity: 1)
buffer.writeString("x")
XCTAssertNoThrow(try channel.writeInbound(buffer))
XCTAssertNoThrow(XCTAssertFalse(try channel.finish()))
XCTAssertNoThrow(XCTAssertTrue(try channel.finish().isClean))
XCTAssertEqual(1, decoder.decodeLastCalls)
}
@@ -930,7 +930,7 @@ public class ByteToMessageDecoderTest: XCTestCase {
XCTAssertNoThrow(XCTAssertEqual(1, try channel.readOutbound()))
XCTAssertNoThrow(XCTAssertEqual(2, try channel.readOutbound()))
XCTAssertNoThrow(XCTAssertEqual(3, try channel.readOutbound()))
XCTAssertNoThrow(XCTAssertFalse(try channel.finish()))
XCTAssertNoThrow(XCTAssertTrue(try channel.finish().isClean))
}
func testWriteObservingByteToMessageDecoderWhereWriteIsReentrantlyCalled() {
@@ -1016,7 +1016,7 @@ public class ByteToMessageDecoderTest: XCTestCase {
XCTAssertNoThrow(XCTAssertEqual("O: 6: z @ 6", try channel.readOutbound()))
XCTAssertNoThrow(XCTAssertEqual("after @ 6", try channel.readOutbound()))
XCTAssertNoThrow(XCTAssertNil(try channel.readOutbound()))
XCTAssertNoThrow(XCTAssertFalse(try channel.finish()))
XCTAssertNoThrow(XCTAssertTrue(try channel.finish().isClean))
}
func testDecodeMethodsNoLongerCalledIfErrorInDecode() {
@@ -1060,7 +1060,7 @@ public class ByteToMessageDecoderTest: XCTestCase {
}
XCTAssertNoThrow(XCTAssertNil(try channel.readInbound()))
XCTAssertNoThrow(XCTAssertFalse(try channel.finish()))
XCTAssertNoThrow(XCTAssertTrue(try channel.finish().isClean))
}
func testDecodeMethodsNoLongerCalledIfErrorInDecodeLast() {
@@ -1229,7 +1229,7 @@ public class ByteToMessageDecoderTest: XCTestCase {
let decoder = CloseAfterThreeMessagesDecoder()
let channel = EmbeddedChannel(handler: ByteToMessageHandler(decoder))
defer {
XCTAssertNoThrow(XCTAssertFalse(try channel.finish()))
XCTAssertNoThrow(XCTAssertTrue(try channel.finish().isClean))
}
XCTAssertNoThrow(try channel.connect(to: SocketAddress(ipAddress: "1.2.3.4", port: 5678)).wait())
var buffer = channel.allocator.buffer(capacity: 16)
@@ -1348,7 +1348,7 @@ public class MessageToByteEncoderTest: XCTestCase {
XCTFail("couldn't read ByteBuffer from channel")
}
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
}
+30 -30
View File
@@ -21,8 +21,8 @@ class EmbeddedChannelTest: XCTestCase {
var buf = channel.allocator.buffer(capacity: 1024)
buf.writeString("hello")
XCTAssertTrue(try channel.writeOutbound(buf))
XCTAssertTrue(try channel.finish())
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()))
@@ -33,8 +33,8 @@ class EmbeddedChannelTest: XCTestCase {
var buf = channel.allocator.buffer(capacity: 1024)
buf.writeString("hello")
XCTAssertTrue(try channel.writeInbound(buf))
XCTAssertTrue(try channel.finish())
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()))
@@ -49,7 +49,7 @@ class EmbeddedChannelTest: XCTestCase {
} catch let err {
XCTAssertEqual(ChannelError.operationUnsupported, err as! ChannelError)
}
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func testWriteOutboundByteBufferReThrow() throws {
@@ -61,12 +61,12 @@ class EmbeddedChannelTest: XCTestCase {
} catch let err {
XCTAssertEqual(ChannelError.operationUnsupported, err as! ChannelError)
}
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func testReadOutboundWrongTypeThrows() {
let channel = EmbeddedChannel()
XCTAssertTrue(try channel.writeOutbound("hello"))
XCTAssertTrue(try channel.writeOutbound("hello").isFull)
do {
_ = try channel.readOutbound(as: Int.self)
XCTFail()
@@ -80,7 +80,7 @@ class EmbeddedChannelTest: XCTestCase {
func testReadInboundWrongTypeThrows() {
let channel = EmbeddedChannel()
XCTAssertTrue(try channel.writeInbound("hello"))
XCTAssertTrue(try channel.writeInbound("hello").isFull)
do {
_ = try channel.readInbound(as: Int.self)
XCTFail()
@@ -95,7 +95,7 @@ class EmbeddedChannelTest: XCTestCase {
func testWrongTypesWithFastpathTypes() {
let channel = EmbeddedChannel()
defer {
XCTAssertNoThrow(XCTAssertFalse(try channel.finish()))
XCTAssertNoThrow(XCTAssertTrue(try channel.finish().isClean))
}
let buffer = channel.allocator.buffer(capacity: 0)
@@ -106,28 +106,28 @@ class EmbeddedChannelTest: XCTestCase {
XCTAssertNoThrow(_ = try fileHandle.takeDescriptorOwnership())
}
XCTAssertTrue(try channel.writeOutbound(buffer))
XCTAssertTrue(try channel.writeOutbound(ioData))
XCTAssertTrue(try channel.writeOutbound(fileHandle))
XCTAssertTrue(try channel.writeOutbound(fileRegion))
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)))
XCTAssertTrue(try channel.writeOutbound(buffer))
XCTAssertTrue(try channel.writeOutbound(ioData))
XCTAssertTrue(try channel.writeOutbound(fileRegion))
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))
XCTAssertTrue(try channel.writeInbound(ioData))
XCTAssertTrue(try channel.writeInbound(fileHandle))
XCTAssertTrue(try channel.writeInbound(fileRegion))
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)))
XCTAssertTrue(try channel.writeInbound(buffer))
XCTAssertTrue(try channel.writeInbound(ioData))
XCTAssertTrue(try channel.writeInbound(fileRegion))
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,
@@ -166,7 +166,7 @@ class EmbeddedChannelTest: XCTestCase {
func testCloseMultipleTimesThrows() throws {
let channel = EmbeddedChannel()
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
// Close a second time. This must fail.
do {
@@ -181,7 +181,7 @@ class EmbeddedChannelTest: XCTestCase {
let channel = EmbeddedChannel()
let inactiveHandler = CloseInChannelInactiveHandler()
_ = try channel.pipeline.addHandler(inactiveHandler).wait()
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
// channelInactive should fire only once.
XCTAssertEqual(inactiveHandler.inactiveNotifications, 1)
@@ -200,7 +200,7 @@ class EmbeddedChannelTest: XCTestCase {
XCTAssertEqual(handler.currentState, .active)
XCTAssertTrue(channel.isActive)
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
XCTAssertEqual(handler.currentState, .unregistered)
XCTAssertFalse(channel.isActive)
}
@@ -238,7 +238,7 @@ class EmbeddedChannelTest: XCTestCase {
let pipelineEventLoop = channel.pipeline.eventLoop
XCTAssert(pipelineEventLoop === channel.eventLoop)
XCTAssert(pipelineEventLoop === (channel._channelCore as! EmbeddedChannelCore).eventLoop)
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
}
func testSendingAnythingOnEmbeddedChannel() throws {
@@ -289,7 +289,7 @@ class EmbeddedChannelTest: XCTestCase {
channel.flush()
XCTAssertNoThrow(XCTAssertNotNil(try channel.readOutbound(as: ByteBuffer.self)))
XCTAssertTrue(writeFuture.isFulfilled)
XCTAssertNoThrow(try XCTAssertFalse(channel.finish()))
XCTAssertNoThrow(XCTAssertTrue(try channel.finish().isClean))
}
func testSetLocalAddressAfterSuccessfulBind() throws {
+1 -1
View File
@@ -175,7 +175,7 @@ class IdleStateHandlerTest : XCTestCase {
channel.pipeline.fireChannelInactive()
channel.pipeline.fireChannelUnregistered()
XCTAssertFalse(try channel.finish())
XCTAssertTrue(try channel.finish().isClean)
eventHandler.assertAllEventsReceived()
}
}
+2 -2
View File
@@ -29,8 +29,8 @@ extension EmbeddedChannel {
func finishAcceptingAlreadyClosed() throws {
do {
let res = try self.finish()
XCTAssertFalse(res)
let result = try self.finish().isClean
XCTAssertTrue(result)
} catch ChannelError.alreadyClosed {
// ok
}
+1
View File
@@ -86,4 +86,5 @@
- change `ChannelPipeline.addHandler(_:after:)` to `ChannelPipeline.addHandler(_:postion:)` where `position` can be `.first`, `.last`, `.before(ChannelHandler)`, and `.after(ChannelHandler)`
- Change `HTTPServerProtocolUpgrader` `protocol` to require `buildUpgradeResponse` to take a `channel` and return an `EventLoopFuture<HTTPHeaders>`.
- `EmbeddedChannel.writeInbound/Outbound` are now `throwing`
- `EmbeddedChannel.finish/writeInbound/writeOutbound` now return an `enum` representation of their effects rather than mystery bools.
- `HTTPMethod.hasRequestBody` and the `HTTPMethod.HasBody` type have been removed from the public API