ClientTestExample.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 testClientStreaming() {
  84. let inputStrings = ["foo", "bar", "baz"]
  85. let fakeService = Echo_EchoServiceTestStub()
  86. let fakeCall = Echo_EchoCollectCallTestStub()
  87. fakeCall.output = Echo_EchoResponse(text: "response")
  88. fakeService.collectCalls.append(fakeCall)
  89. let client = ClientUnderTest(service: fakeService)
  90. XCTAssertEqual("response", try client.collectWords(inputStrings))
  91. // Ensure that the expected requests have been sent.
  92. XCTAssertEqual(inputStrings.map { Echo_EchoRequest(text: $0) }, fakeCall.inputs)
  93. }
  94. func testServerStreaming() {
  95. let outputStrings = ["foo", "bar", "baz"]
  96. let fakeService = Echo_EchoServiceTestStub()
  97. let fakeCall = Echo_EchoExpandCallTestStub()
  98. fakeCall.outputs = outputStrings.map { Echo_EchoResponse(text: $0) }
  99. fakeService.expandCalls.append(fakeCall)
  100. let client = ClientUnderTest(service: fakeService)
  101. XCTAssertEqual(outputStrings, try client.expandWords("inputWord"))
  102. // Ensure that all responses have been consumed.
  103. XCTAssertEqual(0, fakeCall.outputs.count)
  104. // Ensure that the expected requests have been sent.
  105. XCTAssertEqual([Echo_EchoRequest(text: "inputWord")], fakeService.expandRequests)
  106. }
  107. func testBidirectionalStreaming() {
  108. let inputStrings = ["foo", "bar", "baz"]
  109. let outputStrings = ["foo2", "bar2", "baz2"]
  110. let fakeService = Echo_EchoServiceTestStub()
  111. let fakeCall = Echo_EchoUpdateCallTestStub()
  112. fakeCall.outputs = outputStrings.map { Echo_EchoResponse(text: $0) }
  113. fakeService.updateCalls.append(fakeCall)
  114. let client = ClientUnderTest(service: fakeService)
  115. XCTAssertEqual(outputStrings, try client.updateWords(inputStrings))
  116. // Ensure that all responses have been consumed.
  117. XCTAssertEqual(0, fakeCall.outputs.count)
  118. // Ensure that the expected requests have been sent.
  119. XCTAssertEqual(inputStrings.map { Echo_EchoRequest(text: $0) }, fakeCall.inputs)
  120. }
  121. }