NormalizationTests.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2021, 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 NIO
  18. import XCTest
  19. /// These tests validate that:
  20. /// - we can compile generated code for functions with same (case-insensitive) name (providing they
  21. /// are generated with 'KeepMethodCasing=true')
  22. /// - the right client function calls the server function with the expected casing.
  23. final class NormalizationTests: GRPCTestCase {
  24. var group: EventLoopGroup!
  25. var server: Server!
  26. var channel: ClientConnection!
  27. override func setUp() {
  28. super.setUp()
  29. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  30. self.server = try! Server.insecure(group: self.group)
  31. .withLogger(self.serverLogger)
  32. .withServiceProviders([NormalizationProvider()])
  33. .bind(host: "localhost", port: 0)
  34. .wait()
  35. self.channel = ClientConnection.insecure(group: self.group)
  36. .withBackgroundActivityLogger(self.clientLogger)
  37. .connect(host: "localhost", port: self.server.channel.localAddress!.port!)
  38. }
  39. override func tearDown() {
  40. XCTAssertNoThrow(try self.channel.close().wait())
  41. XCTAssertNoThrow(try self.server.initiateGracefulShutdown().wait())
  42. XCTAssertNoThrow(try self.group.syncShutdownGracefully())
  43. super.tearDown()
  44. }
  45. func testUnary() throws {
  46. let client = Normalization_NormalizationClient(channel: channel)
  47. let unary1 = client.unary(.init())
  48. let response1 = try unary1.response.wait()
  49. XCTAssert(response1.functionName.starts(with: "unary"))
  50. let unary2 = client.Unary(.init())
  51. let response2 = try unary2.response.wait()
  52. XCTAssert(response2.functionName.starts(with: "Unary"))
  53. }
  54. func testClientStreaming() throws {
  55. let client = Normalization_NormalizationClient(channel: channel)
  56. let clientStreaming1 = client.clientStreaming()
  57. clientStreaming1.sendEnd(promise: nil)
  58. let response1 = try clientStreaming1.response.wait()
  59. XCTAssert(response1.functionName.starts(with: "clientStreaming"))
  60. let clientStreaming2 = client.ClientStreaming()
  61. clientStreaming2.sendEnd(promise: nil)
  62. let response2 = try clientStreaming2.response.wait()
  63. XCTAssert(response2.functionName.starts(with: "ClientStreaming"))
  64. }
  65. func testServerStreaming() throws {
  66. let client = Normalization_NormalizationClient(channel: channel)
  67. let serverStreaming1 = client.serverStreaming(.init()) {
  68. XCTAssert($0.functionName.starts(with: "serverStreaming"))
  69. }
  70. XCTAssertEqual(try serverStreaming1.status.wait(), .ok)
  71. let serverStreaming2 = client.ServerStreaming(.init()) {
  72. XCTAssert($0.functionName.starts(with: "ServerStreaming"))
  73. }
  74. XCTAssertEqual(try serverStreaming2.status.wait(), .ok)
  75. }
  76. func testBidirectionalStreaming() throws {
  77. let client = Normalization_NormalizationClient(channel: channel)
  78. let bidirectionalStreaming1 = client.bidirectionalStreaming {
  79. XCTAssert($0.functionName.starts(with: "bidirectionalStreaming"))
  80. }
  81. bidirectionalStreaming1.sendEnd(promise: nil)
  82. XCTAssertEqual(try bidirectionalStreaming1.status.wait(), .ok)
  83. let bidirectionalStreaming2 = client.BidirectionalStreaming {
  84. XCTAssert($0.functionName.starts(with: "BidirectionalStreaming"))
  85. }
  86. bidirectionalStreaming2.sendEnd(promise: nil)
  87. XCTAssertEqual(try bidirectionalStreaming2.status.wait(), .ok)
  88. }
  89. }