ClientTestExample.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. extension ClientTestExample {
  64. func testUnary() {
  65. let fakeService = Echo_EchoServiceTestStub()
  66. fakeService.getResponses.append(Echo_EchoResponse(text: "bar"))
  67. let client = ClientUnderTest(service: fakeService)
  68. XCTAssertEqual("bar", try client.getWord("foo"))
  69. // Ensure that all responses have been consumed.
  70. XCTAssertEqual(0, fakeService.getResponses.count)
  71. // Ensure that the expected requests have been sent.
  72. XCTAssertEqual([Echo_EchoRequest(text: "foo")], fakeService.getRequests)
  73. }
  74. func testClientStreaming() {
  75. let inputStrings = ["foo", "bar", "baz"]
  76. let fakeService = Echo_EchoServiceTestStub()
  77. let fakeCall = Echo_EchoCollectCallTestStub()
  78. fakeCall.output = Echo_EchoResponse(text: "response")
  79. fakeService.collectCalls.append(fakeCall)
  80. let client = ClientUnderTest(service: fakeService)
  81. XCTAssertEqual("response", try client.collectWords(inputStrings))
  82. // Ensure that the expected requests have been sent.
  83. XCTAssertEqual(inputStrings.map { Echo_EchoRequest(text: $0) }, fakeCall.inputs)
  84. }
  85. func testServerStreaming() {
  86. let outputStrings = ["foo", "bar", "baz"]
  87. let fakeService = Echo_EchoServiceTestStub()
  88. let fakeCall = Echo_EchoExpandCallTestStub()
  89. fakeCall.outputs = outputStrings.map { Echo_EchoResponse(text: $0) }
  90. fakeService.expandCalls.append(fakeCall)
  91. let client = ClientUnderTest(service: fakeService)
  92. XCTAssertEqual(outputStrings, try client.expandWords("inputWord"))
  93. // Ensure that all responses have been consumed.
  94. XCTAssertEqual(0, fakeCall.outputs.count)
  95. // Ensure that the expected requests have been sent.
  96. XCTAssertEqual([Echo_EchoRequest(text: "inputWord")], fakeService.expandRequests)
  97. }
  98. func testBidirectionalStreaming() {
  99. let inputStrings = ["foo", "bar", "baz"]
  100. let outputStrings = ["foo2", "bar2", "baz2"]
  101. let fakeService = Echo_EchoServiceTestStub()
  102. let fakeCall = Echo_EchoUpdateCallTestStub()
  103. fakeCall.outputs = outputStrings.map { Echo_EchoResponse(text: $0) }
  104. fakeService.updateCalls.append(fakeCall)
  105. let client = ClientUnderTest(service: fakeService)
  106. XCTAssertEqual(outputStrings, try client.updateWords(inputStrings))
  107. // Ensure that all responses have been consumed.
  108. XCTAssertEqual(0, fakeCall.outputs.count)
  109. // Ensure that the expected requests have been sent.
  110. XCTAssertEqual(inputStrings.map { Echo_EchoRequest(text: $0) }, fakeCall.inputs)
  111. }
  112. }