Skip to content

Commit

Permalink
Fail promise in MessageToByteHandler write error cases (#1096)
Browse files Browse the repository at this point in the history
* Fail promise in MessageToByteHandler write error cases

* Add test case for MessageToByteHandler failing write promises
  • Loading branch information
wlisac authored and Lukasa committed Aug 8, 2019
1 parent b564a72 commit 2658683
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sources/NIO/Codec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ extension MessageToByteHandler {
case .notInChannelYet:
preconditionFailure("MessageToByteHandler.write called before it was added to a Channel")
case .error(let error):
promise?.fail(error)
context.fireErrorCaught(error)
return
case .done:
Expand All @@ -784,6 +785,7 @@ extension MessageToByteHandler {
context.write(self.wrapOutboundOut(self.buffer!), promise: promise)
} catch {
self.state = .error(error)
promise?.fail(error)
context.fireErrorCaught(error)
}
}
Expand Down
1 change: 1 addition & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import XCTest
testCase(IntegerTypesTest.allTests),
testCase(MarkedCircularBufferTests.allTests),
testCase(MessageToByteEncoderTest.allTests),
testCase(MessageToByteHandlerTest.allTests),
testCase(MulticastTest.allTests),
testCase(NIOAnyDebugTest.allTests),
testCase(NIOCloseOnErrorHandlerTest.allTests),
Expand Down
9 changes: 9 additions & 0 deletions Tests/NIOTests/CodecTest+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,12 @@ extension MessageToByteEncoderTest {
}
}

extension MessageToByteHandlerTest {

static var allTests : [(String, (MessageToByteHandlerTest) -> () throws -> Void)] {
return [
("testThrowingEncoderFailsPromises", testThrowingEncoderFailsPromises),
]
}
}

28 changes: 28 additions & 0 deletions Tests/NIOTests/CodecTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1587,3 +1587,31 @@ private class PairOfBytesDecoder: ByteToMessageDecoder {
return .needMoreData
}
}

public final class MessageToByteHandlerTest: XCTestCase {
private struct ThrowingMessageToByteEncoder: MessageToByteEncoder {
private struct HandlerError: Error { }

typealias OutboundIn = Int

public func encode(data value: Int, out: inout ByteBuffer) throws {
if value == 0 {
out.writeInteger(value)
} else {
throw HandlerError()
}
}
}

func testThrowingEncoderFailsPromises() {
let channel = EmbeddedChannel()

XCTAssertNoThrow(try channel.pipeline.addHandler(MessageToByteHandler(ThrowingMessageToByteEncoder())).wait())

XCTAssertNoThrow(try channel.writeAndFlush(0).wait())

XCTAssertThrowsError(try channel.writeAndFlush(1).wait())

XCTAssertThrowsError(try channel.writeAndFlush(0).wait())
}
}

0 comments on commit 2658683

Please sign in to comment.