RPCRouterTests.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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()
  21. XCTAssertEqual(router.count, 0)
  22. XCTAssertEqual(router.methods, [])
  23. XCTAssertFalse(router.hasHandler(forMethod: MethodDescriptor(service: "foo", method: "bar")))
  24. XCTAssertFalse(router.removeHandler(forMethod: MethodDescriptor(service: "foo", method: "bar")))
  25. }
  26. func testRegisterMethod() async throws {
  27. var router = RPCRouter()
  28. let method = MethodDescriptor(service: "foo", method: "bar")
  29. router.registerHandler(
  30. forMethod: method,
  31. deserializer: IdentityDeserializer(),
  32. serializer: IdentitySerializer()
  33. ) { _, _ in
  34. throw RPCError(code: .failedPrecondition, message: "Shouldn't be called")
  35. }
  36. XCTAssertEqual(router.count, 1)
  37. XCTAssertEqual(router.methods, [method])
  38. XCTAssertTrue(router.hasHandler(forMethod: method))
  39. }
  40. func testRemoveMethod() async throws {
  41. var router = RPCRouter()
  42. let method = MethodDescriptor(service: "foo", method: "bar")
  43. router.registerHandler(
  44. forMethod: method,
  45. deserializer: IdentityDeserializer(),
  46. serializer: IdentitySerializer()
  47. ) { _, _ in
  48. throw RPCError(code: .failedPrecondition, message: "Shouldn't be called")
  49. }
  50. XCTAssertTrue(router.removeHandler(forMethod: method))
  51. XCTAssertFalse(router.hasHandler(forMethod: method))
  52. XCTAssertEqual(router.count, 0)
  53. XCTAssertEqual(router.methods, [])
  54. }
  55. }