GRPCChannelHandlerResponseCapturingTestCase.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 2019, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. import NIO
  18. import NIOHTTP1
  19. @testable import GRPC
  20. import EchoModel
  21. import EchoImplementation
  22. import XCTest
  23. import Logging
  24. class CollectingChannelHandler<OutboundIn>: ChannelOutboundHandler {
  25. var responses: [OutboundIn] = []
  26. func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
  27. promise?.succeed(())
  28. responses.append(unwrapOutboundIn(data))
  29. }
  30. }
  31. class CollectingServerErrorDelegate: ServerErrorDelegate {
  32. var errors: [Error] = []
  33. func observeLibraryError(_ error: Error) {
  34. self.errors.append(error)
  35. }
  36. }
  37. class GRPCChannelHandlerResponseCapturingTestCase: GRPCTestCase {
  38. static let echoProvider: [String: CallHandlerProvider] = ["echo.Echo": EchoProvider()]
  39. class var defaultServiceProvider: [String: CallHandlerProvider] {
  40. return echoProvider
  41. }
  42. func configureChannel(withHandlers handlers: [ChannelHandler]) -> EventLoopFuture<EmbeddedChannel> {
  43. let channel = EmbeddedChannel()
  44. return channel.pipeline.addHandlers(handlers, position: .first)
  45. .map { _ in channel }
  46. }
  47. var errorCollector: CollectingServerErrorDelegate = CollectingServerErrorDelegate()
  48. /// Waits for `count` responses to be collected and then returns them. The test fails if the number
  49. /// of collected responses does not match the expected.
  50. ///
  51. /// - Parameters:
  52. /// - count: expected number of responses.
  53. /// - servicesByName: service providers keyed by their service name.
  54. /// - callback: a callback called after the channel has been setup, intended to "fill" the channel
  55. /// with messages. The callback is called before this function returns.
  56. /// - Returns: The responses collected from the pipeline.
  57. func waitForGRPCChannelHandlerResponses(
  58. count: Int,
  59. servicesByName: [String: CallHandlerProvider] = defaultServiceProvider,
  60. callback: @escaping (EmbeddedChannel) throws -> Void
  61. ) throws -> [_RawGRPCServerResponsePart] {
  62. let collector = CollectingChannelHandler<_RawGRPCServerResponsePart>()
  63. try configureChannel(withHandlers: [collector, GRPCChannelHandler(servicesByName: servicesByName, errorDelegate: errorCollector, logger: Logger(label: "io.grpc.testing"))])
  64. .flatMapThrowing(callback)
  65. .wait()
  66. XCTAssertEqual(count, collector.responses.count)
  67. return collector.responses
  68. }
  69. }