| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- /*
- * Copyright 2018, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- import Dispatch
- import EchoModel
- import Foundation
- import NIOCore
- import NIOHPACK
- import NIOHTTP1
- import NIOHTTP2
- import XCTest
- @testable import GRPC
- let thrownError = GRPCStatus(code: .internalError, message: "expected error")
- let transformedError = GRPCStatus(code: .aborted, message: "transformed error")
- let transformedMetadata = HPACKHeaders([("transformed", "header")])
- // Motivation for two different providers: Throwing immediately causes the event observer future (in the
- // client-streaming and bidi-streaming cases) to throw immediately, _before_ the corresponding handler has even added
- // to the channel. We want to test that case as well as the one where we throw only _after_ the handler has been added
- // to the channel.
- class ImmediateThrowingEchoProvider: Echo_EchoProvider {
- var interceptors: Echo_EchoServerInterceptorFactoryProtocol? { return nil }
- func get(
- request: Echo_EchoRequest,
- context: StatusOnlyCallContext
- ) -> EventLoopFuture<Echo_EchoResponse> {
- return context.eventLoop.makeFailedFuture(thrownError)
- }
- func expand(
- request: Echo_EchoRequest,
- context: StreamingResponseCallContext<Echo_EchoResponse>
- ) -> EventLoopFuture<GRPCStatus> {
- return context.eventLoop.makeFailedFuture(thrownError)
- }
- func collect(
- context: UnaryResponseCallContext<Echo_EchoResponse>
- ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
- return context.eventLoop.makeFailedFuture(thrownError)
- }
- func update(
- context: StreamingResponseCallContext<Echo_EchoResponse>
- ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
- return context.eventLoop.makeFailedFuture(thrownError)
- }
- }
- extension EventLoop {
- func makeFailedFuture<T>(_ error: Error, delay: TimeInterval) -> EventLoopFuture<T> {
- return self.scheduleTask(in: .nanoseconds(Int64(delay * 1000 * 1000 * 1000))) { () }
- .futureResult
- .flatMapThrowing { _ -> T in throw error }
- }
- }
- /// See `ImmediateThrowingEchoProvider`.
- class DelayedThrowingEchoProvider: Echo_EchoProvider {
- let interceptors: Echo_EchoServerInterceptorFactoryProtocol? = nil
- func get(
- request: Echo_EchoRequest,
- context: StatusOnlyCallContext
- ) -> EventLoopFuture<Echo_EchoResponse> {
- return context.eventLoop.makeFailedFuture(thrownError, delay: 0.01)
- }
- func expand(
- request: Echo_EchoRequest,
- context: StreamingResponseCallContext<Echo_EchoResponse>
- ) -> EventLoopFuture<GRPCStatus> {
- return context.eventLoop.makeFailedFuture(thrownError, delay: 0.01)
- }
- func collect(
- context: UnaryResponseCallContext<Echo_EchoResponse>
- ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
- return context.eventLoop.makeFailedFuture(thrownError, delay: 0.01)
- }
- func update(
- context: StreamingResponseCallContext<Echo_EchoResponse>
- ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
- return context.eventLoop.makeFailedFuture(thrownError, delay: 0.01)
- }
- }
- /// Ensures that fulfilling the status promise (where possible) with an error yields the same result as failing the future.
- class ErrorReturningEchoProvider: ImmediateThrowingEchoProvider {
- // There's no status promise to fulfill for unary calls (only the response promise), so that case is omitted.
- override func expand(
- request: Echo_EchoRequest,
- context: StreamingResponseCallContext<Echo_EchoResponse>
- ) -> EventLoopFuture<GRPCStatus> {
- return context.eventLoop.makeSucceededFuture(thrownError)
- }
- override func collect(
- context: UnaryResponseCallContext<Echo_EchoResponse>
- ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
- return context.eventLoop.makeSucceededFuture({ _ in
- context.responseStatus = thrownError
- context.responsePromise.succeed(Echo_EchoResponse())
- })
- }
- override func update(
- context: StreamingResponseCallContext<Echo_EchoResponse>
- ) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> {
- return context.eventLoop.makeSucceededFuture({ _ in
- context.statusPromise.succeed(thrownError)
- })
- }
- }
- private class ErrorTransformingDelegate: ServerErrorDelegate {
- func transformRequestHandlerError(
- _ error: Error,
- headers: HPACKHeaders
- ) -> GRPCStatusAndTrailers? {
- return GRPCStatusAndTrailers(status: transformedError, trailers: transformedMetadata)
- }
- }
- class ServerThrowingTests: EchoTestCaseBase {
- var expectedError: GRPCStatus { return thrownError }
- var expectedMetadata: HPACKHeaders? {
- return HPACKHeaders([("grpc-status", "13"), ("grpc-message", "expected error")])
- }
- override func makeEchoProvider() -> Echo_EchoProvider { return ImmediateThrowingEchoProvider() }
- func testUnary() throws {
- let call = client.get(Echo_EchoRequest(text: "foo"))
- XCTAssertEqual(self.expectedError, try call.status.wait())
- let trailers = try call.trailingMetadata.wait()
- if let expected = self.expectedMetadata {
- for (name, value, _) in expected {
- XCTAssertTrue(trailers[name].contains(value))
- }
- }
- XCTAssertThrowsError(try call.response.wait()) {
- XCTAssertEqual(self.expectedError, $0 as? GRPCStatus)
- }
- }
- func testClientStreaming() throws {
- let call = client.collect()
- // This is racing with the server error; it might fail, it might not.
- try? call.sendEnd().wait()
- XCTAssertEqual(self.expectedError, try call.status.wait())
- let trailers = try call.trailingMetadata.wait()
- if let expected = self.expectedMetadata {
- for (name, value, _) in expected {
- XCTAssertTrue(trailers[name].contains(value))
- }
- }
- if type(of: self.makeEchoProvider()) != ErrorReturningEchoProvider.self {
- // With `ErrorReturningEchoProvider` we actually _return_ a response, which means that the `response` future
- // will _not_ fail, so in that case this test doesn't apply.
- XCTAssertThrowsError(try call.response.wait()) {
- XCTAssertEqual(self.expectedError, $0 as? GRPCStatus)
- }
- }
- }
- func testServerStreaming() throws {
- let call = client.expand(
- Echo_EchoRequest(text: "foo")
- ) {
- XCTFail("no message expected, got \($0)")
- }
- // Nothing to throw here, but the `status` should be the expected error.
- XCTAssertEqual(self.expectedError, try call.status.wait())
- let trailers = try call.trailingMetadata.wait()
- if let expected = self.expectedMetadata {
- for (name, value, _) in expected {
- XCTAssertTrue(trailers[name].contains(value))
- }
- }
- }
- func testBidirectionalStreaming() throws {
- let call = client.update { XCTFail("no message expected, got \($0)") }
- // This is racing with the server error; it might fail, it might not.
- try? call.sendEnd().wait()
- // Nothing to throw here, but the `status` should be the expected error.
- XCTAssertEqual(self.expectedError, try call.status.wait())
- let trailers = try call.trailingMetadata.wait()
- if let expected = self.expectedMetadata {
- for (name, value, _) in expected {
- XCTAssertTrue(trailers[name].contains(value))
- }
- }
- }
- }
- class ServerDelayedThrowingTests: ServerThrowingTests {
- override func makeEchoProvider() -> Echo_EchoProvider { return DelayedThrowingEchoProvider() }
- override func testUnary() throws {
- try super.testUnary()
- }
- override func testClientStreaming() throws {
- try super.testClientStreaming()
- }
- override func testServerStreaming() throws {
- try super.testServerStreaming()
- }
- override func testBidirectionalStreaming() throws {
- try super.testBidirectionalStreaming()
- }
- }
- class ClientThrowingWhenServerReturningErrorTests: ServerThrowingTests {
- override func makeEchoProvider() -> Echo_EchoProvider { return ErrorReturningEchoProvider() }
- override func testUnary() throws {
- try super.testUnary()
- }
- override func testClientStreaming() throws {
- try super.testClientStreaming()
- }
- override func testServerStreaming() throws {
- try super.testServerStreaming()
- }
- override func testBidirectionalStreaming() throws {
- try super.testBidirectionalStreaming()
- }
- }
- class ServerErrorTransformingTests: ServerThrowingTests {
- override var expectedError: GRPCStatus { return transformedError }
- override var expectedMetadata: HPACKHeaders? {
- return HPACKHeaders([
- ("grpc-status", "10"), ("grpc-message", "transformed error"),
- ("transformed", "header"),
- ])
- }
- override func makeErrorDelegate() -> ServerErrorDelegate? { return ErrorTransformingDelegate() }
- override func testUnary() throws {
- try super.testUnary()
- }
- override func testClientStreaming() throws {
- try super.testClientStreaming()
- }
- override func testServerStreaming() throws {
- try super.testServerStreaming()
- }
- override func testBidirectionalStreaming() throws {
- try super.testBidirectionalStreaming()
- }
- }
|