Skip to content

Commit 2a1b0a8

Browse files
test(core): port 1918's held-window rows to guard the publish chain
Answers review threads 4508986872 (a write on another path publishes while the first path's publish is blocked) and 4508986906 (resetChain detaches a link that is still publishing so the next same-path write starts), and carries 1918's same-path row too, for family parity, since no open root on this branch asks for it. The rows are verbatim from 1918 guardedWrite.spec.ts:943, :1043 and :1246; the resetChain row's closing assertion carries this branch's publish shape, safeWriteText(target, content, { backup: true }) at guardedWrite.ts:383 and :480, because 1918 publishes without the options argument. Focused suite: 82 passed; each row fails alone under a targeted mutation of guardedWrite.ts. Test-only commit: no changeset, no changelog.
1 parent da2b913 commit 2a1b0a8

1 file changed

Lines changed: 128 additions & 0 deletions

File tree

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

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,49 @@ describe("guardedWrite (S4a, epic #1375)", () => {
741741
})
742742
})
743743

744+
// Ported from 1918 guardedWrite.spec.ts:943 (row 4507855167). No open root on this branch asks
745+
// for this row; the lead asked for it across the family and this branch carries the same chain.
746+
it("the second publication does not start while the first is still running", async () => {
747+
// Row 4507855167. Call order and non-overlap are different claims: NthCalledWith says which
748+
// publish ran first, and a second publish that began while the first was still inside the publish
749+
// primitive satisfies it. The first publish is held open on a promise this row controls, the
750+
// invocation list is read while that hold is open, and only then is the hold released.
751+
const reg = new ObservationRegistry()
752+
reg.observe(abs("overlap.txt"), "v1")
753+
mockedComputeVersionToken.mockResolvedValue("v1")
754+
const task = createMockTask({ observationRegistry: reg })
755+
756+
const started: string[] = []
757+
let releaseFirst: () => void = () => {}
758+
const firstHold = new Promise<void>((resolve) => {
759+
releaseFirst = resolve
760+
})
761+
mockedSafeWriteText
762+
.mockImplementationOnce(async () => {
763+
started.push("first")
764+
await firstHold
765+
})
766+
.mockImplementationOnce(async () => {
767+
started.push("second")
768+
})
769+
770+
const p1 = guardedWrite(task, "overlap.txt", "first", "update")
771+
const p2 = guardedWrite(task, "overlap.txt", "second", "update")
772+
773+
// One macrotask tick is enough for every microtask-only step ahead of a publish to run for both
774+
// writes, so a second link that was not waiting on the first would already have published here.
775+
await new Promise((resolve) => {
776+
setTimeout(resolve, 0)
777+
})
778+
expect(started).toEqual(["first"])
779+
780+
releaseFirst()
781+
const [r1, r2] = await Promise.allSettled([p1, p2])
782+
expect(r1.status).toBe("fulfilled")
783+
expect(r2.status).toBe("fulfilled")
784+
expect(started).toEqual(["first", "second"])
785+
})
786+
744787
it("the chain settles after a rejection - a later matching write still runs", async () => {
745788
const reg = new ObservationRegistry()
746789
reg.observe(abs("settle.txt"), "v1")
@@ -801,6 +844,44 @@ describe("guardedWrite (S4a, epic #1375)", () => {
801844

802845
expect(mockedSafeWriteText).toHaveBeenCalledTimes(2)
803846
})
847+
// Ported from 1918 guardedWrite.spec.ts:1043. This row answers root 4508986872 on this branch.
848+
it("lets a write on another path publish while the first path's publish is blocked", async () => {
849+
// The row is about progress during the blocked window, which a settled call
850+
// count cannot show: with one chain shared by every path the second write would
851+
// queue behind the first and this assertion would never see a publish at all.
852+
const reg = new ObservationRegistry()
853+
reg.observe(abs("gate-a.txt"), "v1")
854+
reg.observe(abs("gate-b.txt"), "v1")
855+
mockedComputeVersionToken.mockResolvedValue("v1")
856+
const task = createMockTask({ observationRegistry: reg })
857+
858+
let releaseGateA: () => void = () => {}
859+
const gateA = new Promise<void>((resolve) => {
860+
releaseGateA = resolve
861+
})
862+
const publishOrder: string[] = []
863+
mockedSafeWriteText.mockImplementation(async (target: string) => {
864+
if (target === abs("gate-a.txt")) {
865+
await gateA
866+
}
867+
publishOrder.push(target)
868+
})
869+
870+
const writeA = guardedWrite(task, "gate-a.txt", "a", "update")
871+
const writeB = guardedWrite(task, "gate-b.txt", "b", "update")
872+
873+
// Settle until b has published; a is still inside its own publish here.
874+
for (let tick = 0; tick < 200 && publishOrder.length === 0; tick++) {
875+
await new Promise((resolve) => setTimeout(resolve, 5))
876+
}
877+
expect(publishOrder).toEqual([abs("gate-b.txt")])
878+
879+
releaseGateA()
880+
await Promise.all([writeA, writeB])
881+
882+
expect(publishOrder).toEqual([abs("gate-b.txt"), abs("gate-a.txt")])
883+
expect(mockedSafeWriteText).toHaveBeenCalledTimes(2)
884+
})
804885
})
805886

