ImmediateServerFailureTests.swift 3.3 KB

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