RPCRouterTests.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright 2023, 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 GRPCCore
  17. import XCTest
  18. final class RPCRouterTests: XCTestCase {
  19. func testEmptyRouter() async throws {
  20. var router = RPCRouter<NoServerTransport>()
  21. XCTAssertEqual(router.count, 0)
  22. XCTAssertEqual(router.methods, [])
  23. XCTAssertFalse(
  24. router.hasHandler(forMethod: MethodDescriptor(fullyQualifiedService: "foo", method: "bar"))
  25. )
  26. XCTAssertFalse(
  27. router.removeHandler(forMethod: MethodDescriptor(fullyQualifiedService: "foo", method: "bar"))
  28. )
  29. }
  30. func testRegisterMethod() async throws {
  31. var router = RPCRouter<NoServerTransport>()
  32. let method = MethodDescriptor(fullyQualifiedService: "foo", method: "bar")
  33. router.registerHandler(
  34. forMethod: method,
  35. deserializer: IdentityDeserializer(),
  36. serializer: IdentitySerializer()
  37. ) { _, _ in
  38. throw RPCError(code: .failedPrecondition, message: "Shouldn't be called")
  39. }
  40. XCTAssertEqual(router.count, 1)
  41. XCTAssertEqual(router.methods, [method])
  42. XCTAssertTrue(router.hasHandler(forMethod: method))
  43. }
  44. func testRemoveMethod() async throws {
  45. var router = RPCRouter<NoServerTransport>()
  46. let method = MethodDescriptor(fullyQualifiedService: "foo", method: "bar")
  47. router.registerHandler(
  48. forMethod: method,
  49. deserializer: IdentityDeserializer(),
  50. serializer: IdentitySerializer()
  51. ) { _, _ in
  52. throw RPCError(code: .failedPrecondition, message: "Shouldn't be called")
  53. }
  54. XCTAssertTrue(router.removeHandler(forMethod: method))
  55. XCTAssertFalse(router.hasHandler(forMethod: method))
  56. XCTAssertEqual(router.count, 0)
  57. XCTAssertEqual(router.methods, [])
  58. }
  59. }
  60. struct NoServerTransport: ServerTransport {
  61. typealias Bytes = [UInt8]
  62. func listen(
  63. streamHandler: @escaping @Sendable (
  64. GRPCCore.RPCStream<Inbound, Outbound>,
  65. GRPCCore.ServerContext
  66. ) async -> Void
  67. ) async throws {
  68. }
  69. func beginGracefulShutdown() {
  70. }
  71. }