ServerContextTests.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 Testing
  18. @Suite("ServerContext")
  19. struct ServerContextTests {
  20. @Suite("CancellationHandle")
  21. struct CancellationHandle {
  22. @Test("Is cancelled")
  23. func isCancelled() async throws {
  24. await withServerContextRPCCancellationHandle { handle in
  25. #expect(!handle.isCancelled)
  26. handle.cancel()
  27. #expect(handle.isCancelled)
  28. }
  29. }
  30. @Test("Wait for cancellation")
  31. func waitForCancellation() async throws {
  32. await withServerContextRPCCancellationHandle { handle in
  33. await withTaskGroup(of: Void.self) { group in
  34. group.addTask {
  35. try? await handle.cancelled
  36. }
  37. handle.cancel()
  38. await group.waitForAll()
  39. }
  40. }
  41. }
  42. @Test("Binds task local")
  43. func bindsTaskLocal() async throws {
  44. await withServerContextRPCCancellationHandle { handle in
  45. let signal = AsyncStream.makeStream(of: Void.self)
  46. await withRPCCancellationHandler {
  47. handle.cancel()
  48. for await _ in signal.stream {}
  49. } onCancelRPC: {
  50. // If the task local wasn't bound, this wouldn't run.
  51. signal.continuation.finish()
  52. }
  53. }
  54. }
  55. }
  56. }