ServerThrowingTests.swift 4.9 KB

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