ServerCancellationManagerTests.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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
  19. struct ServerCancellationManagerTests {
  20. @Test("Isn't cancelled after init")
  21. func isNotCancelled() {
  22. let manager = ServerCancellationManager()
  23. #expect(!manager.isRPCCancelled)
  24. }
  25. @Test("Is cancelled")
  26. func isCancelled() {
  27. let manager = ServerCancellationManager()
  28. manager.cancelRPC()
  29. #expect(manager.isRPCCancelled)
  30. }
  31. @Test("Cancellation handler runs")
  32. func addCancellationHandler() async throws {
  33. let manager = ServerCancellationManager()
  34. let signal = AsyncStream.makeStream(of: Void.self)
  35. let id = manager.addRPCCancelledHandler {
  36. signal.continuation.finish()
  37. }
  38. #expect(id != nil)
  39. manager.cancelRPC()
  40. let events: [Void] = await signal.stream.reduce(into: []) { $0.append($1) }
  41. #expect(events.isEmpty)
  42. }
  43. @Test("Cancellation handler runs immediately when already cancelled")
  44. func addCancellationHandlerAfterCancelled() async throws {
  45. let manager = ServerCancellationManager()
  46. let signal = AsyncStream.makeStream(of: Void.self)
  47. manager.cancelRPC()
  48. let id = manager.addRPCCancelledHandler {
  49. signal.continuation.finish()
  50. }
  51. #expect(id == nil)
  52. let events: [Void] = await signal.stream.reduce(into: []) { $0.append($1) }
  53. #expect(events.isEmpty)
  54. }
  55. @Test("Remove cancellation handler")
  56. func removeCancellationHandler() async throws {
  57. let manager = ServerCancellationManager()
  58. let id = manager.addRPCCancelledHandler {
  59. Issue.record("Unexpected cancellation")
  60. }
  61. #expect(id != nil)
  62. manager.removeRPCCancelledHandler(withID: id!)
  63. manager.cancelRPC()
  64. }
  65. @Test("Wait for cancellation")
  66. func waitForCancellation() async throws {
  67. let manager = ServerCancellationManager()
  68. try await withThrowingTaskGroup(of: Void.self) { group in
  69. group.addTask {
  70. try await manager.suspendUntilRPCIsCancelled()
  71. }
  72. manager.cancelRPC()
  73. try await group.waitForAll()
  74. }
  75. }
  76. }