ClientCancellingTests.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // Waits 10ms before each send operation and does not log sending errors,
  21. // as these are expected when the call times out.
  22. private class SleepingEchoProvider: Echo_EchoProvider {
  23. func get(request: Echo_EchoRequest, session _: Echo_EchoGetSession) throws -> Echo_EchoResponse {
  24. Thread.sleep(forTimeInterval: 0.1)
  25. var response = Echo_EchoResponse()
  26. response.text = "Swift echo get: " + request.text
  27. return response
  28. }
  29. func expand(request: Echo_EchoRequest, session: Echo_EchoExpandSession) throws -> ServerStatus? {
  30. let parts = request.text.components(separatedBy: " ")
  31. for (i, part) in parts.enumerated() {
  32. Thread.sleep(forTimeInterval: 0.1)
  33. var response = Echo_EchoResponse()
  34. response.text = "Swift echo expand (\(i)): \(part)"
  35. try session.send(response)
  36. }
  37. return .ok
  38. }
  39. func collect(session: Echo_EchoCollectSession) throws -> Echo_EchoResponse? {
  40. Thread.sleep(forTimeInterval: 0.1)
  41. var parts: [String] = []
  42. while true {
  43. do {
  44. guard let request = try session.receive()
  45. else { break } // End of stream
  46. parts.append(request.text)
  47. } catch {
  48. break
  49. }
  50. }
  51. var response = Echo_EchoResponse()
  52. response.text = "Swift echo collect: " + parts.joined(separator: " ")
  53. return response
  54. }
  55. func update(session: Echo_EchoUpdateSession) throws -> ServerStatus? {
  56. var count = 0
  57. while true {
  58. do {
  59. Thread.sleep(forTimeInterval: 0.1)
  60. guard let request = try session.receive()
  61. else { break } // End of stream
  62. var response = Echo_EchoResponse()
  63. response.text = "Swift echo update (\(count)): \(request.text)"
  64. count += 1
  65. try session.send(response)
  66. } catch {
  67. break
  68. }
  69. }
  70. return .ok
  71. }
  72. }
  73. class ClientCancellingTests: BasicEchoTestCase {
  74. static var allTests: [(String, (ClientCancellingTests) -> () throws -> Void)] {
  75. return [
  76. ("testUnary", testUnary),
  77. ("testClientStreaming", testClientStreaming),
  78. ("testServerStreaming", testServerStreaming),
  79. ("testBidirectionalStreaming", testBidirectionalStreaming),
  80. ]
  81. }
  82. override func makeProvider() -> Echo_EchoProvider { return SleepingEchoProvider() }
  83. }
  84. private func manyWords(_ count: Int) -> String {
  85. return (0..<count).map { String(describing: $0) }.joined(separator: " ")
  86. }
  87. extension ClientCancellingTests {
  88. func testUnary() {
  89. let completionHandlerExpectation = expectation(description: "final completion handler called")
  90. let call = try! client.get(Echo_EchoRequest(text: manyWords(10))) { response, callResult in
  91. XCTAssertNil(response)
  92. XCTAssertEqual(.cancelled, callResult.statusCode)
  93. completionHandlerExpectation.fulfill()
  94. }
  95. call.cancel()
  96. waitForExpectations(timeout: defaultTimeout)
  97. }
  98. func testClientStreaming() {
  99. let completionHandlerExpectation = expectation(description: "final completion handler called")
  100. let call = try! client.collect { callResult in
  101. XCTAssertEqual(.cancelled, callResult.statusCode)
  102. completionHandlerExpectation.fulfill()
  103. }
  104. call.cancel()
  105. let sendExpectation = expectation(description: "send completion handler 1 called")
  106. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertEqual(.unknown, $0 as! CallError); sendExpectation.fulfill() }
  107. call.waitForSendOperationsToFinish()
  108. do {
  109. let result = try call.closeAndReceive()
  110. XCTFail("should have thrown, received \(result) instead")
  111. } catch let receiveError {
  112. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  113. }
  114. waitForExpectations(timeout: defaultTimeout)
  115. }
  116. func testServerStreaming() {
  117. let completionHandlerExpectation = expectation(description: "completion handler called")
  118. let call = try! client.expand(Echo_EchoRequest(text: manyWords(10))) { callResult in
  119. XCTAssertEqual(.cancelled, callResult.statusCode)
  120. completionHandlerExpectation.fulfill()
  121. }
  122. XCTAssertEqual("Swift echo expand (0): 0", try! call.receive()!.text)
  123. call.cancel()
  124. do {
  125. let result = try call.receive()
  126. XCTFail("should have thrown, received \(String(describing: result)) instead")
  127. } catch let receiveError {
  128. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  129. }
  130. waitForExpectations(timeout: defaultTimeout)
  131. }
  132. func testBidirectionalStreaming() {
  133. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  134. let call = try! client.update { callResult in
  135. XCTAssertEqual(.cancelled, callResult.statusCode)
  136. finalCompletionHandlerExpectation.fulfill()
  137. }
  138. var sendExpectation = expectation(description: "send completion handler 1 called")
  139. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  140. XCTAssertEqual("Swift echo update (0): foo", try! call.receive()!.text)
  141. call.cancel()
  142. sendExpectation = expectation(description: "send completion handler 2 called")
  143. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertEqual(.unknown, $0 as! CallError); sendExpectation.fulfill() }
  144. do {
  145. let result = try call.receive()
  146. XCTFail("should have thrown, received \(String(describing: result)) instead")
  147. } catch let receiveError {
  148. XCTAssertEqual(.unknown, (receiveError as! RPCError).callResult!.statusCode)
  149. }
  150. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  151. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  152. waitForExpectations(timeout: defaultTimeout)
  153. }
  154. }