806887
describe("path resolution", () => {
@@ -967,6 +1048,53 @@ describe("guardedWrite (S4a, epic #1375)", () => {
9671048

9681049
expect(mockedSafeWriteText).toHaveBeenLastCalledWith(abs("x.txt"), "b", { backup: true })
9691050
})
1051+
// Ported from 1918 guardedWrite.spec.ts:1246. This row answers root 4508986906 on this branch.
1052+
// The last assertion carries this branch's publish shape: guardedWrite.ts:383 and :480 call
1053+
// safeWriteText(target, content, { backup: true }), where 1918 calls it without options
1054+
// (guardedWrite.ts:207 and :290). The in-flight assertions above it are verbatim from 1918.
1055+
it("detaches a link that is still publishing so the next write starts immediately", async () => {
1056+
// The settled result of the row above cannot tell a detached chain from an
1057+
// emptied one, because nothing was in flight there. Here the first publish is
1058+
// still awaiting its gate when resetChain runs: if resetChain left the pending
1059+
// link as the path's tail, the second write would queue behind a publish that
1060+
// cannot finish until the second one is already waited on - the wedge the hook
1061+
// exists to undo.
1062+
const reg = new ObservationRegistry()
1063+
reg.observe(abs("chain.txt"), "v1")
1064+
mockedComputeVersionToken.mockResolvedValue("v1")
1065+
const task = createMockTask({ observationRegistry: reg })
1066+
1067+
let releaseGate: () => void = () => {}
1068+
const gate = new Promise<void>((resolve) => {
1069+
releaseGate = resolve
1070+
})
1071+
const entered: string[] = []
1072+
mockedSafeWriteText.mockImplementation(async (_target: string, content: string | Uint8Array) => {
1073+
entered.push(String(content))
1074+
if (content === "a") {
1075+
await gate
1076+
}
1077+
})
1078+
1079+
const firstWrite = guardedWrite(task, "chain.txt", "a", "update")
1080+
for (let tick = 0; tick < 200 && entered.length === 0; tick++) {
1081+
await new Promise((resolve) => setTimeout(resolve, 5))
1082+
}
1083+
// The first publish is in flight and unfinished.
1084+
expect(entered).toEqual(["a"])
1085+
1086+
resetChain()
1087+
const secondWrite = guardedWrite(task, "chain.txt", "b", "update")
1088+
for (let tick = 0; tick < 200 && entered.length < 2; tick++) {
1089+
await new Promise((resolve) => setTimeout(resolve, 5))
1090+
}
1091+
expect(entered).toEqual(["a", "b"])
1092+
1093+
releaseGate()
1094+
await Promise.all([firstWrite, secondWrite])
1095+
expect(entered).toEqual(["a", "b"])
1096+
expect(mockedSafeWriteText).toHaveBeenLastCalledWith(abs("chain.txt"), "b", { backup: true })
1097+
})
9701098
})
9711099

9721100
it("does not run a queued write once the owning task is cancelled", async () => {

0 commit comments

Comments
 (0)