Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/dms/e2e/mongo-server.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import test from "node:test";
import { fileURLToPath } from "node:url";
import { MongoClient } from "mongodb";
import { pidIn } from "./helpers/pids.mjs";
import { isAlive } from "./identity.mjs";
import { isRunning } from "./identity.mjs";
import { mongodBinary, startMongod } from "./mongo.mjs";
import { stopRecorded } from "./processes.mjs";

Expand Down Expand Up @@ -107,7 +107,7 @@ await test("a mongod that cannot join the replica set is stopped", async (t) =>
await assert.rejects(startMongod(paths, [PACKAGE_ROOT]), {
message: "This node was not started with replication enabled.",
});
assert.equal(isAlive(pidIn(spawned)), false);
assert.equal(isRunning(pidIn(spawned)), false);
});

await test("a mongod restarted on its port and data folder serves a change stream again", async (t) => {
Expand Down
42 changes: 40 additions & 2 deletions packages/dms/e2e/run-state.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
rmSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { platform, tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { describeFailure } from "./failure.mjs";
import { assertSameNamespaces } from "./identity.mjs";
Expand All @@ -26,6 +26,9 @@ import {
} from "./json.mjs";

const RUN_FILE = "run.json";
const REPLACE_RETRY_MS = 2_000;
const REPLACE_POLL_MS = 5;
const REPLACE_PAUSE = new Int32Array(new SharedArrayBuffer(4));
const ID_BYTES = 3;
const TIMESTAMP_LENGTH = 15;

Expand Down Expand Up @@ -275,7 +278,42 @@ export function saveRun(state) {
writeFileSync(`${file}.next`, `${JSON.stringify(state, null, 2)}\n`, {
mode: 0o600,
});
renameSync(`${file}.next`, file);
replaceFile(`${file}.next`, file);
}

/**
* Renames `from` over `to`. Windows refuses with EPERM to replace a file
* another process holds open, as a service loading the run does for a
* moment, so there the rename is tried again until that read is done.
*
* @param {string} from The new file
* @param {string} to The file it replaces
* @throws When the rename fails for another reason, or Windows still refuses it after {@link REPLACE_RETRY_MS}
*/
function replaceFile(from, to) {
const deadline = Date.now() + REPLACE_RETRY_MS;
for (;;) {
try {
renameSync(from, to);
return;
} catch (error) {
if (!isHeldOpen(error) || Date.now() >= deadline) throw error;
Atomics.wait(REPLACE_PAUSE, 0, 0, REPLACE_POLL_MS);
}
}
}

/**
* @param {unknown} error
* @returns {boolean}
*/
function isHeldOpen(error) {
return (
platform() === "win32" &&
error instanceof Error &&
"code" in error &&
error.code === "EPERM"
);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/dms/e2e/services.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { join } from "node:path";
import test from "node:test";
import { pathToFileURL } from "node:url";
import { newRun } from "./helpers/fake-run.mjs";
import { hasProc } from "./identity.mjs";
import { listenersOf, restartablePort } from "./processes.mjs";
import { readRun, saveRun } from "./run-state.mjs";

Expand Down Expand Up @@ -44,7 +45,8 @@ await ensureMail(loadRun(${JSON.stringify(state.dir)}));`,
const mail = readRun(state.dir).processes.find(
(entry) => entry.name === "mail",
);
assert.deepEqual(listenersOf(state.smtpPort), [mail?.pid]);
assert.ok(mail, "the run records no mail sink");
if (hasProc()) assert.deepEqual(listenersOf(state.smtpPort), [mail.pid]);
});

await test("the service entry refuses a name it does not serve and names those it does (catches an entry that exits 0 on a typo, leaving the stack to wait on a port nothing listens on)", () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/dms/e2e/suite.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const CONFIG = `export default ${JSON.stringify({
* @returns {string} The checkout root
*/
function twoSuiteCheckout(t) {
const root = realpathSync(mkdtempSync(join(tmpdir(), "dms-e2e-suite-")));
const root = realpathSync.native(
mkdtempSync(join(tmpdir(), "dms-e2e-suite-")),
);
const cwd = process.cwd();
const recorded = process.env.DMS_E2E_SUITE;
const prebuilt = process.env.DMS_E2E_PREBUILT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ describe("[unit] a declaration file edited between two builder operations", () =
!result.ok && result.error.code === "typecheck_failed"
? result.error.diagnostics
: [];
expect(diagnostics.map((diagnostic) => diagnostic.file)).to.deep.equal([
path.join(app.root, "src", "labels.ts"),
]);
expect(
diagnostics.map((diagnostic) => path.normalize(diagnostic.file)),
).to.deep.equal([path.join(app.root, "src", "labels.ts")]);
});

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ async function addBanner() {

function failedFiles(result: Awaited<ReturnType<typeof addBanner>>): string[] {
return !result.ok && result.error.code === "typecheck_failed"
? result.error.diagnostics.map((diagnostic) => diagnostic.file)
? result.error.diagnostics.map((diagnostic) =>
path.normalize(diagnostic.file),
)
: [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ function installTone(tone: string): void {

function failedFiles(result: OpResult<unknown>): string[] {
return !result.ok && result.error.code === "typecheck_failed"
? result.error.diagnostics.map((diagnostic) => diagnostic.file)
? result.error.diagnostics.map((diagnostic) =>
path.normalize(diagnostic.file),
)
: [];
}

Expand Down
Loading