Skip to content

Commit 8e8d855

Browse files
authored
test: stabilize flaky fs.watch and worker-scaler tests (#4806)
- Skip the two FileWatcher tests that exercise fs.watch on Windows + Node 24 to avoid the libuv assertion in src/win/fs-event.c tracked upstream as nodejs/node#63638. - Replace the single short CPU burst + fixed sleep in the worker-scaler cooldown test with sustained load and polling for the expected worker count, so the scale-up is observed deterministically. Assisted-by: claude-code:claude-opus-4-7 Signed-off-by: Matteo Collina <[email protected]>
1 parent d1d05d0 commit 8e8d855

2 files changed

Lines changed: 48 additions & 27 deletions

File tree

‎packages/foundation/test/file-system.test.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ import {
1919
searchJavascriptFiles
2020
} from '../index.js'
2121

22+
// fs.watch aborts the process on Windows + Node 24 with a libuv assertion
23+
// in src/win/fs-event.c. Tracked upstream at https://github.com/nodejs/node/issues/63638.
24+
const skipFsWatch =
25+
process.platform === 'win32' && parseInt(process.versions.node.split('.')[0], 10) >= 24
26+
2227
test('FileWatcher - should throw an error if there is no path argument', async t => {
2328
throws(() => new FileWatcher({}), { message: 'path option is required' })
2429
})
@@ -64,7 +69,7 @@ test('FileWatcher - should not watch not allowed files', async t => {
6469
equal(false, fileWatcher.shouldFileBeWatched('another.file'))
6570
})
6671

67-
test('FileWatcher - should emit event if file is updated', async t => {
72+
test('FileWatcher - should emit event if file is updated', { skip: skipFsWatch }, async t => {
6873
const tmpDir = await mkdtemp(join(os.tmpdir(), 'plt-utils-test-'))
6974
const filename = join(tmpDir, 'test.file')
7075
const fileWatcher = new FileWatcher({ path: tmpDir })
@@ -85,7 +90,7 @@ test('FileWatcher - should emit event if file is updated', async t => {
8590
await Promise.race([sleep(5000), promise])
8691
})
8792

88-
test('FileWatcher - should not call fs watch twice', async t => {
93+
test('FileWatcher - should not call fs watch twice', { skip: skipFsWatch }, async t => {
8994
const tmpDir = await mkdtemp(join(os.tmpdir(), 'plt-utils-test-'))
9095
const filename = join(tmpDir, 'test.file')
9196
const fileWatcher = new FileWatcher({ path: tmpDir })

‎packages/runtime/test/worker-scaler-1.test.js‎

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,39 @@ const configurations = {
1616
'worker-scaler': 'platformatic.worker-scaler.json'
1717
}
1818

19+
function countWorkers (workers, applicationId) {
20+
let count = 0
21+
for (const worker of Object.values(workers)) {
22+
if (worker.application === applicationId) count++
23+
}
24+
return count
25+
}
26+
27+
async function waitForWorkers (app, applicationId, expectedCount, { timeoutMs = 30000, intervalMs = 250 } = {}) {
28+
const start = Date.now()
29+
let workers
30+
while (Date.now() - start < timeoutMs) {
31+
workers = await app.getWorkers()
32+
if (countWorkers(workers, applicationId) === expectedCount) return workers
33+
await sleep(intervalMs)
34+
}
35+
return workers
36+
}
37+
38+
async function driveLoad (entryUrl, signal) {
39+
while (!signal.aborted) {
40+
try {
41+
await request(entryUrl + '/service-2/cpu-intensive', {
42+
method: 'POST',
43+
headers: { 'Content-Type': 'application/json' },
44+
body: JSON.stringify({ timeout: 500 })
45+
})
46+
} catch {
47+
// Ignore transient errors while the scaler is adding workers.
48+
}
49+
}
50+
}
51+
1952
for (const [name, file] of Object.entries(configurations)) {
2053
test(`should scale an application if elu is higher than treshold (configuration ${name})`, async t => {
2154
const configFile = join(fixturesDir, 'worker-scaler', file)
@@ -71,33 +104,16 @@ for (const [name, file] of Object.entries(configurations)) {
71104

72105
t.after(() => app.close())
73106

74-
const { statusCode } = await request(entryUrl + '/service-2/cpu-intensive', {
75-
method: 'POST',
76-
headers: {
77-
'Content-Type': 'application/json'
78-
},
79-
body: JSON.stringify({ timeout: 1000 })
107+
const ac = new AbortController()
108+
const load = driveLoad(entryUrl, ac.signal)
109+
t.after(async () => {
110+
ac.abort()
111+
await load
80112
})
81-
assert.strictEqual(statusCode, 200)
82113

83-
await sleep(10000)
84-
85-
const workers = await app.getWorkers()
86-
87-
const service1Workers = []
88-
const service2Workers = []
89-
90-
for (const worker of Object.values(workers)) {
91-
if (worker.application === 'service-1') {
92-
service1Workers.push(worker)
93-
}
94-
if (worker.application === 'service-2') {
95-
service2Workers.push(worker)
96-
}
97-
}
98-
99-
assert.strictEqual(service1Workers.length, 1)
100-
assert.strictEqual(service2Workers.length, 2)
114+
const workers = await waitForWorkers(app, 'service-2', 2)
115+
assert.strictEqual(countWorkers(workers, 'service-1'), 1)
116+
assert.strictEqual(countWorkers(workers, 'service-2'), 2)
101117
})
102118

103119
test(`should not scale applications when the elu is lower than treshold (configuration ${name})`, async t => {

0 commit comments

Comments
 (0)