ServerCancellingTests.swift 4.6 KB

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