EchoTests.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. } else {
  57. server = Echo_EchoServer(address: address, provider: provider)
  58. server.start(queue: DispatchQueue.global())
  59. client = Echo_EchoServiceClient(address: address, secure: false)
  60. }
  61. client.timeout = defaultTimeout
  62. }
  63. override func tearDown() {
  64. client = nil
  65. server.server.stop()
  66. server = nil
  67. super.tearDown()
  68. }
  69. }
  70. // Currently broken and thus commented out.
  71. // TODO(danielalm): Fix these.
  72. //class EchoTestsSecure: EchoTests {
  73. // override var secure: Bool { return true }
  74. //}
  75. extension EchoTests {
  76. func testUnary() {
  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. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  82. }
  83. }
  84. extension EchoTests {
  85. func testClientStreaming() {
  86. let completionHandlerExpectation = expectation(description: "final completion handler called")
  87. let call = try! client.collect { callResult in
  88. XCTAssertEqual(.ok, callResult.statusCode)
  89. completionHandlerExpectation.fulfill()
  90. }
  91. var sendExpectation = expectation(description: "send completion handler 1 called")
  92. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  93. sendExpectation = expectation(description: "send completion handler 2 called")
  94. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  95. sendExpectation = expectation(description: "send completion handler 3 called")
  96. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  97. call.waitForSendOperationsToFinish()
  98. let response = try! call.closeAndReceive()
  99. XCTAssertEqual("Swift echo collect: foo bar baz", response.text)
  100. waitForExpectations(timeout: defaultTimeout)
  101. }
  102. func testClientStreamingLotsOfMessages() {
  103. let completionHandlerExpectation = expectation(description: "completion handler called")
  104. let call = try! client.collect { callResult in
  105. XCTAssertEqual(.ok, callResult.statusCode)
  106. completionHandlerExpectation.fulfill()
  107. }
  108. for string in EchoTests.lotsOfStrings {
  109. let sendExpectation = expectation(description: "send completion handler \(string) called")
  110. try! call.send(Echo_EchoRequest(text: string)) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  111. }
  112. call.waitForSendOperationsToFinish()
  113. let response = try! call.closeAndReceive()
  114. XCTAssertEqual("Swift echo collect: " + EchoTests.lotsOfStrings.joined(separator: " "), response.text)
  115. waitForExpectations(timeout: defaultTimeout)
  116. }
  117. }
  118. extension EchoTests {
  119. func testServerStreaming() {
  120. let completionHandlerExpectation = expectation(description: "completion handler called")
  121. let call = try! client.expand(Echo_EchoRequest(text: "foo bar baz")) { callResult in
  122. XCTAssertEqual(.ok, callResult.statusCode)
  123. completionHandlerExpectation.fulfill()
  124. }
  125. XCTAssertEqual("Swift echo expand (0): foo", try! call.receive().text)
  126. XCTAssertEqual("Swift echo expand (1): bar", try! call.receive().text)
  127. XCTAssertEqual("Swift echo expand (2): baz", try! call.receive().text)
  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. waitForExpectations(timeout: defaultTimeout)
  140. }
  141. }
  142. extension EchoTests {
  143. func testBidirectionalStreamingBatched() {
  144. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  145. let call = try! client.update { callResult in
  146. XCTAssertEqual(.ok, callResult.statusCode)
  147. finalCompletionHandlerExpectation.fulfill()
  148. }
  149. var sendExpectation = expectation(description: "send completion handler 1 called")
  150. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  151. sendExpectation = expectation(description: "send completion handler 2 called")
  152. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  153. sendExpectation = expectation(description: "send completion handler 3 called")
  154. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  155. call.waitForSendOperationsToFinish()
  156. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  157. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  158. XCTAssertEqual("Swift echo update (0): foo", try! call.receive().text)
  159. XCTAssertEqual("Swift echo update (1): bar", try! call.receive().text)
  160. XCTAssertEqual("Swift echo update (2): baz", try! call.receive().text)
  161. waitForExpectations(timeout: defaultTimeout)
  162. }
  163. func testBidirectionalStreamingPingPong() {
  164. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  165. let call = try! client.update { callResult in
  166. XCTAssertEqual(.ok, callResult.statusCode)
  167. finalCompletionHandlerExpectation.fulfill()
  168. }
  169. var sendExpectation = expectation(description: "send completion handler 1 called")
  170. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  171. XCTAssertEqual("Swift echo update (0): foo", try! call.receive().text)
  172. sendExpectation = expectation(description: "send completion handler 2 called")
  173. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  174. XCTAssertEqual("Swift echo update (1): bar", try! call.receive().text)
  175. sendExpectation = expectation(description: "send completion handler 3 called")
  176. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  177. XCTAssertEqual("Swift echo update (2): baz", try! call.receive().text)
  178. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  179. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  180. waitForExpectations(timeout: defaultTimeout)
  181. }
  182. func testBidirectionalStreamingLotsOfMessagesBatched() {
  183. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  184. let call = try! client.update { callResult in
  185. XCTAssertEqual(.ok, callResult.statusCode)
  186. finalCompletionHandlerExpectation.fulfill()
  187. }
  188. for string in EchoTests.lotsOfStrings {
  189. let sendExpectation = expectation(description: "send completion handler \(string) called")
  190. try! call.send(Echo_EchoRequest(text: string)) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  191. }
  192. call.waitForSendOperationsToFinish()
  193. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  194. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  195. for string in EchoTests.lotsOfStrings {
  196. XCTAssertEqual("Swift echo update (\(string)): \(string)", try! call.receive().text)
  197. }
  198. waitForExpectations(timeout: defaultTimeout)
  199. }
  200. func testBidirectionalStreamingLotsOfMessagesPingPong() {
  201. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  202. let call = try! client.update { callResult in
  203. XCTAssertEqual(.ok, callResult.statusCode)
  204. finalCompletionHandlerExpectation.fulfill()
  205. }
  206. for string in EchoTests.lotsOfStrings {
  207. let sendExpectation = expectation(description: "send completion handler \(string) called")
  208. try! call.send(Echo_EchoRequest(text: string)) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  209. XCTAssertEqual("Swift echo update (\(string)): \(string)", try! call.receive().text)
  210. }
  211. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  212. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  213. waitForExpectations(timeout: defaultTimeout)
  214. }
  215. }