ServerCancellingTests.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. }
  49. extension ServerCancellingTests {
  50. func testServerThrowsUnary() {
  51. do {
  52. let result = try client.get(Echo_EchoRequest(text: "foo")).text
  53. XCTFail("should have thrown, received \(result) instead")
  54. } catch {
  55. guard case let .callError(callResult) = error as! RPCError
  56. else { XCTFail("unexpected error \(error)"); return }
  57. XCTAssertEqual(.cancelled, callResult.statusCode)
  58. XCTAssertEqual("Cancelled", callResult.statusMessage)
  59. }
  60. }
  61. func testServerThrowsClientStreaming() {
  62. let completionHandlerExpectation = expectation(description: "final completion handler called")
  63. let call = try! client.collect { callResult in
  64. XCTAssertEqual(.cancelled, callResult.statusCode)
  65. XCTAssertEqual("Cancelled", 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(.cancelled, callResult.statusCode)
  87. XCTAssertEqual("Cancelled", 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(.cancelled, callResult.statusCode)
  98. XCTAssertEqual("Cancelled", 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. }