EchoTests.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 EchoTests: BasicEchoTestCase {
  21. static var allTests: [(String, (EchoTests) -> () throws -> Void)] {
  22. return [
  23. ("testUnary", testUnary),
  24. ("testClientStreaming", testClientStreaming),
  25. ("testClientStreamingLotsOfMessages", testClientStreamingLotsOfMessages),
  26. ("testServerStreaming", testServerStreaming),
  27. ("testServerStreamingLotsOfMessages", testServerStreamingLotsOfMessages),
  28. ("testBidirectionalStreamingBatched", testBidirectionalStreamingBatched),
  29. ("testBidirectionalStreamingPingPong", testBidirectionalStreamingPingPong),
  30. ("testBidirectionalStreamingLotsOfMessagesBatched", testBidirectionalStreamingLotsOfMessagesBatched),
  31. ("testBidirectionalStreamingLotsOfMessagesPingPong", testBidirectionalStreamingLotsOfMessagesPingPong)
  32. ]
  33. }
  34. static let lotsOfStrings = (0..<1000).map { String(describing: $0) }
  35. }
  36. class EchoTestsSecure: EchoTests {
  37. override var secure: Bool { return true }
  38. }
  39. extension EchoTests {
  40. func testUnary() {
  41. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  42. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  43. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  44. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  45. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  46. }
  47. }
  48. extension EchoTests {
  49. func testClientStreaming() {
  50. let completionHandlerExpectation = expectation(description: "final completion handler called")
  51. let call = try! client.collect { callResult in
  52. XCTAssertEqual(.ok, callResult.statusCode)
  53. completionHandlerExpectation.fulfill()
  54. }
  55. var sendExpectation = expectation(description: "send completion handler 1 called")
  56. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  57. sendExpectation = expectation(description: "send completion handler 2 called")
  58. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  59. sendExpectation = expectation(description: "send completion handler 3 called")
  60. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  61. call.waitForSendOperationsToFinish()
  62. let response = try! call.closeAndReceive()
  63. XCTAssertEqual("Swift echo collect: foo bar baz", response.text)
  64. waitForExpectations(timeout: defaultTimeout)
  65. }
  66. func testClientStreamingLotsOfMessages() {
  67. let completionHandlerExpectation = expectation(description: "completion handler called")
  68. let call = try! client.collect { callResult in
  69. XCTAssertEqual(.ok, callResult.statusCode)
  70. completionHandlerExpectation.fulfill()
  71. }
  72. for string in EchoTests.lotsOfStrings {
  73. let sendExpectation = expectation(description: "send completion handler \(string) called")
  74. try! call.send(Echo_EchoRequest(text: string)) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  75. }
  76. call.waitForSendOperationsToFinish()
  77. let response = try! call.closeAndReceive()
  78. XCTAssertEqual("Swift echo collect: " + EchoTests.lotsOfStrings.joined(separator: " "), response.text)
  79. waitForExpectations(timeout: defaultTimeout)
  80. }
  81. }
  82. extension EchoTests {
  83. func testServerStreaming() {
  84. let completionHandlerExpectation = expectation(description: "completion handler called")
  85. let call = try! client.expand(Echo_EchoRequest(text: "foo bar baz")) { callResult in
  86. XCTAssertEqual(.ok, callResult.statusCode)
  87. completionHandlerExpectation.fulfill()
  88. }
  89. XCTAssertEqual("Swift echo expand (0): foo", try! call.receive()!.text)
  90. XCTAssertEqual("Swift echo expand (1): bar", try! call.receive()!.text)
  91. XCTAssertEqual("Swift echo expand (2): baz", try! call.receive()!.text)
  92. XCTAssertNil(try! call.receive())
  93. waitForExpectations(timeout: defaultTimeout)
  94. }
  95. func testServerStreamingLotsOfMessages() {
  96. let completionHandlerExpectation = expectation(description: "completion handler called")
  97. let call = try! client.expand(Echo_EchoRequest(text: EchoTests.lotsOfStrings.joined(separator: " "))) { callResult in
  98. XCTAssertEqual(.ok, callResult.statusCode)
  99. completionHandlerExpectation.fulfill()
  100. }
  101. for string in EchoTests.lotsOfStrings {
  102. XCTAssertEqual("Swift echo expand (\(string)): \(string)", try! call.receive()!.text)
  103. }
  104. XCTAssertNil(try! call.receive())
  105. waitForExpectations(timeout: defaultTimeout)
  106. }
  107. }
  108. extension EchoTests {
  109. func testBidirectionalStreamingBatched() {
  110. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  111. let call = try! client.update { callResult in
  112. XCTAssertEqual(.ok, callResult.statusCode)
  113. finalCompletionHandlerExpectation.fulfill()
  114. }
  115. var sendExpectation = expectation(description: "send completion handler 1 called")
  116. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  117. sendExpectation = expectation(description: "send completion handler 2 called")
  118. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  119. sendExpectation = expectation(description: "send completion handler 3 called")
  120. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  121. call.waitForSendOperationsToFinish()
  122. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  123. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  124. XCTAssertEqual("Swift echo update (0): foo", try! call.receive()!.text)
  125. XCTAssertEqual("Swift echo update (1): bar", try! call.receive()!.text)
  126. XCTAssertEqual("Swift echo update (2): baz", try! call.receive()!.text)
  127. XCTAssertNil(try! call.receive())
  128. waitForExpectations(timeout: defaultTimeout)
  129. }
  130. func testBidirectionalStreamingPingPong() {
  131. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  132. let call = try! client.update { callResult in
  133. XCTAssertEqual(.ok, callResult.statusCode)
  134. finalCompletionHandlerExpectation.fulfill()
  135. }
  136. var sendExpectation = expectation(description: "send completion handler 1 called")
  137. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  138. XCTAssertEqual("Swift echo update (0): foo", try! call.receive()!.text)
  139. sendExpectation = expectation(description: "send completion handler 2 called")
  140. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  141. XCTAssertEqual("Swift echo update (1): bar", try! call.receive()!.text)
  142. sendExpectation = expectation(description: "send completion handler 3 called")
  143. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  144. XCTAssertEqual("Swift echo update (2): baz", try! call.receive()!.text)
  145. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  146. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  147. XCTAssertNil(try! call.receive())
  148. waitForExpectations(timeout: defaultTimeout)
  149. }
  150. func testBidirectionalStreamingLotsOfMessagesBatched() {
  151. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  152. let call = try! client.update { callResult in
  153. XCTAssertEqual(.ok, callResult.statusCode)
  154. finalCompletionHandlerExpectation.fulfill()
  155. }
  156. for string in EchoTests.lotsOfStrings {
  157. let sendExpectation = expectation(description: "send completion handler \(string) called")
  158. try! call.send(Echo_EchoRequest(text: string)) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  159. }
  160. call.waitForSendOperationsToFinish()
  161. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  162. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  163. for string in EchoTests.lotsOfStrings {
  164. XCTAssertEqual("Swift echo update (\(string)): \(string)", try! call.receive()!.text)
  165. }
  166. XCTAssertNil(try! call.receive())
  167. waitForExpectations(timeout: defaultTimeout)
  168. }
  169. func testBidirectionalStreamingLotsOfMessagesPingPong() {
  170. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  171. let call = try! client.update { callResult in
  172. XCTAssertEqual(.ok, callResult.statusCode)
  173. finalCompletionHandlerExpectation.fulfill()
  174. }
  175. for string in EchoTests.lotsOfStrings {
  176. let sendExpectation = expectation(description: "send completion handler \(string) called")
  177. try! call.send(Echo_EchoRequest(text: string)) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  178. XCTAssertEqual("Swift echo update (\(string)): \(string)", try! call.receive()!.text)
  179. }
  180. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  181. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  182. XCTAssertNil(try! call.receive())
  183. waitForExpectations(timeout: defaultTimeout)
  184. }
  185. }