2
0

ImmediateServerFailureTests.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 EchoModel
  17. import Foundation
  18. import GRPC
  19. import NIOCore
  20. import XCTest
  21. class ImmediatelyFailingEchoProvider: Echo_EchoProvider {
  22. let interceptors: Echo_EchoServerInterceptorFactoryProtocol? = nil
  23. static let status: GRPCStatus = .init(code: .unavailable, message: nil)
  24. func get(
  25. request: Echo_EchoRequest,
  26. context: StatusOnlyCallContext
  27. ) -> EventLoopFuture<Echo_EchoResponse> {
  28. return context.eventLoop.makeFailedFuture(ImmediatelyFailingEchoProvider.status)
  29. }
  30. func expand(
  31. request: Echo_EchoRequest,
  32. context: StreamingResponseCallContext<Echo_EchoResponse>
  33. ) -> EventLoopFuture<GRPCStatus> {
  34. return context.eventLoop.makeFailedFuture(ImmediatelyFailingEchoProvider.status)
  35. }
  36. func collect(
  37. context: UnaryResponseCallContext<Echo_EchoResponse>
  38. ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  39. context.responsePromise.fail(ImmediatelyFailingEchoProvider.status)
  40. return context.eventLoop.makeSucceededFuture({ _ in
  41. // no-op
  42. })
  43. }
  44. func update(
  45. context: StreamingResponseCallContext<Echo_EchoResponse>
  46. ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  47. context.statusPromise.fail(ImmediatelyFailingEchoProvider.status)
  48. return context.eventLoop.makeSucceededFuture({ _ in
  49. // no-op
  50. })
  51. }
  52. }
  53. class ImmediatelyFailingProviderTests: EchoTestCaseBase {
  54. override func makeEchoProvider() -> Echo_EchoProvider {
  55. return ImmediatelyFailingEchoProvider()
  56. }
  57. func testUnary() throws {
  58. let expcectation = self.makeStatusExpectation()
  59. let call = self.client.get(Echo_EchoRequest(text: "foo"))
  60. call.status.map { $0.code }.assertEqual(.unavailable, fulfill: expcectation)
  61. self.wait(for: [expcectation], timeout: self.defaultTestTimeout)
  62. }
  63. func testServerStreaming() throws {
  64. let expcectation = self.makeStatusExpectation()
  65. let call = self.client.expand(Echo_EchoRequest(text: "foo")) { response in
  66. XCTFail("unexpected response: \(response)")
  67. }
  68. call.status.map { $0.code }.assertEqual(.unavailable, fulfill: expcectation)
  69. self.wait(for: [expcectation], timeout: self.defaultTestTimeout)
  70. }
  71. func testClientStreaming() throws {
  72. let expcectation = self.makeStatusExpectation()
  73. let call = self.client.collect()
  74. call.status.map { $0.code }.assertEqual(.unavailable, fulfill: expcectation)
  75. self.wait(for: [expcectation], timeout: self.defaultTestTimeout)
  76. }
  77. func testBidirectionalStreaming() throws {
  78. let expcectation = self.makeStatusExpectation()
  79. let call = self.client.update { response in
  80. XCTFail("unexpected response: \(response)")
  81. }
  82. call.status.map { $0.code }.assertEqual(.unavailable, fulfill: expcectation)
  83. self.wait(for: [expcectation], timeout: self.defaultTestTimeout)
  84. }
  85. }