|
|
@@ -9,6 +9,19 @@ import { tmpdir } from "../fixture/fixture"
|
|
|
|
|
|
afterEach(() => Instance.disposeAll())
|
|
|
|
|
|
+async function touch(file: string, time: number) {
|
|
|
+ const date = new Date(time)
|
|
|
+ await fs.utimes(file, date, date)
|
|
|
+}
|
|
|
+
|
|
|
+function gate() {
|
|
|
+ let open!: () => void
|
|
|
+ const wait = new Promise<void>((resolve) => {
|
|
|
+ open = resolve
|
|
|
+ })
|
|
|
+ return { open, wait }
|
|
|
+}
|
|
|
+
|
|
|
describe("file/time", () => {
|
|
|
const sessionID = SessionID.make("ses_00000000000000000000000001")
|
|
|
|
|
|
@@ -25,7 +38,6 @@ describe("file/time", () => {
|
|
|
expect(before).toBeUndefined()
|
|
|
|
|
|
await FileTime.read(sessionID, filepath)
|
|
|
- await Bun.sleep(10)
|
|
|
|
|
|
const after = await FileTime.get(sessionID, filepath)
|
|
|
expect(after).toBeInstanceOf(Date)
|
|
|
@@ -44,7 +56,6 @@ describe("file/time", () => {
|
|
|
fn: async () => {
|
|
|
await FileTime.read(SessionID.make("ses_00000000000000000000000002"), filepath)
|
|
|
await FileTime.read(SessionID.make("ses_00000000000000000000000003"), filepath)
|
|
|
- await Bun.sleep(10)
|
|
|
|
|
|
const time1 = await FileTime.get(SessionID.make("ses_00000000000000000000000002"), filepath)
|
|
|
const time2 = await FileTime.get(SessionID.make("ses_00000000000000000000000003"), filepath)
|
|
|
@@ -63,14 +74,10 @@ describe("file/time", () => {
|
|
|
await Instance.provide({
|
|
|
directory: tmp.path,
|
|
|
fn: async () => {
|
|
|
- FileTime.read(sessionID, filepath)
|
|
|
- await Bun.sleep(10)
|
|
|
+ await FileTime.read(sessionID, filepath)
|
|
|
const first = await FileTime.get(sessionID, filepath)
|
|
|
|
|
|
- await Bun.sleep(10)
|
|
|
-
|
|
|
- FileTime.read(sessionID, filepath)
|
|
|
- await Bun.sleep(10)
|
|
|
+ await FileTime.read(sessionID, filepath)
|
|
|
const second = await FileTime.get(sessionID, filepath)
|
|
|
|
|
|
expect(second!.getTime()).toBeGreaterThanOrEqual(first!.getTime())
|
|
|
@@ -84,12 +91,12 @@ describe("file/time", () => {
|
|
|
await using tmp = await tmpdir()
|
|
|
const filepath = path.join(tmp.path, "file.txt")
|
|
|
await fs.writeFile(filepath, "content", "utf-8")
|
|
|
+ await touch(filepath, 1_000)
|
|
|
|
|
|
await Instance.provide({
|
|
|
directory: tmp.path,
|
|
|
fn: async () => {
|
|
|
- FileTime.read(sessionID, filepath)
|
|
|
- await Bun.sleep(10)
|
|
|
+ await FileTime.read(sessionID, filepath)
|
|
|
await FileTime.assert(sessionID, filepath)
|
|
|
},
|
|
|
})
|
|
|
@@ -112,13 +119,14 @@ describe("file/time", () => {
|
|
|
await using tmp = await tmpdir()
|
|
|
const filepath = path.join(tmp.path, "file.txt")
|
|
|
await fs.writeFile(filepath, "content", "utf-8")
|
|
|
+ await touch(filepath, 1_000)
|
|
|
|
|
|
await Instance.provide({
|
|
|
directory: tmp.path,
|
|
|
fn: async () => {
|
|
|
- FileTime.read(sessionID, filepath)
|
|
|
- await Bun.sleep(100)
|
|
|
+ await FileTime.read(sessionID, filepath)
|
|
|
await fs.writeFile(filepath, "modified content", "utf-8")
|
|
|
+ await touch(filepath, 2_000)
|
|
|
await expect(FileTime.assert(sessionID, filepath)).rejects.toThrow("modified since it was last read")
|
|
|
},
|
|
|
})
|
|
|
@@ -128,13 +136,14 @@ describe("file/time", () => {
|
|
|
await using tmp = await tmpdir()
|
|
|
const filepath = path.join(tmp.path, "file.txt")
|
|
|
await fs.writeFile(filepath, "content", "utf-8")
|
|
|
+ await touch(filepath, 1_000)
|
|
|
|
|
|
await Instance.provide({
|
|
|
directory: tmp.path,
|
|
|
fn: async () => {
|
|
|
await FileTime.read(sessionID, filepath)
|
|
|
- await Bun.sleep(100)
|
|
|
await fs.writeFile(filepath, "modified", "utf-8")
|
|
|
+ await touch(filepath, 2_000)
|
|
|
|
|
|
let error: Error | undefined
|
|
|
try {
|
|
|
@@ -191,18 +200,25 @@ describe("file/time", () => {
|
|
|
directory: tmp.path,
|
|
|
fn: async () => {
|
|
|
const order: number[] = []
|
|
|
+ const hold = gate()
|
|
|
+ const ready = gate()
|
|
|
|
|
|
const op1 = FileTime.withLock(filepath, async () => {
|
|
|
order.push(1)
|
|
|
- await Bun.sleep(50)
|
|
|
+ ready.open()
|
|
|
+ await hold.wait
|
|
|
order.push(2)
|
|
|
})
|
|
|
|
|
|
+ await ready.wait
|
|
|
+
|
|
|
const op2 = FileTime.withLock(filepath, async () => {
|
|
|
order.push(3)
|
|
|
order.push(4)
|
|
|
})
|
|
|
|
|
|
+ hold.open()
|
|
|
+
|
|
|
await Promise.all([op1, op2])
|
|
|
expect(order).toEqual([1, 2, 3, 4])
|
|
|
},
|
|
|
@@ -219,15 +235,21 @@ describe("file/time", () => {
|
|
|
fn: async () => {
|
|
|
let started1 = false
|
|
|
let started2 = false
|
|
|
+ const hold = gate()
|
|
|
+ const ready = gate()
|
|
|
|
|
|
const op1 = FileTime.withLock(filepath1, async () => {
|
|
|
started1 = true
|
|
|
- await Bun.sleep(50)
|
|
|
+ ready.open()
|
|
|
+ await hold.wait
|
|
|
expect(started2).toBe(true)
|
|
|
})
|
|
|
|
|
|
+ await ready.wait
|
|
|
+
|
|
|
const op2 = FileTime.withLock(filepath2, async () => {
|
|
|
started2 = true
|
|
|
+ hold.open()
|
|
|
})
|
|
|
|
|
|
await Promise.all([op1, op2])
|
|
|
@@ -265,12 +287,12 @@ describe("file/time", () => {
|
|
|
await using tmp = await tmpdir()
|
|
|
const filepath = path.join(tmp.path, "file.txt")
|
|
|
await fs.writeFile(filepath, "content", "utf-8")
|
|
|
+ await touch(filepath, 1_000)
|
|
|
|
|
|
await Instance.provide({
|
|
|
directory: tmp.path,
|
|
|
fn: async () => {
|
|
|
- FileTime.read(sessionID, filepath)
|
|
|
- await Bun.sleep(10)
|
|
|
+ await FileTime.read(sessionID, filepath)
|
|
|
|
|
|
const stats = Filesystem.stat(filepath)
|
|
|
expect(stats?.mtime).toBeInstanceOf(Date)
|
|
|
@@ -285,17 +307,17 @@ describe("file/time", () => {
|
|
|
await using tmp = await tmpdir()
|
|
|
const filepath = path.join(tmp.path, "file.txt")
|
|
|
await fs.writeFile(filepath, "original", "utf-8")
|
|
|
+ await touch(filepath, 1_000)
|
|
|
|
|
|
await Instance.provide({
|
|
|
directory: tmp.path,
|
|
|
fn: async () => {
|
|
|
- FileTime.read(sessionID, filepath)
|
|
|
- await Bun.sleep(10)
|
|
|
+ await FileTime.read(sessionID, filepath)
|
|
|
|
|
|
const originalStat = Filesystem.stat(filepath)
|
|
|
|
|
|
- await Bun.sleep(100)
|
|
|
await fs.writeFile(filepath, "modified", "utf-8")
|
|
|
+ await touch(filepath, 2_000)
|
|
|
|
|
|
const newStat = Filesystem.stat(filepath)
|
|
|
expect(newStat!.mtime.getTime()).toBeGreaterThan(originalStat!.mtime.getTime())
|