ServerThrowingTests.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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(["some_long_key": "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. ("testServerThrowsUnary_noTrailingMetadataCorruption", testServerThrowsUnary_noTrailingMetadataCorruptionAfterOriginalTrailingMetadataGetsReleased),
  42. ("testServerThrowsClientStreaming", testServerThrowsClientStreaming),
  43. ("testServerThrowsServerStreaming", testServerThrowsServerStreaming),
  44. ("testServerThrowsBidirectionalStreaming", testServerThrowsBidirectionalStreaming)
  45. ]
  46. }
  47. override func makeProvider() -> Echo_EchoProvider { return StatusThrowingProvider() }
  48. }
  49. extension ServerThrowingTests {
  50. func testServerThrowsUnary() throws {
  51. do {
  52. let result = try client.get(Echo_EchoRequest(text: "foo")).text
  53. XCTFail("should have thrown, received \(result) instead")
  54. return
  55. } catch let error as RPCError {
  56. guard case let .callError(callResult) = error
  57. else { XCTFail("unexpected error \(error)"); return }
  58. XCTAssertEqual(.permissionDenied, callResult.statusCode)
  59. XCTAssertEqual("custom status message", callResult.statusMessage)
  60. XCTAssertEqual(["some_long_key": "bar"], callResult.trailingMetadata?.dictionaryRepresentation)
  61. }
  62. }
  63. func testServerThrowsUnary_noTrailingMetadataCorruptionAfterOriginalTrailingMetadataGetsReleased() throws {
  64. do {
  65. let result = try client.get(Echo_EchoRequest(text: "foo")).text
  66. XCTFail("should have thrown, received \(result) instead")
  67. return
  68. } catch let error as RPCError {
  69. guard case let .callError(callResult) = error
  70. else { XCTFail("unexpected error \(error)"); return }
  71. // Send another RPC to cause the original metadata array to be deallocated.
  72. // If we were not copying the metadata array correctly, this would cause the metadata of the call result to become
  73. // corrupted.
  74. _ = try? client.get(Echo_EchoRequest(text: "foo")).text
  75. // It seems like we need this sleep for the memory corruption to occur.
  76. Thread.sleep(forTimeInterval: 0.01)
  77. XCTAssertEqual(["some_long_key": "bar"], callResult.trailingMetadata?.dictionaryRepresentation)
  78. }
  79. }
  80. func testServerThrowsClientStreaming() {
  81. let completionHandlerExpectation = expectation(description: "final completion handler called")
  82. let call = try! client.collect { callResult in
  83. XCTAssertEqual(.permissionDenied, callResult.statusCode)
  84. XCTAssertEqual("custom status message", callResult.statusMessage)
  85. completionHandlerExpectation.fulfill()
  86. }
  87. let sendExpectation = expectation(description: "send completion handler 1 called")
  88. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in
  89. // The server only times out later in its lifecycle, so we shouldn't get an error when trying to send a message.
  90. XCTAssertNil($0)
  91. sendExpectation.fulfill()
  92. }
  93. call.waitForSendOperationsToFinish()
  94. do {
  95. let result = try call.closeAndReceive()
  96. XCTFail("should have thrown, received \(result) instead")
  97. } catch let receiveError {
  98. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  99. }
  100. waitForExpectations(timeout: defaultTimeout)
  101. }
  102. func testServerThrowsServerStreaming() {
  103. let completionHandlerExpectation = expectation(description: "completion handler called")
  104. let call = try! client.expand(Echo_EchoRequest(text: "foo bar baz")) { callResult in
  105. XCTAssertEqual(.permissionDenied, callResult.statusCode)
  106. XCTAssertEqual("custom status message", callResult.statusMessage)
  107. completionHandlerExpectation.fulfill()
  108. }
  109. // FIXME(danielalm): Why does `call.receive()` essentially return "end of stream", rather than returning an error?
  110. XCTAssertNil(try! call.receive())
  111. waitForExpectations(timeout: defaultTimeout)
  112. }
  113. func testServerThrowsBidirectionalStreaming() {
  114. let completionHandlerExpectation = expectation(description: "completion handler called")
  115. let call = try! client.update { callResult in
  116. XCTAssertEqual(.permissionDenied, callResult.statusCode)
  117. XCTAssertEqual("custom status message", callResult.statusMessage)
  118. completionHandlerExpectation.fulfill()
  119. }
  120. let sendExpectation = expectation(description: "send completion handler 1 called")
  121. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in
  122. // The server only times out later in its lifecycle, so we shouldn't get an error when trying to send a message.
  123. XCTAssertNil($0)
  124. sendExpectation.fulfill()
  125. }
  126. call.waitForSendOperationsToFinish()
  127. // FIXME(danielalm): Why does `call.receive()` essentially return "end of stream", rather than returning an error?
  128. XCTAssertNil(try! call.receive())
  129. waitForExpectations(timeout: defaultTimeout)
  130. }
  131. }