StreamingRequestClientCallTests.swift 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright 2019, 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 EchoModel
  17. import Foundation
  18. import GRPC
  19. import XCTest
  20. class StreamingRequestClientCallTests: EchoTestCaseBase {
  21. class ResponseCounter {
  22. var expectation: XCTestExpectation
  23. init(expectation: XCTestExpectation) {
  24. self.expectation = expectation
  25. }
  26. func increment() {
  27. self.expectation.fulfill()
  28. }
  29. }
  30. func testSendMessages() throws {
  31. let messagesReceived = self.expectation(description: "messages received")
  32. let counter = ResponseCounter(expectation: messagesReceived)
  33. let update = self.client.update { _ in
  34. counter.increment()
  35. }
  36. // Send the first batch.
  37. let requests = ["foo", "bar", "baz"].map { Echo_EchoRequest(text: $0) }
  38. messagesReceived.expectedFulfillmentCount = requests.count
  39. XCTAssertNoThrow(try update.sendMessages(requests).wait())
  40. // Wait for the responses.
  41. self.wait(for: [messagesReceived], timeout: 0.5)
  42. let statusReceived = self.expectation(description: "status received")
  43. update.status.map { $0.code }.assertEqual(.ok, fulfill: statusReceived)
  44. // End the call.
  45. update.sendEnd(promise: nil)
  46. self.wait(for: [statusReceived], timeout: 0.5)
  47. }
  48. }