Skip to content

Instantly share code, notes, and snippets.

@weissi
Last active April 9, 2019 11:23
Show Gist options
  • Save weissi/0888e6e5a62f474b64acf484e5d5c2b8 to your computer and use it in GitHub Desktop.
Save weissi/0888e6e5a62f474b64acf484e5d5c2b8 to your computer and use it in GitHub Desktop.
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 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
//
//===----------------------------------------------------------------------===//
class CounterHandler: ChannelDuplexHandler {
typealias InboundIn = Any
typealias InboundOut = Any
typealias OutboundIn = Any
typealias OutboundOut = Any
var channelRegisteredCalls = 0
var channelUnregisteredCalls = 0
var channelActiveCalls = 0
var channelInactiveCalls = 0
var channelReadCalls = 0
var channelReadCompleteCalls = 0
var channelWritabilityChangedCalls = 0
var userInboundEventTriggeredCalls = 0
var errorCaughtCalls = 0
var registerCalls = 0
var bindCalls = 0
var connectCalls = 0
var writeCalls = 0
var flushCalls = 0
var readCalls = 0
var closeCalls = 0
var triggerUserOutboundEventCalls = 0
func channelRegistered(context: ChannelHandlerContext) {
self.channelRegisteredCalls += 1
XCTAssertEqual(1, self.channelRegisteredCalls, "\(#function) should only ever be called once")
context.fireChannelRegistered()
}
func channelUnregistered(context: ChannelHandlerContext) {
self.channelUnregisteredCalls += 1
XCTAssertEqual(1, self.channelUnregisteredCalls, "\(#function) should only ever be called once")
context.fireChannelUnregistered()
}
func channelActive(context: ChannelHandlerContext) {
self.channelActiveCalls += 1
XCTAssertEqual(1, self.channelActiveCalls, "\(#function) should only ever be called once")
context.fireChannelActive()
}
func channelInactive(context: ChannelHandlerContext) {
self.channelInactiveCalls += 1
XCTAssertEqual(1, self.channelInactiveCalls, "\(#function) should only ever be called once")
context.fireChannelInactive()
}
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
self.channelReadCalls += 1
context.fireChannelRead(data)
}
func channelReadComplete(context: ChannelHandlerContext) {
self.channelReadCompleteCalls += 1
context.fireChannelReadComplete()
}
func channelWritabilityChanged(context: ChannelHandlerContext) {
self.channelWritabilityChangedCalls += 1
context.fireChannelWritabilityChanged()
}
func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
self.userInboundEventTriggeredCalls += 1
context.fireUserInboundEventTriggered(event)
}
func errorCaught(context: ChannelHandlerContext, error: Error) {
self.errorCaughtCalls += 1
context.fireErrorCaught(error)
}
func register(context: ChannelHandlerContext, promise: EventLoopPromise<Void>?) {
self.registerCalls += 1
XCTAssertEqual(1, self.registerCalls, "\(#function) should only ever be called once")
context.register(promise: promise)
}
func bind(context: ChannelHandlerContext, to: SocketAddress, promise: EventLoopPromise<Void>?) {
self.bindCalls += 1
XCTAssertEqual(1, self.bindCalls, "\(#function) should only ever be called once")
context.bind(to: to, promise: promise)
}
func connect(context: ChannelHandlerContext, to: SocketAddress, promise: EventLoopPromise<Void>?) {
self.connectCalls += 1
XCTAssertEqual(1, self.connectCalls, "\(#function) should only ever be called once")
context.connect(to: to, promise: promise)
}
func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
self.writeCalls += 1
context.write(data, promise: promise)
}
func flush(context: ChannelHandlerContext) {
self.flushCalls += 1
context.flush()
}
func read(context: ChannelHandlerContext) {
self.readCalls += 1
context.read()
}
func close(context: ChannelHandlerContext, mode: CloseMode, promise: EventLoopPromise<Void>?) {
self.closeCalls += 1
XCTAssertEqual(1, self.closeCalls, "\(#function) should only ever be called once")
context.close(mode: mode, promise: promise)
}
func triggerUserOutboundEvent(context: ChannelHandlerContext, event: Any, promise: EventLoopPromise<Void>?) {
self.triggerUserOutboundEventCalls += 1
context.triggerUserOutboundEvent(event, promise: promise)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment