From 667812a297a538863695c20bf5d8228301298db5 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 13 Jul 2022 11:16:42 -0400 Subject: fix(cli): synchronize async stdio/file reads and writes (#15092) Fixes a regression where async writes and reads could get out of order. --- cli/tests/unit/files_test.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'cli/tests/unit') diff --git a/cli/tests/unit/files_test.ts b/cli/tests/unit/files_test.ts index d15f1f538..5fb590d72 100644 --- a/cli/tests/unit/files_test.ts +++ b/cli/tests/unit/files_test.ts @@ -263,6 +263,38 @@ Deno.test( }, ); +Deno.test( + { permissions: { write: true } }, + async function writeSyncWhileAsyncFails() { + const tempDir = await Deno.makeTempDir(); + try { + const filePath = tempDir + "/file.txt"; + const file = await Deno.open(filePath, { create: true, write: true }); + const rid = file.rid; + try { + // set a file lock so the async write will be held up + await Deno.flock(rid, true); + let p: Promise | undefined; + try { + p = Deno.write(rid, new TextEncoder().encode("test")); + assertThrows( + () => Deno.writeSync(rid, new TextEncoder().encode("test")), + Error, + "Resource is unavailable because it is in use by a promise", + ); + } finally { + await Deno.funlock(rid); + } + await p; + } finally { + file.close(); + } + } finally { + Deno.removeSync(tempDir, { recursive: true }); + } + }, +); + Deno.test(async function openOptions() { const filename = "cli/tests/testdata/fixture.json"; await assertRejects( -- cgit v1.2.3