|
| 1 | +import Testing |
| 2 | +@testable import OpenClaw |
| 3 | + |
| 4 | +private actor SimpleTaskSignal { |
| 5 | + private var signaled = false |
| 6 | + private var waiters: [CheckedContinuation<Void, Never>] = [] |
| 7 | + |
| 8 | + func signal() { |
| 9 | + self.signaled = true |
| 10 | + let waiters = self.waiters |
| 11 | + self.waiters.removeAll() |
| 12 | + for waiter in waiters { |
| 13 | + waiter.resume() |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + func wait() async { |
| 18 | + if self.signaled { |
| 19 | + return |
| 20 | + } |
| 21 | + await withCheckedContinuation { continuation in |
| 22 | + self.waiters.append(continuation) |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +private actor SimpleTaskOperationProbe { |
| 28 | + private var recordedCalls: [String] = [] |
| 29 | + |
| 30 | + func recordCall(_ value: String) { |
| 31 | + self.recordedCalls.append(value) |
| 32 | + } |
| 33 | + |
| 34 | + func calls() -> [String] { |
| 35 | + self.recordedCalls |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +struct SimpleTaskSupportTests { |
| 40 | + @Test |
| 41 | + @MainActor |
| 42 | + func `cancelling during sleep does not run another operation`() async { |
| 43 | + let sleepStarted = SimpleTaskSignal() |
| 44 | + let operation = SimpleTaskOperationProbe() |
| 45 | + var task: Task<Void, Never>? |
| 46 | + |
| 47 | + SimpleTaskSupport.startDetachedLoop( |
| 48 | + task: &task, |
| 49 | + interval: 60, |
| 50 | + sleep: { nanoseconds in |
| 51 | + await sleepStarted.signal() |
| 52 | + try await Task.sleep(nanoseconds: nanoseconds) |
| 53 | + }, |
| 54 | + operation: { |
| 55 | + await operation.recordCall("loop") |
| 56 | + }) |
| 57 | + |
| 58 | + await sleepStarted.wait() |
| 59 | + guard let runningTask = task else { |
| 60 | + Issue.record("detached loop did not start") |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + SimpleTaskSupport.stop(task: &task) |
| 65 | + await runningTask.value |
| 66 | + |
| 67 | + #expect(await operation.calls() == ["loop"]) |
| 68 | + #expect(task == nil) |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + @MainActor |
| 73 | + func `rescheduling cancels the superseded operation`() async { |
| 74 | + let firstSleepStarted = SimpleTaskSignal() |
| 75 | + let operation = SimpleTaskOperationProbe() |
| 76 | + var task: Task<Void, Never>? |
| 77 | + |
| 78 | + SimpleTaskSupport.schedule( |
| 79 | + task: &task, |
| 80 | + delay: 60, |
| 81 | + sleep: { nanoseconds in |
| 82 | + await firstSleepStarted.signal() |
| 83 | + try await Task.sleep(nanoseconds: nanoseconds) |
| 84 | + }, |
| 85 | + operation: { |
| 86 | + await operation.recordCall("first") |
| 87 | + }) |
| 88 | + |
| 89 | + await firstSleepStarted.wait() |
| 90 | + guard let firstTask = task else { |
| 91 | + Issue.record("first scheduled task did not start") |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + SimpleTaskSupport.schedule( |
| 96 | + task: &task, |
| 97 | + delay: 0, |
| 98 | + sleep: { _ in }, |
| 99 | + operation: { |
| 100 | + await operation.recordCall("second") |
| 101 | + }) |
| 102 | + guard let secondTask = task else { |
| 103 | + Issue.record("replacement task did not start") |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + await firstTask.value |
| 108 | + await secondTask.value |
| 109 | + SimpleTaskSupport.stop(task: &task) |
| 110 | + |
| 111 | + #expect(await operation.calls() == ["second"]) |
| 112 | + #expect(task == nil) |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + @MainActor |
| 117 | + func `cancelling after sleep completes does not run the scheduled operation`() async { |
| 118 | + let operation = SimpleTaskOperationProbe() |
| 119 | + var task: Task<Void, Never>? |
| 120 | + |
| 121 | + SimpleTaskSupport.schedule( |
| 122 | + task: &task, |
| 123 | + delay: 0, |
| 124 | + sleep: { _ in }, |
| 125 | + beforeOperationCheck: { |
| 126 | + withUnsafeCurrentTask { currentTask in |
| 127 | + currentTask?.cancel() |
| 128 | + } |
| 129 | + }, |
| 130 | + operation: { |
| 131 | + await operation.recordCall("stale") |
| 132 | + }) |
| 133 | + |
| 134 | + guard let runningTask = task else { |
| 135 | + Issue.record("scheduled task did not start") |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + await runningTask.value |
| 140 | + SimpleTaskSupport.stop(task: &task) |
| 141 | + |
| 142 | + #expect(await operation.calls().isEmpty) |
| 143 | + #expect(task == nil) |
| 144 | + } |
| 145 | +} |
0 commit comments