RPCRouterTests.swift 2.1 KB

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