Skip to content

Commit f2f785d

Browse files
test(guarded-write): bind the FIFO chain to blocked publishes and a pending reset
Three review asks on src/core/tools/__tests__/guardedWrite.spec.ts were about assertions this file did not carry, so they are landed as executable rows rather than as code: - a third write is now queued behind a *blocked* second write on the same path, which the shipped eviction row (two rapid writes behind an idle tail) does not cover. - the independence row now holds the first path's publish open and requires the second path to settle while it is held, releasing the gate in finally. As shipped it ran both writes against a writer that resolves immediately, so a single global queue passed it. - a pending-link row: resetChain() runs while a publish is in flight and the later write must reach safeWriteText anyway, which is detachment rather than ordering. Answers review roots PRRT_kwDOSK-bwM61KUmV, 607 (…61KUml) and PRRT_kwDOSK-bwM61KUmq. These rows bind behavior the head already had, so their controls are negations of that behavior, run against src/core/tools/guardedWrite.ts as it stands at 6b7d45e with the probe reverted before commit: - PROBE N1 (enqueue ignores any pending tail): row "keeps a third write queued behind a blocked second write on the same path" exit 1 - "expected [ 'start:second', 'start:third', …(1) ] to deeply equal [ 'start:second' ]". - PROBE N2 (one queue for every path): row "writes on different paths are independent (no cross-path serialization)" exit 1 - "Test timed out in 20000ms." - PROBE N3 (resetChain does nothing): row "detaches a pending link so a later write runs while the blocked one is still held" exit 1 - "Test timed out in 20000ms." With the implementation as shipped the file is exit 0: Tests 44 passed (44).
1 parent 6b7d45e commit f2f785d

1 file changed

Lines changed: 109 additions & 1 deletion

File tree

‎src/core/tools/__tests__/guardedWrite.spec.ts‎

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,17 +469,84 @@ describe("guardedWrite (S4a, epic #1375)", () => {
469469
expect(mockedSafeWriteText).toHaveBeenCalledTimes(3)
470470
})
471471

472+
it("keeps a third write queued behind a blocked second write on the same path", async () => {
473+
const reg = new ObservationRegistry()
474+
reg.observe(abs("queue.txt"), "v1")
475+
mockedComputeVersionToken.mockResolvedValue("v1")
476+
const task = createMockTask({ observationRegistry: reg })
477+
478+
// Settle one write first so its chain entry is evicted with it: the three writes
479+
// below are submitted to a chain that was just emptied, which is the state the ask
480+
// is about - links enqueued while a publish is already in flight, not links queued
481+
// behind an idle tail.
482+
await guardedWrite(task, "queue.txt", "first", "update")
483+
484+
const order: string[] = []
485+
let releaseSecond: () => void = () => {}
486+
const secondHeld = new Promise<void>((resolve) => {
487+
releaseSecond = resolve
488+
})
489+
mockedSafeWriteText.mockImplementation(async (_path: string, content: string) => {
490+
order.push("start:" + content)
491+
if (content === "second") {
492+
// Hold only the second publication: the third write must not reach
493+
// safeWriteText until this one settles.
494+
await secondHeld
495+
}
496+
order.push("end:" + content)
497+
})
498+
499+
const p2 = guardedWrite(task, "queue.txt", "second", "update")
500+
const p3 = guardedWrite(task, "queue.txt", "third", "update")
501+
502+
await new Promise((resolve) => setTimeout(resolve, 0))
503+
expect(order).toEqual(["start:second"])
504+
505+
releaseSecond()
506+
await Promise.all([p2, p3])
507+
508+
expect(order).toEqual(["start:second", "end:second", "start:third", "end:third"])
509+
// Three publishes: the settled first write plus the two queued behind it.
510+
expect(mockedSafeWriteText).toHaveBeenCalledTimes(3)
511+
})
472512
it("writes on different paths are independent (no cross-path serialization)", async () => {
473513
const reg = new ObservationRegistry()
474514
reg.observe(abs("a.txt"), "v1")
475515
reg.observe(abs("b.txt"), "v1")
476516
mockedComputeVersionToken.mockResolvedValue("v1")
477517
const task = createMockTask({ observationRegistry: reg })
478518

519+
const order: string[] = []
520+
let releaseFirst: () => void = () => {}
521+
const firstHeld = new Promise<void>((resolve) => {
522+
releaseFirst = resolve
523+
})
524+
mockedSafeWriteText.mockImplementation(async (_path: string, content: string) => {
525+
order.push("start:" + content)
526+
if (content === "a") {
527+
// Hold only the first path publish open. The chains are keyed per absolute
528+
// path, so the second path must reach its publish anyway; a single global
529+
// queue would leave b.txt waiting here and the await below would time out.
530+
await firstHeld
531+
}
532+
order.push("end:" + content)
533+
})
534+
479535
const p1 = guardedWrite(task, "a.txt", "a", "update")
480536
const p2 = guardedWrite(task, "b.txt", "b", "update")
481-
await Promise.all([p1, p2])
482537

538+
try {
539+
// The second path settles while the first is still inside safeWriteText.
540+
await p2
541+
expect(order).toEqual(["start:a", "start:b", "end:b"])
542+
} finally {
543+
// Release the held gate on every exit path, including an assertion failure
544+
// above, so a failing row cannot leave the chain blocked for the next one.
545+
releaseFirst()
546+
await p1
547+
}
548+
549+
expect(order).toEqual(["start:a", "start:b", "end:b", "end:a"])
483550
expect(mockedSafeWriteText).toHaveBeenCalledTimes(2)
484551
})
485552
})
@@ -636,6 +703,47 @@ describe("guardedWrite (S4a, epic #1375)", () => {
636703
preCommitVerify: expect.any(Function),
637704
})
638705
})
706+
it("detaches a pending link so a later write runs while the blocked one is still held", async () => {
707+
const reg = new ObservationRegistry()
708+
reg.observe(abs("pending.txt"), "v1")
709+
mockedComputeVersionToken.mockResolvedValue("v1")
710+
const task = createMockTask({ observationRegistry: reg })
711+
712+
const order: string[] = []
713+
let releaseFirst: () => void = () => {}
714+
const firstHeld = new Promise<void>((resolve) => {
715+
releaseFirst = resolve
716+
})
717+
mockedSafeWriteText.mockImplementation(async (_path: string, content: string) => {
718+
order.push("start:" + content)
719+
if (content === "held") {
720+
await firstHeld
721+
}
722+
order.push("end:" + content)
723+
})
724+
725+
// A publish is in flight on this path's chain at the moment resetChain() runs.
726+
const p1 = guardedWrite(task, "pending.txt", "held", "update")
727+
await new Promise((resolve) => setTimeout(resolve, 0))
728+
expect(order).toEqual(["start:held"])
729+
730+
resetChain()
731+
732+
try {
733+
// Detachment is the assertion: the abandoned tail may not hold the new write,
734+
// so the fresh write must reach its publish and settle while the held link is
735+
// still inside safeWriteText. A resetChain() that did nothing would leave this
736+
// await behind the held link and time out.
737+
await guardedWrite(task, "pending.txt", "fresh", "update")
738+
expect(order).toEqual(["start:held", "start:fresh", "end:fresh"])
739+
} finally {
740+
releaseFirst()
741+
await p1
742+
}
743+
744+
expect(order).toEqual(["start:held", "start:fresh", "end:fresh", "end:held"])
745+
expect(mockedSafeWriteText).toHaveBeenCalledTimes(2)
746+
})
639747
})
640748

641749
describe("atomicity of the guard check and publish", () => {

0 commit comments

Comments
 (0)