ImmediateServerFailureTests.swift 3.3 KB

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