ClientServerWithMethods.swift 1.8 KB

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