Skip to content

Commit 683412c

Browse files
authored
refactor: add timeout for race condition in heart test (#5131)
* refactor: add timeout for race condition in heart test * fixup!: set mtime to 0 and check for update * fixup!: use utimes directly instead of file open * fixup!: remove import
1 parent 18ff996 commit 683412c

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

test/unit/node/heart.test.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { logger } from "@coder/logger"
2-
import { readFile, writeFile, stat } from "fs/promises"
2+
import { readFile, writeFile, stat, utimes } from "fs/promises"
33
import { Heart, heartbeatTimer } from "../../../src/node/heart"
44
import { clean, mockLogger, tmpdir } from "../../utils/helpers"
55

@@ -33,17 +33,26 @@ describe("Heart", () => {
3333
const pathToFile = `${testDir}/file.txt`
3434
await writeFile(pathToFile, text)
3535
const fileContents = await readFile(pathToFile, { encoding: "utf8" })
36-
const fileStatusBeforeEdit = await stat(pathToFile)
36+
// Explicitly set the modified time to 0 so that we can check
37+
// that the file was indeed modified after calling heart.beat().
38+
// This works around any potential race conditions.
39+
// Docs: https://nodejs.org/api/fs.html#fspromisesutimespath-atime-mtime
40+
await utimes(pathToFile, 0, 0)
41+
3742
expect(fileContents).toBe(text)
3843

3944
heart = new Heart(pathToFile, mockIsActive(true))
4045
heart.beat()
46+
// HACK@jsjoeio - beat has some async logic but is not an async method
47+
// Therefore, we have to create an artificial wait in order to make sure
48+
// all async code has completed before asserting
49+
await new Promise((r) => setTimeout(r, 100))
4150
// Check that the heart wrote to the heartbeatFilePath and overwrote our text
4251
const fileContentsAfterBeat = await readFile(pathToFile, { encoding: "utf8" })
4352
expect(fileContentsAfterBeat).not.toBe(text)
4453
// Make sure the modified timestamp was updated.
4554
const fileStatusAfterEdit = await stat(pathToFile)
46-
expect(fileStatusAfterEdit.mtimeMs).toBeGreaterThan(fileStatusBeforeEdit.mtimeMs)
55+
expect(fileStatusAfterEdit.mtimeMs).toBeGreaterThan(0)
4756
})
4857
it("should log a warning when given an invalid file path", async () => {
4958
heart = new Heart(`fakeDir/fake.txt`, mockIsActive(false))
@@ -52,7 +61,6 @@ describe("Heart", () => {
5261
// Therefore, we have to create an artificial wait in order to make sure
5362
// all async code has completed before asserting
5463
await new Promise((r) => setTimeout(r, 100))
55-
// expect(logger.trace).toHaveBeenCalled()
5664
expect(logger.warn).toHaveBeenCalled()
5765
})
5866
it("should be active after calling beat", () => {

0 commit comments

Comments
 (0)