ClientTestExample.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // Sample test suite to demonstrate how one would test client code that
  21. // uses an object that implements the `...Service` protocol.
  22. // These tests don't really test the logic of the SwiftGRPC library, but are meant
  23. // as an example of how one would go about testing their own client/server code that
  24. // relies on SwiftGRPC.
  25. fileprivate class ClientUnderTest {
  26. let service: Echo_EchoService
  27. init(service: Echo_EchoService) {
  28. self.service = service
  29. }
  30. func getWord(_ input: String) throws -> String {
  31. return try service.get(Echo_EchoRequest(text: input)).text
  32. }
  33. func collectWords(_ input: [String]) throws -> String {
  34. let call = try service.collect(completion: nil)
  35. for text in input {
  36. try call.send(Echo_EchoRequest(text: text), completion: { _ in })
  37. }
  38. call.waitForSendOperationsToFinish()
  39. return try call.closeAndReceive().text
  40. }
  41. func expandWords(_ input: String) throws -> [String] {
  42. let call = try service.expand(Echo_EchoRequest(text: input), completion: nil)
  43. var results: [String] = []
  44. while let response = try call.receive() {
  45. results.append(response.text)
  46. }
  47. return results
  48. }
  49. func updateWords(_ input: [String]) throws -> [String] {
  50. let call = try service.update(completion: nil)
  51. for text in input {
  52. try call.send(Echo_EchoRequest(text: text), completion: { _ in })
  53. }
  54. call.waitForSendOperationsToFinish()
  55. var results: [String] = []
  56. while let response = try call.receive() {
  57. results.append(response.text)
  58. }
  59. return results
  60. }
  61. }
  62. class ClientTestExample: XCTestCase {
  63. static var allTests: [(String, (ClientTestExample) -> () throws -> Void)] {
  64. return [
  65. ("testUnary", testUnary),
  66. ("testClientStreaming", testClientStreaming),
  67. ("testServerStreaming", testServerStreaming),
  68. ("testBidirectionalStreaming", testBidirectionalStreaming)
  69. ]
  70. }
  71. }
  72. extension ClientTestExample {
  73. func testUnary() {
  74. let fakeService = Echo_EchoServiceTestStub()
  75. fakeService.getResponses.append(Echo_EchoResponse(text: "bar"))
  76. let client = ClientUnderTest(service: fakeService)
  77. XCTAssertEqual("bar", try client.getWord("foo"))
  78. // Ensure that all responses have been consumed.
  79. XCTAssertEqual(0, fakeService.getResponses.count)
  80. // Ensure that the expected requests have been sent.
  81. XCTAssertEqual([Echo_EchoRequest(text: "foo")], fakeService.getRequests)
  82. }
  83. func testClientAsynchronous() throws {
  84. let fakeService = Echo_EchoServiceTestStub()
  85. fakeService.getResponses.append(Echo_EchoResponse(text: "bar"))
  86. let client = ClientUnderTest(service: fakeService)
  87. let completionHandlerExpectation = expectation(description: "request completion handler called")
  88. _ = try client.service.get(Echo_EchoRequest(text: "foo")) { response, _ in
  89. XCTAssertEqual("bar", response?.text)
  90. completionHandlerExpectation.fulfill()
  91. }
  92. waitForExpectations(timeout: 1)
  93. // Ensure that all responses have been consumed.
  94. XCTAssertEqual(0, fakeService.getResponses.count)
  95. // Ensure that the expected requests have been sent.
  96. XCTAssertEqual([Echo_EchoRequest(text: "foo")], fakeService.getRequests)
  97. }
  98. func testClientStreaming() {
  99. let inputStrings = ["foo", "bar", "baz"]
  100. let fakeService = Echo_EchoServiceTestStub()
  101. let fakeCall = Echo_EchoCollectCallTestStub()
  102. fakeCall.output = Echo_EchoResponse(text: "response")
  103. fakeService.collectCalls.append(fakeCall)
  104. let client = ClientUnderTest(service: fakeService)
  105. XCTAssertEqual("response", try client.collectWords(inputStrings))
  106. // Ensure that the expected requests have been sent.
  107. XCTAssertEqual(inputStrings.map { Echo_EchoRequest(text: $0) }, fakeCall.inputs)
  108. }
  109. func testServerStreaming() {
  110. let outputStrings = ["foo", "bar", "baz"]
  111. let fakeService = Echo_EchoServiceTestStub()
  112. let fakeCall = Echo_EchoExpandCallTestStub()
  113. fakeCall.outputs = outputStrings.map { Echo_EchoResponse(text: $0) }
  114. fakeService.expandCalls.append(fakeCall)
  115. let client = ClientUnderTest(service: fakeService)
  116. XCTAssertEqual(outputStrings, try client.expandWords("inputWord"))
  117. // Ensure that all responses have been consumed.
  118. XCTAssertEqual(0, fakeCall.outputs.count)
  119. // Ensure that the expected requests have been sent.
  120. XCTAssertEqual([Echo_EchoRequest(text: "inputWord")], fakeService.expandRequests)
  121. }
  122. func testBidirectionalStreaming() {
  123. let inputStrings = ["foo", "bar", "baz"]
  124. let outputStrings = ["foo2", "bar2", "baz2"]
  125. let fakeService = Echo_EchoServiceTestStub()
  126. let fakeCall = Echo_EchoUpdateCallTestStub()
  127. fakeCall.outputs = outputStrings.map { Echo_EchoResponse(text: $0) }
  128. fakeService.updateCalls.append(fakeCall)
  129. let client = ClientUnderTest(service: fakeService)
  130. XCTAssertEqual(outputStrings, try client.updateWords(inputStrings))
  131. // Ensure that all responses have been consumed.
  132. XCTAssertEqual(0, fakeCall.outputs.count)
  133. // Ensure that the expected requests have been sent.
  134. XCTAssertEqual(inputStrings.map { Echo_EchoRequest(text: $0) }, fakeCall.inputs)
  135. }
  136. }