ServerThrowingTests.swift 5.8 KB

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