Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API breaking] Add types to inbound/outbound debug handlers #81

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Sources/NIOExtras/DebugInboundEventsHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ import NIO

/// ChannelInboundHandler that prints all inbound events that pass through the pipeline by default,
/// overridable by providing your own closure for custom logging. See DebugOutboundEventsHandler for outbound events.
public class DebugInboundEventsHandler: ChannelInboundHandler {
public class DebugInboundEventsHandler<T>: ChannelInboundHandler {

public typealias InboundIn = Any
public typealias InboudOut = Any
public typealias InboundIn = T
public typealias InboudOut = T

public enum Event {
case registered
case unregistered
case active
case inactive
case read(data: NIOAny)
case read(data: T)
case readComplete
case writabilityChanged(isWritable: Bool)
case userInboundEventTriggered(event: Any)
Expand Down Expand Up @@ -60,7 +60,7 @@ public class DebugInboundEventsHandler: ChannelInboundHandler {
}

public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
logger(.read(data: data), context)
logger(.read(data: self.unwrapInboundIn(data)), context)
context.fireChannelRead(data)
}

Expand Down
10 changes: 5 additions & 5 deletions Sources/NIOExtras/DebugOutboundEventsHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ import NIO

/// ChannelOutboundHandler that prints all outbound events that pass through the pipeline by default,
/// overridable by providing your own closure for custom logging. See DebugInboundEventsHandler for inbound events.
public class DebugOutboundEventsHandler: ChannelOutboundHandler {
public class DebugOutboundEventsHandler<T>: ChannelOutboundHandler {

public typealias OutboundIn = Any
public typealias OutboundOut = Any
public typealias OutboundIn = T
public typealias OutboundOut = T

public enum Event {
case register
case bind(address: SocketAddress)
case connect(address: SocketAddress)
case write(data: NIOAny)
case write(data: T)
case flush
case read
case close(mode: CloseMode)
Expand Down Expand Up @@ -54,7 +54,7 @@ public class DebugOutboundEventsHandler: ChannelOutboundHandler {
}

public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
logger(.write(data: data), context)
logger(.write(data: self.unwrapOutboundIn(data)), context)
context.write(data, promise: promise)
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/NIOExtrasTests/DebugInboundEventsHandlerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import NIOExtras
class DebugInboundEventsHandlerTest: XCTestCase {

private var channel: EmbeddedChannel!
private var lastEvent: DebugInboundEventsHandler.Event!
private var handlerUnderTest: DebugInboundEventsHandler!
private var lastEvent: DebugInboundEventsHandler<ByteBuffer>.Event!
private var handlerUnderTest: DebugInboundEventsHandler<ByteBuffer>!

override func setUp() {
super.setUp()
Expand Down Expand Up @@ -91,7 +91,7 @@ class DebugInboundEventsHandlerTest: XCTestCase {
expectedBuffer.setString(messageString, at: 0)
let nioAny = NIOAny(expectedBuffer)
channel.pipeline.fireChannelRead(nioAny)
XCTAssertEqual(lastEvent, .read(data: nioAny))
XCTAssertEqual(lastEvent, .read(data: expectedBuffer))
}

}
Expand Down
13 changes: 8 additions & 5 deletions Tests/NIOExtrasTests/DebugOutboundEventsHandlerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import NIOExtras
class DebugOutboundEventsHandlerTest: XCTestCase {

private var channel: EmbeddedChannel!
private var lastEvent: DebugOutboundEventsHandler.Event!
private var handlerUnderTest: DebugOutboundEventsHandler!
private var lastEvent: DebugOutboundEventsHandler<ByteBuffer>.Event!
private var handlerUnderTest: DebugOutboundEventsHandler<ByteBuffer>!

override func setUp() {
super.setUp()
Expand Down Expand Up @@ -56,9 +56,12 @@ class DebugOutboundEventsHandlerTest: XCTestCase {
}

func testWrite() {
let data = NIOAny(" 1 2 3 ")
channel.write(data, promise: nil)
XCTAssertEqual(lastEvent, .write(data: data))
let messageString = "message"
var expectedBuffer = ByteBufferAllocator().buffer(capacity: messageString.count)
expectedBuffer.setString(messageString, at: 0)
let nioAny = NIOAny(expectedBuffer)
channel.write(nioAny, promise: nil)
XCTAssertEqual(lastEvent, .write(data: expectedBuffer))
}

func testFlush() {
Expand Down