EchoTests.swift 11 KB

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