ServerThrowingTests.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright 2018, 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 Dispatch
  17. import Foundation
  18. import NIO
  19. import NIOHTTP1
  20. import NIOHTTP2
  21. @testable import SwiftGRPCNIO
  22. import XCTest
  23. private let expectedError = GRPCStatus(code: .internalError, message: "expected error")
  24. // Motivation for two different providers: Throwing immediately causes the event observer future (in the
  25. // client-streaming and bidi-streaming cases) to throw immediately, _before_ the corresponding handler has even added
  26. // to the channel. We want to test that case as well as the one where we throw only _after_ the handler has been added
  27. // to the channel.
  28. private class ImmediateThrowingEchoProviderNIO: Echo_EchoProvider_NIO {
  29. func get(request: Echo_EchoRequest, context: StatusOnlyCallContext) -> EventLoopFuture<Echo_EchoResponse> {
  30. return context.eventLoop.newFailedFuture(error: expectedError)
  31. }
  32. func expand(request: Echo_EchoRequest, context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<GRPCStatus> {
  33. return context.eventLoop.newFailedFuture(error: expectedError)
  34. }
  35. func collect(context: UnaryResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  36. return context.eventLoop.newFailedFuture(error: expectedError)
  37. }
  38. func update(context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  39. return context.eventLoop.newFailedFuture(error: expectedError)
  40. }
  41. }
  42. private extension EventLoop {
  43. func newFailedFuture<T>(error: Error, delay: TimeInterval) -> EventLoopFuture<T> {
  44. return self.scheduleTask(in: .nanoseconds(TimeAmount.Value(delay * 1000 * 1000 * 1000))) { () }.futureResult
  45. .thenThrowing { _ -> T in throw error }
  46. }
  47. }
  48. /// See `ImmediateThrowingEchoProviderNIO`.
  49. private class DelayedThrowingEchoProviderNIO: Echo_EchoProvider_NIO {
  50. func get(request: Echo_EchoRequest, context: StatusOnlyCallContext) -> EventLoopFuture<Echo_EchoResponse> {
  51. return context.eventLoop.newFailedFuture(error: expectedError, delay: 0.01)
  52. }
  53. func expand(request: Echo_EchoRequest, context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<GRPCStatus> {
  54. return context.eventLoop.newFailedFuture(error: expectedError, delay: 0.01)
  55. }
  56. func collect(context: UnaryResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  57. return context.eventLoop.newFailedFuture(error: expectedError, delay: 0.01)
  58. }
  59. func update(context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  60. return context.eventLoop.newFailedFuture(error: expectedError, delay: 0.01)
  61. }
  62. }
  63. /// Ensures that fulfilling the status promise (where possible) with an error yields the same result as failing the future.
  64. private class ErrorReturningEchoProviderNIO: ImmediateThrowingEchoProviderNIO {
  65. // There's no status promise to fulfill for unary calls (only the response promise), so that case is omitted.
  66. override func expand(request: Echo_EchoRequest, context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<GRPCStatus> {
  67. return context.eventLoop.newSucceededFuture(result: expectedError)
  68. }
  69. override func collect(context: UnaryResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  70. return context.eventLoop.newSucceededFuture(result: { _ in
  71. context.responseStatus = expectedError
  72. context.responsePromise.succeed(result: Echo_EchoResponse())
  73. })
  74. }
  75. override func update(context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
  76. return context.eventLoop.newSucceededFuture(result: { _ in
  77. context.statusPromise.succeed(result: expectedError)
  78. })
  79. }
  80. }
  81. class ServerThrowingTests: NIOBasicEchoTestCase {
  82. override func makeEchoProvider() -> Echo_EchoProvider_NIO { return ImmediateThrowingEchoProviderNIO() }
  83. static var allTests: [(String, (ServerThrowingTests) -> () throws -> Void)] {
  84. return [
  85. ("testUnary", testUnary),
  86. ("testClientStreaming", testClientStreaming),
  87. ("testServerStreaming", testServerStreaming),
  88. ("testBidirectionalStreaming", testBidirectionalStreaming),
  89. ]
  90. }
  91. }
  92. class ServerDelayedThrowingTests: ServerThrowingTests {
  93. override func makeEchoProvider() -> Echo_EchoProvider_NIO { return DelayedThrowingEchoProviderNIO() }
  94. }
  95. class ClientThrowingWhenServerReturningErrorTests: ServerThrowingTests {
  96. override func makeEchoProvider() -> Echo_EchoProvider_NIO { return ErrorReturningEchoProviderNIO() }
  97. }
  98. extension ServerThrowingTests {
  99. func testUnary() throws {
  100. let call = client.get(Echo_EchoRequest(text: "foo"))
  101. XCTAssertEqual(expectedError, try call.status.wait())
  102. XCTAssertThrowsError(try call.response.wait()) {
  103. XCTAssertEqual(expectedError, $0 as? GRPCStatus)
  104. }
  105. }
  106. func testClientStreaming() {
  107. let call = client.collect()
  108. XCTAssertNoThrow(try call.sendEnd().wait())
  109. XCTAssertEqual(expectedError, try call.status.wait())
  110. if type(of: makeEchoProvider()) != ErrorReturningEchoProviderNIO.self {
  111. // With `ErrorReturningEchoProviderNIO` we actually _return_ a response, which means that the `response` future
  112. // will _not_ fail, so in that case this test doesn't apply.
  113. XCTAssertThrowsError(try call.response.wait()) {
  114. XCTAssertEqual(expectedError, $0 as? GRPCStatus)
  115. }
  116. }
  117. }
  118. func testServerStreaming() {
  119. let call = client.expand(Echo_EchoRequest(text: "foo")) { XCTFail("no message expected, got \($0)") }
  120. // Nothing to throw here, but the `status` should be the expected error.
  121. XCTAssertEqual(expectedError, try call.status.wait())
  122. }
  123. func testBidirectionalStreaming() {
  124. let call = client.update() { XCTFail("no message expected, got \($0)") }
  125. XCTAssertNoThrow(try call.sendEnd().wait())
  126. // Nothing to throw here, but the `status` should be the expected error.
  127. XCTAssertEqual(expectedError, try call.status.wait())
  128. }
  129. }