ClientCancellingTests.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. class ClientCancellingTests: BasicEchoTestCase {
  21. }
  22. extension ClientCancellingTests {
  23. func testUnary() {
  24. let completionHandlerExpectation = expectation(description: "final completion handler called")
  25. let call = try! client.get(Echo_EchoRequest(text: "foo bar baz")) { response, callResult in
  26. XCTAssertNil(response)
  27. XCTAssertEqual(.cancelled, callResult.statusCode)
  28. completionHandlerExpectation.fulfill()
  29. }
  30. call.cancel()
  31. waitForExpectations(timeout: defaultTimeout)
  32. }
  33. func testClientStreaming() {
  34. let completionHandlerExpectation = expectation(description: "final completion handler called")
  35. let call = try! client.collect { callResult in
  36. XCTAssertEqual(.cancelled, callResult.statusCode)
  37. completionHandlerExpectation.fulfill()
  38. }
  39. call.cancel()
  40. let sendExpectation = expectation(description: "send completion handler 1 called")
  41. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertEqual(.unknown, $0 as! CallError); sendExpectation.fulfill() }
  42. call.waitForSendOperationsToFinish()
  43. do {
  44. let result = try call.closeAndReceive()
  45. XCTFail("should have thrown, received \(result) instead")
  46. } catch let receiveError {
  47. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  48. }
  49. waitForExpectations(timeout: defaultTimeout)
  50. }
  51. func testServerStreaming() {
  52. let completionHandlerExpectation = expectation(description: "completion handler called")
  53. let call = try! client.expand(Echo_EchoRequest(text: "foo bar baz")) { callResult in
  54. XCTAssertEqual(.cancelled, callResult.statusCode)
  55. completionHandlerExpectation.fulfill()
  56. }
  57. XCTAssertEqual("Swift echo expand (0): foo", try! call.receive()!.text)
  58. call.cancel()
  59. do {
  60. let result = try call.receive()
  61. XCTFail("should have thrown, received \(String(describing: result)) instead")
  62. } catch let receiveError {
  63. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  64. }
  65. waitForExpectations(timeout: defaultTimeout)
  66. }
  67. func testBidirectionalStreaming() {
  68. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  69. let call = try! client.update { callResult in
  70. XCTAssertEqual(.cancelled, callResult.statusCode)
  71. finalCompletionHandlerExpectation.fulfill()
  72. }
  73. var sendExpectation = expectation(description: "send completion handler 1 called")
  74. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  75. XCTAssertEqual("Swift echo update (0): foo", try! call.receive()!.text)
  76. call.cancel()
  77. sendExpectation = expectation(description: "send completion handler 2 called")
  78. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertEqual(.unknown, $0 as! CallError); sendExpectation.fulfill() }
  79. do {
  80. let result = try call.receive()
  81. XCTFail("should have thrown, received \(String(describing: result)) instead")
  82. } catch let receiveError {
  83. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  84. }
  85. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  86. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  87. waitForExpectations(timeout: defaultTimeout)
  88. }
  89. }