GRPCChannelHandlerResponseCapturingTestCase.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 XCTest
  21. import Logging
  22. class CollectingChannelHandler<OutboundIn>: ChannelOutboundHandler {
  23. var responses: [OutboundIn] = []
  24. func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
  25. promise?.succeed(())
  26. responses.append(unwrapOutboundIn(data))
  27. }
  28. }
  29. class CollectingServerErrorDelegate: ServerErrorDelegate {
  30. var errors: [Error] = []
  31. var asGRPCErrors: [GRPCError]? {
  32. return self.errors as? [GRPCError]
  33. }
  34. var asGRPCServerErrors: [GRPCServerError]? {
  35. return (self.asGRPCErrors?.map { $0.wrappedError }) as? [GRPCServerError]
  36. }
  37. var asGRPCCommonErrors: [GRPCCommonError]? {
  38. return (self.asGRPCErrors?.map { $0.wrappedError }) as? [GRPCCommonError]
  39. }
  40. func observeLibraryError(_ error: Error) {
  41. self.errors.append(error)
  42. }
  43. }
  44. class GRPCChannelHandlerResponseCapturingTestCase: GRPCTestCase {
  45. static let echoProvider: [String: CallHandlerProvider] = ["echo.Echo": EchoProvider()]
  46. class var defaultServiceProvider: [String: CallHandlerProvider] {
  47. return echoProvider
  48. }
  49. func configureChannel(withHandlers handlers: [ChannelHandler]) -> EventLoopFuture<EmbeddedChannel> {
  50. let channel = EmbeddedChannel()
  51. return channel.pipeline.addHandlers(handlers, position: .first)
  52. .map { _ in channel }
  53. }
  54. var errorCollector: CollectingServerErrorDelegate = CollectingServerErrorDelegate()
  55. /// Waits for `count` responses to be collected and then returns them. The test fails if the number
  56. /// of collected responses does not match the expected.
  57. ///
  58. /// - Parameters:
  59. /// - count: expected number of responses.
  60. /// - servicesByName: service providers keyed by their service name.
  61. /// - callback: a callback called after the channel has been setup, intended to "fill" the channel
  62. /// with messages. The callback is called before this function returns.
  63. /// - Returns: The responses collected from the pipeline.
  64. func waitForGRPCChannelHandlerResponses(
  65. count: Int,
  66. servicesByName: [String: CallHandlerProvider] = defaultServiceProvider,
  67. callback: @escaping (EmbeddedChannel) throws -> Void
  68. ) throws -> [RawGRPCServerResponsePart] {
  69. let collector = CollectingChannelHandler<RawGRPCServerResponsePart>()
  70. try configureChannel(withHandlers: [collector, GRPCChannelHandler(servicesByName: servicesByName, errorDelegate: errorCollector, logger: Logger(label: "io.grpc.testing"))])
  71. .flatMapThrowing(callback)
  72. .wait()
  73. XCTAssertEqual(count, collector.responses.count)
  74. return collector.responses
  75. }
  76. }