EchoTests.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. ("testUnaryLotsOfRequests", testUnaryLotsOfRequests),
  25. ("testClientStreaming", testClientStreaming),
  26. ("testClientStreamingLotsOfMessages", testClientStreamingLotsOfMessages),
  27. ("testServerStreaming", testServerStreaming),
  28. ("testServerStreamingLotsOfMessages", testServerStreamingLotsOfMessages),
  29. ("testBidirectionalStreamingBatched", testBidirectionalStreamingBatched),
  30. ("testBidirectionalStreamingPingPong", testBidirectionalStreamingPingPong),
  31. ("testBidirectionalStreamingLotsOfMessagesBatched", testBidirectionalStreamingLotsOfMessagesBatched),
  32. ("testBidirectionalStreamingLotsOfMessagesPingPong", testBidirectionalStreamingLotsOfMessagesPingPong)
  33. ]
  34. }
  35. static let lotsOfStrings = (0..<1000).map { String(describing: $0) }
  36. }
  37. class EchoTestsSecure: EchoTests {
  38. override var security: Security { return .ssl }
  39. }
  40. class EchoTestsMutualAuth: EchoTests {
  41. override var security: Security { return .tlsMutualAuth }
  42. }
  43. extension EchoTests {
  44. func testUnary() {
  45. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  46. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  47. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  48. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  49. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  50. }
  51. func testUnaryLotsOfRequests() {
  52. // No need to spam the log with 10k lines.
  53. server.shouldLogRequests = false
  54. // Sending that many requests at once can sometimes trip things up, it seems.
  55. client.timeout = 5.0
  56. let clockStart = clock()
  57. let numberOfRequests = 10_000
  58. for i in 0..<numberOfRequests {
  59. if i % 1_000 == 0 && i > 0 {
  60. print("\(i) requests sent so far, elapsed time: \(Double(clock() - clockStart) / Double(CLOCKS_PER_SEC))")
  61. }
  62. XCTAssertEqual("Swift echo get: foo \(i)", try client.get(Echo_EchoRequest(text: "foo \(i)")).text)
  63. }
  64. print("total time for \(numberOfRequests) requests: \(Double(clock() - clockStart) / Double(CLOCKS_PER_SEC))")
  65. }
  66. }
  67. extension EchoTests {
  68. func testClientStreaming() {
  69. let completionHandlerExpectation = expectation(description: "final completion handler called")
  70. let call = try! client.collect { callResult in
  71. XCTAssertEqual(.ok, callResult.statusCode)
  72. completionHandlerExpectation.fulfill()
  73. }
  74. var sendExpectation = expectation(description: "send completion handler 1 called")
  75. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  76. sendExpectation = expectation(description: "send completion handler 2 called")
  77. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  78. sendExpectation = expectation(description: "send completion handler 3 called")
  79. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  80. call.waitForSendOperationsToFinish()
  81. let response = try! call.closeAndReceive()
  82. XCTAssertEqual("Swift echo collect: foo bar baz", response.text)
  83. waitForExpectations(timeout: defaultTimeout)
  84. }
  85. func testClientStreamingLotsOfMessages() {
  86. let completionHandlerExpectation = expectation(description: "completion handler called")
  87. let call = try! client.collect { callResult in
  88. XCTAssertEqual(.ok, callResult.statusCode)
  89. completionHandlerExpectation.fulfill()
  90. }
  91. for string in EchoTests.lotsOfStrings {
  92. let sendExpectation = expectation(description: "send completion handler \(string) called")
  93. try! call.send(Echo_EchoRequest(text: string)) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  94. }
  95. call.waitForSendOperationsToFinish()
  96. let response = try! call.closeAndReceive()
  97. XCTAssertEqual("Swift echo collect: " + EchoTests.lotsOfStrings.joined(separator: " "), response.text)
  98. waitForExpectations(timeout: defaultTimeout)
  99. }
  100. }
  101. extension EchoTests {
  102. func testServerStreaming() {
  103. let completionHandlerExpectation = expectation(description: "completion handler called")
  104. let call = try! client.expand(Echo_EchoRequest(text: "foo bar baz")) { callResult in
  105. XCTAssertEqual(.ok, callResult.statusCode)
  106. completionHandlerExpectation.fulfill()
  107. }
  108. XCTAssertEqual("Swift echo expand (0): foo", try! call.receive()!.text)
  109. XCTAssertEqual("Swift echo expand (1): bar", try! call.receive()!.text)
  110. XCTAssertEqual("Swift echo expand (2): baz", try! call.receive()!.text)
  111. XCTAssertNil(try! call.receive())
  112. waitForExpectations(timeout: defaultTimeout)
  113. }
  114. func testServerStreamingLotsOfMessages() {
  115. let completionHandlerExpectation = expectation(description: "completion handler called")
  116. let call = try! client.expand(Echo_EchoRequest(text: EchoTests.lotsOfStrings.joined(separator: " "))) { callResult in
  117. XCTAssertEqual(.ok, callResult.statusCode)
  118. completionHandlerExpectation.fulfill()
  119. }
  120. for string in EchoTests.lotsOfStrings {
  121. XCTAssertEqual("Swift echo expand (\(string)): \(string)", try! call.receive()!.text)
  122. }
  123. XCTAssertNil(try! call.receive())
  124. waitForExpectations(timeout: defaultTimeout)
  125. }
  126. }
  127. extension EchoTests {
  128. func testBidirectionalStreamingBatched() {
  129. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  130. let call = try! client.update { callResult in
  131. XCTAssertEqual(.ok, callResult.statusCode)
  132. finalCompletionHandlerExpectation.fulfill()
  133. }
  134. var sendExpectation = expectation(description: "send completion handler 1 called")
  135. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  136. sendExpectation = expectation(description: "send completion handler 2 called")
  137. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  138. sendExpectation = expectation(description: "send completion handler 3 called")
  139. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  140. call.waitForSendOperationsToFinish()
  141. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  142. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  143. XCTAssertEqual("Swift echo update (0): foo", try! call.receive()!.text)
  144. XCTAssertEqual("Swift echo update (1): bar", try! call.receive()!.text)
  145. XCTAssertEqual("Swift echo update (2): baz", try! call.receive()!.text)
  146. XCTAssertNil(try! call.receive())
  147. waitForExpectations(timeout: defaultTimeout)
  148. }
  149. func testBidirectionalStreamingPingPong() {
  150. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  151. let call = try! client.update { callResult in
  152. XCTAssertEqual(.ok, callResult.statusCode)
  153. finalCompletionHandlerExpectation.fulfill()
  154. }
  155. var sendExpectation = expectation(description: "send completion handler 1 called")
  156. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  157. XCTAssertEqual("Swift echo update (0): foo", try! call.receive()!.text)
  158. sendExpectation = expectation(description: "send completion handler 2 called")
  159. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  160. XCTAssertEqual("Swift echo update (1): bar", try! call.receive()!.text)
  161. sendExpectation = expectation(description: "send completion handler 3 called")
  162. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  163. XCTAssertEqual("Swift echo update (2): baz", try! call.receive()!.text)
  164. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  165. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  166. XCTAssertNil(try! call.receive())
  167. waitForExpectations(timeout: defaultTimeout)
  168. }
  169. func testBidirectionalStreamingLotsOfMessagesBatched() {
  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. }
  179. call.waitForSendOperationsToFinish()
  180. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  181. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  182. for string in EchoTests.lotsOfStrings {
  183. XCTAssertEqual("Swift echo update (\(string)): \(string)", try! call.receive()!.text)
  184. }
  185. XCTAssertNil(try! call.receive())
  186. waitForExpectations(timeout: defaultTimeout)
  187. }
  188. func testBidirectionalStreamingLotsOfMessagesPingPong() {
  189. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  190. let call = try! client.update { callResult in
  191. XCTAssertEqual(.ok, callResult.statusCode)
  192. finalCompletionHandlerExpectation.fulfill()
  193. }
  194. for string in EchoTests.lotsOfStrings {
  195. let sendExpectation = expectation(description: "send completion handler \(string) called")
  196. try! call.send(Echo_EchoRequest(text: string)) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  197. XCTAssertEqual("Swift echo update (\(string)): \(string)", try! call.receive()!.text)
  198. }
  199. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  200. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  201. XCTAssertNil(try! call.receive())
  202. waitForExpectations(timeout: defaultTimeout)
  203. }
  204. }