2
0

NormalizationTests.swift 3.8 KB

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