FakeChannelTests.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2020, 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 GRPC
  17. import EchoModel
  18. import NIO
  19. import XCTest
  20. class FakeChannelTests: GRPCTestCase {
  21. typealias Request = Echo_EchoRequest
  22. typealias Response = Echo_EchoResponse
  23. var channel: FakeChannel!
  24. override func setUp() {
  25. self.channel = FakeChannel()
  26. }
  27. private func makeUnaryResponse(
  28. path: String = "/foo/bar",
  29. requestHandler: @escaping (FakeRequestPart<Request>) -> () = { _ in }
  30. ) -> FakeUnaryResponse<Request, Response> {
  31. return self.channel.makeFakeUnaryResponse(path: path, requestHandler: requestHandler)
  32. }
  33. private func makeStreamingResponse(
  34. path: String = "/foo/bar",
  35. requestHandler: @escaping (FakeRequestPart<Request>) -> () = { _ in }
  36. ) -> FakeStreamingResponse<Request, Response> {
  37. return self.channel.makeFakeStreamingResponse(path: path, requestHandler: requestHandler)
  38. }
  39. private func makeUnaryCall(
  40. request: Request,
  41. path: String = "/foo/bar",
  42. callOptions: CallOptions = CallOptions()
  43. ) -> UnaryCall<Request, Response> {
  44. return self.channel.makeUnaryCall(path: path, request: request, callOptions: callOptions)
  45. }
  46. private func makeBidirectionalStreamingCall(
  47. path: String = "/foo/bar",
  48. callOptions: CallOptions = CallOptions(),
  49. handler: @escaping (Response) -> ()
  50. ) -> BidirectionalStreamingCall<Request, Response> {
  51. return self.channel.makeBidirectionalStreamingCall(path: path, callOptions: callOptions, handler: handler)
  52. }
  53. func testUnary() {
  54. let response = self.makeUnaryResponse { part in
  55. switch part {
  56. case .message(let request):
  57. XCTAssertEqual(request, Request.with { $0.text = "Foo" })
  58. default:
  59. ()
  60. }
  61. }
  62. let call = self.makeUnaryCall(request: .with { $0.text = "Foo" })
  63. XCTAssertNoThrow(try response.sendMessage(.with { $0.text = "Bar" }))
  64. XCTAssertEqual(try call.response.wait(), .with { $0.text = "Bar"} )
  65. XCTAssertTrue(try call.status.map { $0.isOk }.wait())
  66. }
  67. func testBidirectional() {
  68. var requests: [Request] = []
  69. let response = self.makeStreamingResponse { part in
  70. switch part {
  71. case .message(let request):
  72. requests.append(request)
  73. default:
  74. ()
  75. }
  76. }
  77. var responses: [Response] = []
  78. let call = self.makeBidirectionalStreamingCall {
  79. responses.append($0)
  80. }
  81. XCTAssertNoThrow(try call.sendMessage(.with { $0.text = "1" }).wait())
  82. XCTAssertNoThrow(try call.sendMessage(.with { $0.text = "2" }).wait())
  83. XCTAssertNoThrow(try call.sendMessage(.with { $0.text = "3" }).wait())
  84. XCTAssertNoThrow(try call.sendEnd().wait())
  85. XCTAssertEqual(requests, (1...3).map { number in .with { $0.text = "\(number)" }})
  86. XCTAssertNoThrow(try response.sendMessage(.with { $0.text = "4" }))
  87. XCTAssertNoThrow(try response.sendMessage(.with { $0.text = "5" }))
  88. XCTAssertNoThrow(try response.sendMessage(.with { $0.text = "6" }))
  89. XCTAssertNoThrow(try response.sendEnd())
  90. XCTAssertEqual(responses, (4...6).map { number in .with { $0.text = "\(number)" }})
  91. XCTAssertTrue(try call.status.map { $0.isOk }.wait())
  92. }
  93. func testMissingResponse() {
  94. let call = self.makeUnaryCall(request: .with { $0.text = "Not going to work" })
  95. XCTAssertThrowsError(try call.initialMetadata.wait())
  96. XCTAssertThrowsError(try call.response.wait())
  97. XCTAssertThrowsError(try call.trailingMetadata.wait())
  98. XCTAssertFalse(try call.status.map { $0.isOk }.wait())
  99. }
  100. func testResponseIsReallyDequeued() {
  101. let response = self.makeUnaryResponse()
  102. let call = self.makeUnaryCall(request: .with { $0.text = "Ping" })
  103. XCTAssertNoThrow(try response.sendMessage(.with { $0.text = "Pong" }))
  104. XCTAssertEqual(try call.response.wait(), .with { $0.text = "Pong" })
  105. let failedCall = self.makeUnaryCall(request: .with { $0.text = "Not going to work" })
  106. XCTAssertThrowsError(try failedCall.initialMetadata.wait())
  107. XCTAssertThrowsError(try failedCall.response.wait())
  108. XCTAssertThrowsError(try failedCall.trailingMetadata.wait())
  109. XCTAssertFalse(try failedCall.status.map { $0.isOk }.wait())
  110. }
  111. }