ServerCancellingTests.swift 5.0 KB

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