|
1 | 1 | // Command queue tests cover bounded command execution and queue ordering. |
2 | 2 | import { AsyncLocalStorage } from "node:async_hooks"; |
| 3 | +import { spawnSync } from "node:child_process"; |
3 | 4 | import { MAX_TIMER_TIMEOUT_MS } from "@openclaw/normalization-core/number-coercion"; |
4 | 5 | import { importFreshModule } from "openclaw/plugin-sdk/test-fixtures"; |
5 | 6 | import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
@@ -226,6 +227,114 @@ describe("command queue", () => { |
226 | 227 | expect(calls).toEqual(["first", "second"]); |
227 | 228 | }); |
228 | 229 |
|
| 230 | + it("preserves priority and FIFO order across partial drains and resumed growth", async () => { |
| 231 | + const lane = "priority-fifo-resume"; |
| 232 | + const calls: string[] = []; |
| 233 | + const enqueue = (label: string, priority?: "foreground" | "background") => |
| 234 | + enqueueCommandInLane( |
| 235 | + lane, |
| 236 | + async () => { |
| 237 | + calls.push(label); |
| 238 | + }, |
| 239 | + { priority }, |
| 240 | + ); |
| 241 | + |
| 242 | + setCommandLaneConcurrency(lane, 0); |
| 243 | + const normal = Array.from({ length: 20 }, (_, index) => enqueue(`normal-${index}`)); |
| 244 | + |
| 245 | + setCommandLaneConcurrency(lane, 5); |
| 246 | + setCommandLaneConcurrency(lane, 0); |
| 247 | + await Promise.all(normal.slice(0, 5)); |
| 248 | + |
| 249 | + const resumedNormal = Array.from({ length: 20 }, (_, index) => enqueue(`normal-${index + 20}`)); |
| 250 | + const background = Array.from({ length: 18 }, (_, index) => |
| 251 | + enqueue(`background-${index}`, "background"), |
| 252 | + ); |
| 253 | + const foreground = Array.from({ length: 18 }, (_, index) => |
| 254 | + enqueue(`foreground-${index}`, "foreground"), |
| 255 | + ); |
| 256 | + setCommandLaneConcurrency(lane, 1); |
| 257 | + await Promise.all([...normal, ...resumedNormal, ...background, ...foreground]); |
| 258 | + |
| 259 | + expect(calls).toEqual([ |
| 260 | + ...Array.from({ length: 5 }, (_, index) => `normal-${index}`), |
| 261 | + ...Array.from({ length: 18 }, (_, index) => `foreground-${index}`), |
| 262 | + ...Array.from({ length: 35 }, (_, index) => `normal-${index + 5}`), |
| 263 | + ...Array.from({ length: 18 }, (_, index) => `background-${index}`), |
| 264 | + ]); |
| 265 | + }); |
| 266 | + |
| 267 | + it("avoids quadratic array work as a paused queue doubles", () => { |
| 268 | + const script = String.raw` |
| 269 | + const { enqueueCommandInLane, setCommandLaneConcurrency } = await import( |
| 270 | + "./src/process/command-queue.ts" |
| 271 | + ); |
| 272 | + const originalFindIndex = Array.prototype.findIndex; |
| 273 | + const originalShift = Array.prototype.shift; |
| 274 | + let enqueueComparisons = 0; |
| 275 | + let shiftedSlots = 0; |
| 276 | +
|
| 277 | + Array.prototype.findIndex = function (predicate, thisArg) { |
| 278 | + return originalFindIndex.call(this, (value, index, array) => { |
| 279 | + enqueueComparisons += 1; |
| 280 | + return predicate.call(thisArg, value, index, array); |
| 281 | + }); |
| 282 | + }; |
| 283 | + Array.prototype.shift = function () { |
| 284 | + shiftedSlots += this.length; |
| 285 | + return originalShift.call(this); |
| 286 | + }; |
| 287 | +
|
| 288 | + const measureQueueWork = async (count) => { |
| 289 | + const lane = "linear-queue-" + count; |
| 290 | + setCommandLaneConcurrency(lane, 0); |
| 291 | + const comparisonStart = enqueueComparisons; |
| 292 | + const tasks = Array.from({ length: count }, (_, index) => |
| 293 | + enqueueCommandInLane(lane, async () => index), |
| 294 | + ); |
| 295 | + const enqueueWork = enqueueComparisons - comparisonStart; |
| 296 | + const shiftedSlotStart = shiftedSlots; |
| 297 | + setCommandLaneConcurrency(lane, count); |
| 298 | + const dequeueWork = shiftedSlots - shiftedSlotStart; |
| 299 | + await Promise.all(tasks); |
| 300 | + return { enqueueWork, dequeueWork }; |
| 301 | + }; |
| 302 | +
|
| 303 | + const smaller = await measureQueueWork(256); |
| 304 | + const larger = await measureQueueWork(512); |
| 305 | + process.stdout.write(JSON.stringify({ smaller, larger })); |
| 306 | + `; |
| 307 | + const result = spawnSync( |
| 308 | + process.execPath, |
| 309 | + ["--import", "tsx", "--input-type=module", "--eval", script], |
| 310 | + { |
| 311 | + cwd: process.cwd(), |
| 312 | + encoding: "utf8", |
| 313 | + env: { |
| 314 | + ...process.env, |
| 315 | + NODE_OPTIONS: undefined, |
| 316 | + VITEST: undefined, |
| 317 | + VITEST_POOL_ID: undefined, |
| 318 | + VITEST_WORKER_ID: undefined, |
| 319 | + }, |
| 320 | + timeout: 60_000, |
| 321 | + }, |
| 322 | + ); |
| 323 | + |
| 324 | + expect(result.error).toBeUndefined(); |
| 325 | + expect(result.status, result.stderr).toBe(0); |
| 326 | + const measurements = JSON.parse(result.stdout) as { |
| 327 | + smaller: { enqueueWork: number; dequeueWork: number }; |
| 328 | + larger: { enqueueWork: number; dequeueWork: number }; |
| 329 | + }; |
| 330 | + expect(measurements.larger.enqueueWork).toBeLessThanOrEqual( |
| 331 | + measurements.smaller.enqueueWork * 3 + 512, |
| 332 | + ); |
| 333 | + expect(measurements.larger.dequeueWork).toBeLessThanOrEqual( |
| 334 | + measurements.smaller.dequeueWork * 3 + 512, |
| 335 | + ); |
| 336 | + }); |
| 337 | + |
229 | 338 | it("reports queueAhead after priority insertion", async () => { |
230 | 339 | vi.useFakeTimers(); |
231 | 340 | try { |
@@ -727,18 +836,25 @@ describe("command queue", () => { |
727 | 836 | await expect(second).resolves.toBe("second"); |
728 | 837 | }); |
729 | 838 |
|
730 | | - it("clearCommandLane rejects pending promises", async () => { |
| 839 | + it("clearCommandLane rejects pending promises at every priority", async () => { |
731 | 840 | // First task blocks the lane. |
732 | 841 | const { task: first, release } = enqueueBlockedMainTask(async () => "first"); |
733 | 842 |
|
734 | | - // Second task is queued behind the first. |
735 | | - const second = enqueueCommandInLane(CommandLane.Main, async () => "second"); |
| 843 | + const background = enqueueCommandInLane(CommandLane.Main, async () => "background", { |
| 844 | + priority: "background", |
| 845 | + }); |
| 846 | + const normal = enqueueCommandInLane(CommandLane.Main, async () => "normal"); |
| 847 | + const foreground = enqueueCommandInLane(CommandLane.Main, async () => "foreground", { |
| 848 | + priority: "foreground", |
| 849 | + }); |
| 850 | + const rejectionChecks = [background, normal, foreground].map((task) => |
| 851 | + expect(task).rejects.toBeInstanceOf(CommandLaneClearedError), |
| 852 | + ); |
736 | 853 |
|
737 | 854 | const removed = clearCommandLane(); |
738 | | - expect(removed).toBe(1); // only the queued (not active) entry |
| 855 | + expect(removed).toBe(3); // only the queued (not active) entries |
739 | 856 |
|
740 | | - // The queued promise should reject. |
741 | | - await expect(second).rejects.toBeInstanceOf(CommandLaneClearedError); |
| 857 | + await Promise.all(rejectionChecks); |
742 | 858 |
|
743 | 859 | // Let the active task finish normally. |
744 | 860 | release(); |
|
0 commit comments