2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-07-25 01:30:28 -04:00
|
|
|
import { assertEquals, fail } from "@std/assert";
|
2023-03-19 12:10:39 -04:00
|
|
|
import { fsync, fsyncSync } from "node:fs";
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "ASYNC: flush any pending data of the given file stream to disk",
|
|
|
|
async fn() {
|
2024-01-24 09:12:22 -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 size = 64;
|
2024-01-24 09:12:22 -05:00
|
|
|
await file.truncate(size);
|
2023-03-19 12:10:39 -04:00
|
|
|
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
2024-01-24 09:12:22 -05:00
|
|
|
fsync(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:12:22 -05:00
|
|
|
assertEquals((await Deno.stat(filePath)).size, size);
|
2023-03-19 12:10:39 -04:00
|
|
|
},
|
|
|
|
() => {
|
|
|
|
fail("No error expected");
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.finally(async () => {
|
2024-01-24 09:12:22 -05:00
|
|
|
await Deno.remove(filePath);
|
2023-03-19 12:10:39 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "SYNC: flush any pending data the given file stream to disk",
|
|
|
|
fn() {
|
2024-01-24 09:12:22 -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 size = 64;
|
2024-01-24 09:12:22 -05:00
|
|
|
file.truncateSync(size);
|
2023-03-19 12:10:39 -04:00
|
|
|
|
|
|
|
try {
|
2024-01-24 09:12:22 -05:00
|
|
|
fsyncSync(file.rid);
|
|
|
|
assertEquals(Deno.statSync(filePath).size, size);
|
2023-03-19 12:10:39 -04:00
|
|
|
} finally {
|
2024-01-24 09:12:22 -05:00
|
|
|
Deno.removeSync(filePath);
|
2023-03-19 12:10:39 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|