StreamingRequestClientCallTests.swift 1.7 KB

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