EchoTests.swift 11 KB

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