EchoTests.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. extension Echo_EchoRequest {
  21. init(text: String) {
  22. self.text = text
  23. }
  24. }
  25. class EchoTests: XCTestCase {
  26. static var allTests: [(String, (EchoTests) -> () throws -> Void)] {
  27. return [
  28. ("testUnary", testUnary),
  29. ("testClientStreaming", testClientStreaming),
  30. ("testClientStreamingLotsOfMessages", testClientStreamingLotsOfMessages),
  31. ("testServerStreaming", testServerStreaming),
  32. ("testServerStreamingLotsOfMessages", testServerStreamingLotsOfMessages),
  33. ("testBidirectionalStreamingBatched", testBidirectionalStreamingBatched),
  34. ("testBidirectionalStreamingPingPong", testBidirectionalStreamingPingPong),
  35. ("testBidirectionalStreamingLotsOfMessagesBatched", testBidirectionalStreamingLotsOfMessagesBatched),
  36. ("testBidirectionalStreamingLotsOfMessagesPingPong", testBidirectionalStreamingLotsOfMessagesPingPong)
  37. ]
  38. }
  39. static let lotsOfStrings = (0..<1000).map { String(describing: $0) }
  40. let defaultTimeout: TimeInterval = 5.0
  41. let provider = EchoProvider()
  42. var server: Echo_EchoServer!
  43. var client: Echo_EchoServiceClient!
  44. var secure: Bool { return false }
  45. override func setUp() {
  46. super.setUp()
  47. let address = "localhost:5050"
  48. if secure {
  49. let certificateString = String(data: certificateForTests, encoding: .utf8)!
  50. server = Echo_EchoServer(address: address,
  51. certificateString: certificateString,
  52. keyString: String(data: keyForTests, encoding: .utf8)!,
  53. provider: provider)
  54. server.start(queue: DispatchQueue.global())
  55. client = Echo_EchoServiceClient(address: address, certificates: certificateString, host: "example.com")
  56. client.host = "example.com"
  57. } else {
  58. server = Echo_EchoServer(address: address, provider: provider)
  59. server.start(queue: DispatchQueue.global())
  60. client = Echo_EchoServiceClient(address: address, secure: false)
  61. }
  62. client.timeout = defaultTimeout
  63. }
  64. override func tearDown() {
  65. client = nil
  66. server.server.stop()
  67. server = nil
  68. super.tearDown()
  69. }
  70. }
  71. class EchoTestsSecure: EchoTests {
  72. override var secure: Bool { return true }
  73. }
  74. extension EchoTests {
  75. func testUnary() {
  76. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  77. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  78. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  79. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  80. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  81. }
  82. }
  83. extension EchoTests {
  84. func testClientStreaming() {
  85. let completionHandlerExpectation = expectation(description: "final completion handler called")
  86. let call = try! client.collect { callResult in
  87. XCTAssertEqual(.ok, callResult.statusCode)
  88. completionHandlerExpectation.fulfill()
  89. }
  90. var sendExpectation = expectation(description: "send completion handler 1 called")
  91. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  92. sendExpectation = expectation(description: "send completion handler 2 called")
  93. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  94. sendExpectation = expectation(description: "send completion handler 3 called")
  95. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  96. call.waitForSendOperationsToFinish()
  97. let response = try! call.closeAndReceive()
  98. XCTAssertEqual("Swift echo collect: foo bar baz", response.text)
  99. waitForExpectations(timeout: defaultTimeout)
  100. }
  101. func testClientStreamingLotsOfMessages() {
  102. let completionHandlerExpectation = expectation(description: "completion handler called")
  103. let call = try! client.collect { callResult in
  104. XCTAssertEqual(.ok, callResult.statusCode)
  105. completionHandlerExpectation.fulfill()
  106. }
  107. for string in EchoTests.lotsOfStrings {
  108. let sendExpectation = expectation(description: "send completion handler \(string) called")
  109. try! call.send(Echo_EchoRequest(text: string)) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  110. }
  111. call.waitForSendOperationsToFinish()
  112. let response = try! call.closeAndReceive()
  113. XCTAssertEqual("Swift echo collect: " + EchoTests.lotsOfStrings.joined(separator: " "), response.text)
  114. waitForExpectations(timeout: defaultTimeout)
  115. }
  116. }
  117. extension EchoTests {
  118. func testServerStreaming() {
  119. let completionHandlerExpectation = expectation(description: "completion handler called")
  120. let call = try! client.expand(Echo_EchoRequest(text: "foo bar baz")) { callResult in
  121. XCTAssertEqual(.ok, callResult.statusCode)
  122. completionHandlerExpectation.fulfill()
  123. }
  124. XCTAssertEqual("Swift echo expand (0): foo", try! call.receive()!.text)
  125. XCTAssertEqual("Swift echo expand (1): bar", try! call.receive()!.text)
  126. XCTAssertEqual("Swift echo expand (2): baz", try! call.receive()!.text)
  127. XCTAssertNil(try! call.receive())
  128. waitForExpectations(timeout: defaultTimeout)
  129. }
  130. func testServerStreamingLotsOfMessages() {
  131. let completionHandlerExpectation = expectation(description: "completion handler called")
  132. let call = try! client.expand(Echo_EchoRequest(text: EchoTests.lotsOfStrings.joined(separator: " "))) { callResult in
  133. XCTAssertEqual(.ok, callResult.statusCode)
  134. completionHandlerExpectation.fulfill()
  135. }
  136. for string in EchoTests.lotsOfStrings {
  137. XCTAssertEqual("Swift echo expand (\(string)): \(string)", try! call.receive()!.text)
  138. }
  139. XCTAssertNil(try! call.receive())
  140. waitForExpectations(timeout: defaultTimeout)
  141. }
  142. }
  143. extension EchoTests {
  144. func testBidirectionalStreamingBatched() {
  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. sendExpectation = expectation(description: "send completion handler 2 called")
  153. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  154. sendExpectation = expectation(description: "send completion handler 3 called")
  155. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  156. call.waitForSendOperationsToFinish()
  157. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  158. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  159. XCTAssertEqual("Swift echo update (0): foo", try! call.receive()!.text)
  160. XCTAssertEqual("Swift echo update (1): bar", try! call.receive()!.text)
  161. XCTAssertEqual("Swift echo update (2): baz", try! call.receive()!.text)
  162. XCTAssertNil(try! call.receive())
  163. waitForExpectations(timeout: defaultTimeout)
  164. }
  165. func testBidirectionalStreamingPingPong() {
  166. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  167. let call = try! client.update { callResult in
  168. XCTAssertEqual(.ok, callResult.statusCode)
  169. finalCompletionHandlerExpectation.fulfill()
  170. }
  171. var sendExpectation = expectation(description: "send completion handler 1 called")
  172. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  173. XCTAssertEqual("Swift echo update (0): foo", try! call.receive()!.text)
  174. sendExpectation = expectation(description: "send completion handler 2 called")
  175. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  176. XCTAssertEqual("Swift echo update (1): bar", try! call.receive()!.text)
  177. sendExpectation = expectation(description: "send completion handler 3 called")
  178. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  179. XCTAssertEqual("Swift echo update (2): baz", try! call.receive()!.text)
  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. func testBidirectionalStreamingLotsOfMessagesBatched() {
  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. }
  195. call.waitForSendOperationsToFinish()
  196. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  197. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  198. for string in EchoTests.lotsOfStrings {
  199. XCTAssertEqual("Swift echo update (\(string)): \(string)", try! call.receive()!.text)
  200. }
  201. XCTAssertNil(try! call.receive())
  202. waitForExpectations(timeout: defaultTimeout)
  203. }
  204. func testBidirectionalStreamingLotsOfMessagesPingPong() {
  205. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  206. let call = try! client.update { callResult in
  207. XCTAssertEqual(.ok, callResult.statusCode)
  208. finalCompletionHandlerExpectation.fulfill()
  209. }
  210. for string in EchoTests.lotsOfStrings {
  211. let sendExpectation = expectation(description: "send completion handler \(string) called")
  212. try! call.send(Echo_EchoRequest(text: string)) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  213. XCTAssertEqual("Swift echo update (\(string)): \(string)", try! call.receive()!.text)
  214. }
  215. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  216. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  217. XCTAssertNil(try! call.receive())
  218. waitForExpectations(timeout: defaultTimeout)
  219. }
  220. }