EchoTests.swift 10 KB

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