ClientServerWithMethods.swift 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright 2024, 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 GRPCInProcessTransport
  18. import Testing
  19. @Suite("withGRPCServer / withGRPCClient")
  20. struct WithMethods {
  21. @Test("Actor isolation")
  22. @available(gRPCSwift 2.0, *)
  23. func actorIsolation() async throws {
  24. let testActor = TestActor()
  25. #expect(await !testActor.hasRun)
  26. try await testActor.run()
  27. #expect(await testActor.hasRun)
  28. }
  29. }
  30. @available(gRPCSwift 2.0, *)
  31. fileprivate actor TestActor {
  32. private(set) var hasRun = false
  33. func run() async throws {
  34. let inProcess = InProcessTransport()
  35. try await withGRPCServer(transport: inProcess.server, services: []) { server in
  36. do {
  37. try await withGRPCClient(transport: inProcess.client) { client in
  38. self.hasRun = true
  39. }
  40. } catch {
  41. // Starting the client can race with the closure returning which begins graceful shutdown.
  42. // If that happens the client run method will throw an error as the client is being run
  43. // when it's already been shutdown. That's okay and expected so rather than slowing down
  44. // the closure tolerate that specific error.
  45. if let error = error as? RuntimeError {
  46. #expect(error.code == .clientIsStopped)
  47. } else {
  48. Issue.record(error)
  49. }
  50. }
  51. }
  52. }
  53. }