ServerCancellationManagerTests.swift 2.7 KB

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