ServerCancellingTests.swift 5.0 KB

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