|
1 | 1 | import { describe, expect, test } from 'bun:test'; |
2 | | -import { CooperativePump, type Ticker } from '../../../src/main/run-loop'; |
| 2 | +import { |
| 3 | + AdaptiveBlockingPump, |
| 4 | + CooperativePump, |
| 5 | + type Ticker, |
| 6 | + type TickScheduler, |
| 7 | +} from '../../../src/main/run-loop'; |
3 | 8 |
|
4 | 9 | const manualTicker = (): { ticker: Ticker; tick: () => void; cancelled: () => boolean } => { |
5 | 10 | let onTick: (() => void) | undefined; |
@@ -126,3 +131,126 @@ describe('CooperativePump interval', () => { |
126 | 131 | expect(seenMs).toBeLessThanOrEqual(32); |
127 | 132 | }); |
128 | 133 | }); |
| 134 | + |
| 135 | +const manualScheduler = (): { |
| 136 | + schedule: TickScheduler; |
| 137 | + run: () => void; |
| 138 | + pending: () => boolean; |
| 139 | +} => { |
| 140 | + let next: (() => void) | undefined; |
| 141 | + return { |
| 142 | + schedule: (tick) => { |
| 143 | + next = tick; |
| 144 | + }, |
| 145 | + run: () => { |
| 146 | + const tick = next; |
| 147 | + next = undefined; |
| 148 | + tick?.(); |
| 149 | + }, |
| 150 | + pending: () => next !== undefined, |
| 151 | + }; |
| 152 | +}; |
| 153 | + |
| 154 | +describe('AdaptiveBlockingPump start / stop', () => { |
| 155 | + test('is not running before start, running after', () => { |
| 156 | + const pump = new AdaptiveBlockingPump(() => false, { schedule: manualScheduler().schedule }); |
| 157 | + expect(pump.isRunning).toBe(false); |
| 158 | + pump.start(); |
| 159 | + expect(pump.isRunning).toBe(true); |
| 160 | + }); |
| 161 | + |
| 162 | + test('start is idempotent — a second start does not drain twice', () => { |
| 163 | + let drains = 0; |
| 164 | + const pump = new AdaptiveBlockingPump( |
| 165 | + () => { |
| 166 | + drains += 1; |
| 167 | + return false; |
| 168 | + }, |
| 169 | + { schedule: manualScheduler().schedule }, |
| 170 | + ); |
| 171 | + pump.start(); |
| 172 | + pump.start(); |
| 173 | + expect(drains).toBe(1); |
| 174 | + }); |
| 175 | + |
| 176 | + test('no draining occurs after stop', () => { |
| 177 | + const s = manualScheduler(); |
| 178 | + let drains = 0; |
| 179 | + const pump = new AdaptiveBlockingPump( |
| 180 | + () => { |
| 181 | + drains += 1; |
| 182 | + return false; |
| 183 | + }, |
| 184 | + { schedule: s.schedule }, |
| 185 | + ); |
| 186 | + pump.start(); |
| 187 | + pump.stop(); |
| 188 | + s.run(); |
| 189 | + expect(drains).toBe(1); |
| 190 | + expect(pump.isRunning).toBe(false); |
| 191 | + }); |
| 192 | +}); |
| 193 | + |
| 194 | +describe('AdaptiveBlockingPump adaptive timeout', () => { |
| 195 | + test('starts at the minimum and drives the drain with the current timeout', () => { |
| 196 | + const s = manualScheduler(); |
| 197 | + const seen: number[] = []; |
| 198 | + const pump = new AdaptiveBlockingPump( |
| 199 | + (ms) => { |
| 200 | + seen.push(ms); |
| 201 | + return false; |
| 202 | + }, |
| 203 | + { minTimeoutMs: 10, maxTimeoutMs: 40, schedule: s.schedule }, |
| 204 | + ); |
| 205 | + pump.start(); |
| 206 | + s.run(); |
| 207 | + s.run(); |
| 208 | + expect(seen).toEqual([10, 20, 40]); |
| 209 | + }); |
| 210 | + |
| 211 | + test('backs off exponentially toward the max while idle, then caps', () => { |
| 212 | + const s = manualScheduler(); |
| 213 | + const pump = new AdaptiveBlockingPump(() => false, { |
| 214 | + minTimeoutMs: 8, |
| 215 | + maxTimeoutMs: 64, |
| 216 | + schedule: s.schedule, |
| 217 | + }); |
| 218 | + pump.start(); |
| 219 | + expect(pump.timeoutMs).toBe(16); |
| 220 | + s.run(); |
| 221 | + expect(pump.timeoutMs).toBe(32); |
| 222 | + s.run(); |
| 223 | + expect(pump.timeoutMs).toBe(64); |
| 224 | + s.run(); |
| 225 | + expect(pump.timeoutMs).toBe(64); |
| 226 | + }); |
| 227 | + |
| 228 | + test('snaps back to the minimum when a tick handles events', () => { |
| 229 | + const s = manualScheduler(); |
| 230 | + let active = false; |
| 231 | + const pump = new AdaptiveBlockingPump(() => active, { |
| 232 | + minTimeoutMs: 8, |
| 233 | + maxTimeoutMs: 64, |
| 234 | + schedule: s.schedule, |
| 235 | + }); |
| 236 | + pump.start(); |
| 237 | + s.run(); |
| 238 | + expect(pump.timeoutMs).toBeGreaterThan(8); |
| 239 | + active = true; |
| 240 | + s.run(); |
| 241 | + expect(pump.timeoutMs).toBe(8); |
| 242 | + }); |
| 243 | + |
| 244 | + test('a throwing drain does not stop the pump and still reschedules', () => { |
| 245 | + const s = manualScheduler(); |
| 246 | + const pump = new AdaptiveBlockingPump( |
| 247 | + () => { |
| 248 | + throw new Error('native hiccup'); |
| 249 | + }, |
| 250 | + { schedule: s.schedule }, |
| 251 | + ); |
| 252 | + pump.start(); |
| 253 | + expect(pump.isRunning).toBe(true); |
| 254 | + expect(s.pending()).toBe(true); |
| 255 | + }); |
| 256 | +}); |
0 commit comments