2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-02-07 11:51:28 -05:00
|
|
|
import { assertEquals, fail } from "@test_util/std/assert/mod.ts";
|
2023-03-19 12:10:39 -04:00
|
|
|
import { fdatasync, fdatasyncSync } from "node:fs";
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name:
|
|
|
|
"ASYNC: flush any pending data operations of the given file stream to disk",
|
|
|
|
async fn() {
|
2024-01-24 09:59:55 -05:00
|
|
|
const filePath = await Deno.makeTempFile();
|
|
|
|
using file = await Deno.open(filePath, {
|
2023-03-19 12:10:39 -04:00
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
create: true,
|
|
|
|
});
|
|
|
|
const data = new Uint8Array(64);
|
2024-01-24 10:36:13 -05:00
|
|
|
await file.write(data);
|
2023-03-19 12:10:39 -04:00
|
|
|
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
2024-01-24 09:59:55 -05:00
|
|
|
fdatasync(file.rid, (err: Error | null) => {
|
2023-03-19 12:10:39 -04:00
|
|
|
if (err !== null) reject();
|
|
|
|
else resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(
|
|
|
|
async () => {
|
2024-01-24 09:59:55 -05:00
|
|
|
assertEquals(await Deno.readFile(filePath), data);
|
2023-03-19 12:10:39 -04:00
|
|
|
},
|
|
|
|
() => {
|
|
|
|
fail("No error expected");
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.finally(async () => {
|
2024-01-24 09:59:55 -05:00
|
|
|
await Deno.remove(filePath);
|
2023-03-19 12:10:39 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name:
|
|
|
|
"SYNC: flush any pending data operations of the given file stream to disk.",
|
|
|
|
fn() {
|
2024-01-24 09:59:55 -05:00
|
|
|
const filePath = Deno.makeTempFileSync();
|
|
|
|
using file = Deno.openSync(filePath, {
|
2023-03-19 12:10:39 -04:00
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
create: true,
|
|
|
|
});
|
|
|
|
const data = new Uint8Array(64);
|
2024-01-24 09:59:55 -05:00
|
|
|
Deno.writeSync(file.rid, data);
|
2023-03-19 12:10:39 -04:00
|
|
|
|
|
|
|
try {
|
2024-01-24 09:59:55 -05:00
|
|
|
fdatasyncSync(file.rid);
|
|
|
|
assertEquals(Deno.readFileSync(filePath), data);
|
2023-03-19 12:10:39 -04:00
|
|
|
} finally {
|
2024-01-24 09:59:55 -05:00
|
|
|
Deno.removeSync(filePath);
|
2023-03-19 12:10:39 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|