EchoTests.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. XCTAssertNil(try! call.receive())
  129. waitForExpectations(timeout: defaultTimeout)
  130. }
  131. func testServerStreamingLotsOfMessages() {
  132. let completionHandlerExpectation = expectation(description: "completion handler called")
  133. let call = try! client.expand(Echo_EchoRequest(text: EchoTests.lotsOfStrings.joined(separator: " "))) { callResult in
  134. XCTAssertEqual(.ok, callResult.statusCode)
  135. completionHandlerExpectation.fulfill()
  136. }
  137. for string in EchoTests.lotsOfStrings {
  138. XCTAssertEqual("Swift echo expand (\(string)): \(string)", try! call.receive()!.text)
  139. }
  140. XCTAssertNil(try! call.receive())
  141. waitForExpectations(timeout: defaultTimeout)
  142. }
  143. }
  144. extension EchoTests {
  145. func testBidirectionalStreamingBatched() {
  146. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  147. let call = try! client.update { callResult in
  148. XCTAssertEqual(.ok, callResult.statusCode)
  149. finalCompletionHandlerExpectation.fulfill()
  150. }
  151. var sendExpectation = expectation(description: "send completion handler 1 called")
  152. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  153. sendExpectation = expectation(description: "send completion handler 2 called")
  154. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  155. sendExpectation = expectation(description: "send completion handler 3 called")
  156. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  157. call.waitForSendOperationsToFinish()
  158. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  159. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  160. XCTAssertEqual("Swift echo update (0): foo", try! call.receive()!.text)
  161. XCTAssertEqual("Swift echo update (1): bar", try! call.receive()!.text)
  162. XCTAssertEqual("Swift echo update (2): baz", try! call.receive()!.text)
  163. XCTAssertNil(try! call.receive())
  164. waitForExpectations(timeout: defaultTimeout)
  165. }
  166. func testBidirectionalStreamingPingPong() {
  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. var sendExpectation = expectation(description: "send completion handler 1 called")
  173. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  174. XCTAssertEqual("Swift echo update (0): foo", try! call.receive()!.text)
  175. sendExpectation = expectation(description: "send completion handler 2 called")
  176. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  177. XCTAssertEqual("Swift echo update (1): bar", try! call.receive()!.text)
  178. sendExpectation = expectation(description: "send completion handler 3 called")
  179. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  180. XCTAssertEqual("Swift echo update (2): baz", try! call.receive()!.text)
  181. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  182. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  183. XCTAssertNil(try! call.receive())
  184. waitForExpectations(timeout: defaultTimeout)
  185. }
  186. func testBidirectionalStreamingLotsOfMessagesBatched() {
  187. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  188. let call = try! client.update { callResult in
  189. XCTAssertEqual(.ok, callResult.statusCode)
  190. finalCompletionHandlerExpectation.fulfill()
  191. }
  192. for string in EchoTests.lotsOfStrings {
  193. let sendExpectation = expectation(description: "send completion handler \(string) called")
  194. try! call.send(Echo_EchoRequest(text: string)) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  195. }
  196. call.waitForSendOperationsToFinish()
  197. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  198. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  199. for string in EchoTests.lotsOfStrings {
  200. XCTAssertEqual("Swift echo update (\(string)): \(string)", try! call.receive()!.text)
  201. }
  202. XCTAssertNil(try! call.receive())
  203. waitForExpectations(timeout: defaultTimeout)
  204. }
  205. func testBidirectionalStreamingLotsOfMessagesPingPong() {
  206. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  207. let call = try! client.update { callResult in
  208. XCTAssertEqual(.ok, callResult.statusCode)
  209. finalCompletionHandlerExpectation.fulfill()
  210. }
  211. for string in EchoTests.lotsOfStrings {
  212. let sendExpectation = expectation(description: "send completion handler \(string) called")
  213. try! call.send(Echo_EchoRequest(text: string)) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  214. XCTAssertEqual("Swift echo update (\(string)): \(string)", try! call.receive()!.text)
  215. }
  216. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  217. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  218. XCTAssertNil(try! call.receive())
  219. waitForExpectations(timeout: defaultTimeout)
  220. }
  221. }