SHARE
TWEET

Untitled

a guest Apr 19th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // This source file is part of the SwiftNIO open source project
  4. //
  5. // Copyright (c) 2019 Apple Inc. and the SwiftNIO project authors
  6. // Licensed under Apache License v2.0
  7. //
  8. // See LICENSE.txt for license information
  9. // See CONTRIBUTORS.txt for the list of SwiftNIO project authors
  10. //
  11. // SPDX-License-Identifier: Apache-2.0
  12. //
  13. //===----------------------------------------------------------------------===//
  14.  
  15. class CounterHandler: ChannelDuplexHandler {
  16.     typealias InboundIn = Any
  17.     typealias InboundOut = Any
  18.     typealias OutboundIn = Any
  19.     typealias OutboundOut = Any
  20.    
  21.     var channelRegisteredCalls = 0
  22.     var channelUnregisteredCalls = 0
  23.     var channelActiveCalls = 0
  24.     var channelInactiveCalls = 0
  25.     var channelReadCalls = 0
  26.     var channelReadCompleteCalls = 0
  27.     var channelWritabilityChangedCalls = 0
  28.     var userInboundEventTriggeredCalls = 0
  29.     var errorCaughtCalls = 0
  30.     var registerCalls = 0
  31.     var bindCalls = 0
  32.     var connectCalls = 0
  33.     var writeCalls = 0
  34.     var flushCalls = 0
  35.     var readCalls = 0
  36.     var closeCalls = 0
  37.     var triggerUserOutboundEventCalls = 0
  38.    
  39.     func channelRegistered(context: ChannelHandlerContext) {
  40.         self.channelRegisteredCalls += 1
  41.         XCTAssertEqual(1, self.channelRegisteredCalls, "\(#function) should only ever be called once")
  42.         context.fireChannelRegistered()
  43.     }
  44.     func channelUnregistered(context: ChannelHandlerContext) {
  45.         self.channelUnregisteredCalls += 1
  46.         XCTAssertEqual(1, self.channelUnregisteredCalls, "\(#function) should only ever be called once")
  47.         context.fireChannelUnregistered()
  48.     }
  49.     func channelActive(context: ChannelHandlerContext) {
  50.         self.channelActiveCalls += 1
  51.         XCTAssertEqual(1, self.channelActiveCalls, "\(#function) should only ever be called once")
  52.         context.fireChannelActive()
  53.     }
  54.     func channelInactive(context: ChannelHandlerContext) {
  55.         self.channelInactiveCalls += 1
  56.         XCTAssertEqual(1, self.channelInactiveCalls, "\(#function) should only ever be called once")
  57.         context.fireChannelInactive()
  58.     }
  59.     func channelRead(context: ChannelHandlerContext, data: NIOAny) {
  60.         self.channelReadCalls += 1
  61.         context.fireChannelRead(data)
  62.     }
  63.     func channelReadComplete(context: ChannelHandlerContext) {
  64.         self.channelReadCompleteCalls += 1
  65.         context.fireChannelReadComplete()
  66.     }
  67.     func channelWritabilityChanged(context: ChannelHandlerContext) {
  68.         self.channelWritabilityChangedCalls += 1
  69.         context.fireChannelWritabilityChanged()
  70.     }
  71.     func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
  72.         self.userInboundEventTriggeredCalls += 1
  73.         context.fireUserInboundEventTriggered(event)
  74.     }
  75.     func errorCaught(context: ChannelHandlerContext, error: Error) {
  76.         self.errorCaughtCalls += 1
  77.         context.fireErrorCaught(error)
  78.     }
  79.     func register(context: ChannelHandlerContext, promise: EventLoopPromise<Void>?) {
  80.         self.registerCalls += 1
  81.         XCTAssertEqual(1, self.registerCalls, "\(#function) should only ever be called once")
  82.         context.register(promise: promise)
  83.     }
  84.     func bind(context: ChannelHandlerContext, to: SocketAddress, promise: EventLoopPromise<Void>?) {
  85.         self.bindCalls += 1
  86.         XCTAssertEqual(1, self.bindCalls, "\(#function) should only ever be called once")
  87.         context.bind(to: to, promise: promise)
  88.     }
  89.     func connect(context: ChannelHandlerContext, to: SocketAddress, promise: EventLoopPromise<Void>?) {
  90.         self.connectCalls += 1
  91.         XCTAssertEqual(1, self.connectCalls, "\(#function) should only ever be called once")
  92.         context.connect(to: to, promise: promise)
  93.     }
  94.     func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
  95.         self.writeCalls += 1
  96.         context.write(data, promise: promise)
  97.     }
  98.     func flush(context: ChannelHandlerContext) {
  99.         self.flushCalls += 1
  100.         context.flush()
  101.     }
  102.     func read(context: ChannelHandlerContext) {
  103.         self.readCalls += 1
  104.         context.read()
  105.     }
  106.     func close(context: ChannelHandlerContext, mode: CloseMode, promise: EventLoopPromise<Void>?) {
  107.         self.closeCalls += 1
  108.         XCTAssertEqual(1, self.closeCalls, "\(#function) should only ever be called once")
  109.         context.close(mode: mode, promise: promise)
  110.     }
  111.     func triggerUserOutboundEvent(context: ChannelHandlerContext, event: Any, promise: EventLoopPromise<Void>?) {
  112.         self.triggerUserOutboundEventCalls += 1
  113.         context.triggerUserOutboundEvent(event, promise: promise)
  114.     }
  115. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top