EchoTests.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 gRPC
  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. #if os(Linux)
  68. // Having to sleep here is really not a good solution,
  69. // but it appears to help with fixing test flakes under Linux.
  70. Thread.sleep(forTimeInterval: 1.0)
  71. #endif
  72. super.tearDown()
  73. }
  74. }
  75. // Currently broken and thus commented out.
  76. // TODO(danielalm): Fix these.
  77. //class EchoTestsSecure: EchoTests {
  78. // override var secure: Bool { return true }
  79. //}
  80. extension EchoTests {
  81. func testUnary() {
  82. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  83. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  84. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  85. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  86. XCTAssertEqual("Swift echo get: foo", try! client.get(Echo_EchoRequest(text: "foo")).text)
  87. }
  88. }
  89. extension EchoTests {
  90. func testClientStreaming() {
  91. let completionHandlerExpectation = expectation(description: "final completion handler called")
  92. let call = try! client.collect { callResult in
  93. XCTAssertEqual(.ok, callResult.statusCode)
  94. completionHandlerExpectation.fulfill()
  95. }
  96. var sendExpectation = expectation(description: "send completion handler 1 called")
  97. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  98. sendExpectation = expectation(description: "send completion handler 2 called")
  99. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  100. sendExpectation = expectation(description: "send completion handler 3 called")
  101. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  102. call.waitForSendOperationsToFinish()
  103. let response = try! call.closeAndReceive()
  104. XCTAssertEqual("Swift echo collect: foo bar baz", response.text)
  105. waitForExpectations(timeout: defaultTimeout)
  106. }
  107. func testClientStreamingLotsOfMessages() {
  108. let completionHandlerExpectation = expectation(description: "completion handler called")
  109. let call = try! client.collect { callResult in
  110. XCTAssertEqual(.ok, callResult.statusCode)
  111. completionHandlerExpectation.fulfill()
  112. }
  113. for string in EchoTests.lotsOfStrings {
  114. let sendExpectation = expectation(description: "send completion handler \(string) called")
  115. try! call.send(Echo_EchoRequest(text: string)) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  116. }
  117. call.waitForSendOperationsToFinish()
  118. let response = try! call.closeAndReceive()
  119. XCTAssertEqual("Swift echo collect: " + EchoTests.lotsOfStrings.joined(separator: " "), response.text)
  120. waitForExpectations(timeout: defaultTimeout)
  121. }
  122. }
  123. extension EchoTests {
  124. func testServerStreaming() {
  125. let completionHandlerExpectation = expectation(description: "completion handler called")
  126. let call = try! client.expand(Echo_EchoRequest(text: "foo bar baz")) { callResult in
  127. XCTAssertEqual(.ok, callResult.statusCode)
  128. completionHandlerExpectation.fulfill()
  129. }
  130. XCTAssertEqual("Swift echo expand (0): foo", try! call.receive().text)
  131. XCTAssertEqual("Swift echo expand (1): bar", try! call.receive().text)
  132. XCTAssertEqual("Swift echo expand (2): baz", try! call.receive().text)
  133. waitForExpectations(timeout: defaultTimeout)
  134. }
  135. func testServerStreamingLotsOfMessages() {
  136. let completionHandlerExpectation = expectation(description: "completion handler called")
  137. let call = try! client.expand(Echo_EchoRequest(text: EchoTests.lotsOfStrings.joined(separator: " "))) { callResult in
  138. XCTAssertEqual(.ok, callResult.statusCode)
  139. completionHandlerExpectation.fulfill()
  140. }
  141. for string in EchoTests.lotsOfStrings {
  142. XCTAssertEqual("Swift echo expand (\(string)): \(string)", try! call.receive().text)
  143. }
  144. waitForExpectations(timeout: defaultTimeout)
  145. }
  146. }
  147. extension EchoTests {
  148. func testBidirectionalStreamingBatched() {
  149. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  150. let call = try! client.update { callResult in
  151. XCTAssertEqual(.ok, callResult.statusCode)
  152. finalCompletionHandlerExpectation.fulfill()
  153. }
  154. var sendExpectation = expectation(description: "send completion handler 1 called")
  155. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  156. sendExpectation = expectation(description: "send completion handler 2 called")
  157. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  158. sendExpectation = expectation(description: "send completion handler 3 called")
  159. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  160. call.waitForSendOperationsToFinish()
  161. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  162. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  163. XCTAssertEqual("Swift echo update (0): foo", try! call.receive().text)
  164. XCTAssertEqual("Swift echo update (1): bar", try! call.receive().text)
  165. XCTAssertEqual("Swift echo update (2): baz", try! call.receive().text)
  166. waitForExpectations(timeout: defaultTimeout)
  167. }
  168. func testBidirectionalStreamingPingPong() {
  169. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  170. let call = try! client.update { callResult in
  171. XCTAssertEqual(.ok, callResult.statusCode)
  172. finalCompletionHandlerExpectation.fulfill()
  173. }
  174. var sendExpectation = expectation(description: "send completion handler 1 called")
  175. try! call.send(Echo_EchoRequest(text: "foo")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  176. XCTAssertEqual("Swift echo update (0): foo", try! call.receive().text)
  177. sendExpectation = expectation(description: "send completion handler 2 called")
  178. try! call.send(Echo_EchoRequest(text: "bar")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  179. XCTAssertEqual("Swift echo update (1): bar", try! call.receive().text)
  180. sendExpectation = expectation(description: "send completion handler 3 called")
  181. try! call.send(Echo_EchoRequest(text: "baz")) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  182. XCTAssertEqual("Swift echo update (2): baz", try! call.receive().text)
  183. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  184. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  185. waitForExpectations(timeout: defaultTimeout)
  186. }
  187. func testBidirectionalStreamingLotsOfMessagesBatched() {
  188. let finalCompletionHandlerExpectation = expectation(description: "final completion handler called")
  189. let call = try! client.update { callResult in
  190. XCTAssertEqual(.ok, callResult.statusCode)
  191. finalCompletionHandlerExpectation.fulfill()
  192. }
  193. for string in EchoTests.lotsOfStrings {
  194. let sendExpectation = expectation(description: "send completion handler \(string) called")
  195. try! call.send(Echo_EchoRequest(text: string)) { [sendExpectation] in XCTAssertNil($0); sendExpectation.fulfill() }
  196. }
  197. call.waitForSendOperationsToFinish()
  198. let closeCompletionHandlerExpectation = expectation(description: "close completion handler called")
  199. try! call.closeSend { closeCompletionHandlerExpectation.fulfill() }
  200. for string in EchoTests.lotsOfStrings {
  201. XCTAssertEqual("Swift echo update (\(string)): \(string)", try! call.receive().text)
  202. }
  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. waitForExpectations(timeout: defaultTimeout)
  219. }
  220. }