ServerThrowingTests.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. @testable import SwiftGRPC
  19. import XCTest
  20. fileprivate let testStatus = ServerStatus(code: .permissionDenied, message: "custom status message")
  21. fileprivate let testStatusWithTrailingMetadata = ServerStatus(code: .permissionDenied, message: "custom status message",
  22. trailingMetadata: try! Metadata(["foo": "bar"]))
  23. fileprivate class StatusThrowingProvider: Echo_EchoProvider {
  24. func get(request: Echo_EchoRequest, session _: Echo_EchoGetSession) throws -> Echo_EchoResponse {
  25. throw testStatusWithTrailingMetadata
  26. }
  27. func expand(request: Echo_EchoRequest, session: Echo_EchoExpandSession) throws -> ServerStatus? {
  28. throw testStatus
  29. }
  30. func collect(session: Echo_EchoCollectSession) throws -> Echo_EchoResponse? {
  31. throw testStatus
  32. }
  33. func update(session: Echo_EchoUpdateSession) throws -> ServerStatus? {
  34. throw testStatus
  35. }
  36. }
  37. class ServerThrowingTests: BasicEchoTestCase {
  38. static var allTests: [(String, (ServerThrowingTests) -> () throws -> Void)] {
  39. return [
  40. ("testServerThrowsUnary", testServerThrowsUnary),
  41. ("testServerThrowsClientStreaming", testServerThrowsClientStreaming),
  42. ("testServerThrowsServerStreaming", testServerThrowsServerStreaming),
  43. ("testServerThrowsBidirectionalStreaming", testServerThrowsBidirectionalStreaming)
  44. ]
  45. }
  46. override func makeProvider() -> Echo_EchoProvider { return StatusThrowingProvider() }
  47. }
  48. extension ServerThrowingTests {
  49. func testServerThrowsUnary() {
  50. do {
  51. let result = try client.get(Echo_EchoRequest(text: "foo")).text
  52. XCTFail("should have thrown, received \(result) instead")
  53. } catch {
  54. guard case let .callError(callResult) = error as! RPCError
  55. else { XCTFail("unexpected error \(error)"); return }
  56. XCTAssertEqual(.permissionDenied, callResult.statusCode)
  57. XCTAssertEqual("custom status message", callResult.statusMessage)
  58. XCTAssertEqual(["foo": "bar"], callResult.trailingMetadata?.dictionaryRepresentation)
  59. }
  60. }
  61. func testServerThrowsClientStreaming() {
  62. let completionHandlerExpectation = expectation(description: "final completion handler called")
  63. let call = try! client.collect { callResult in
  64. XCTAssertEqual(.permissionDenied, callResult.statusCode)
  65. XCTAssertEqual("custom status message", callResult.statusMessage)
  66. completionHandlerExpectation.fulfill()
  67. }
  68. let sendExpectation = expectation(description: "send completion handler 1 called")
  69. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in
  70. // The server only times out later in its lifecycle, so we shouldn't get an error when trying to send a message.
  71. XCTAssertNil($0)
  72. sendExpectation.fulfill()
  73. }
  74. call.waitForSendOperationsToFinish()
  75. do {
  76. let result = try call.closeAndReceive()
  77. XCTFail("should have thrown, received \(result) instead")
  78. } catch let receiveError {
  79. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  80. }
  81. waitForExpectations(timeout: defaultTimeout)
  82. }
  83. func testServerThrowsServerStreaming() {
  84. let completionHandlerExpectation = expectation(description: "completion handler called")
  85. let call = try! client.expand(Echo_EchoRequest(text: "foo bar baz")) { callResult in
  86. XCTAssertEqual(.permissionDenied, callResult.statusCode)
  87. XCTAssertEqual("custom status message", callResult.statusMessage)
  88. completionHandlerExpectation.fulfill()
  89. }
  90. // FIXME(danielalm): Why does `call.receive()` essentially return "end of stream", rather than returning an error?
  91. XCTAssertNil(try! call.receive())
  92. waitForExpectations(timeout: defaultTimeout)
  93. }
  94. func testServerThrowsBidirectionalStreaming() {
  95. let completionHandlerExpectation = expectation(description: "completion handler called")
  96. let call = try! client.update { callResult in
  97. XCTAssertEqual(.permissionDenied, callResult.statusCode)
  98. XCTAssertEqual("custom status message", callResult.statusMessage)
  99. completionHandlerExpectation.fulfill()
  100. }
  101. let sendExpectation = expectation(description: "send completion handler 1 called")
  102. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in
  103. // The server only times out later in its lifecycle, so we shouldn't get an error when trying to send a message.
  104. XCTAssertNil($0)
  105. sendExpectation.fulfill()
  106. }
  107. call.waitForSendOperationsToFinish()
  108. // FIXME(danielalm): Why does `call.receive()` essentially return "end of stream", rather than returning an error?
  109. XCTAssertNil(try! call.receive())
  110. waitForExpectations(timeout: defaultTimeout)
  111. }
  112. }