Skip to content

Commit

Permalink
tests: fix test failure due to os.freemem() behaviour change in node …
Browse files Browse the repository at this point in the history
…v18 (#2530)

Recent node v18 nightly builds changed the behaviour of os.freemem()
on Linux to report "MemAvailable" from /proc/meminfo rather than
"MemFree". This broke our tests.
  • Loading branch information
trentm authored Jan 15, 2022
1 parent 7d2e759 commit 6acaaa0
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions test/metrics/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const os = require('os')

const semver = require('semver')
const test = require('tape')

const Metrics = require('../../lib/metrics')
Expand Down Expand Up @@ -73,10 +74,14 @@ test('reports expected metrics', function (t) {
},
'system.memory.actual.free': (value) => {
const free = os.freemem()
if (os.type() === 'Linux') {
// On Linux we use MemAvailable from /proc/meminfo as the value for this metric
// The Node.js API os.freemem() is reporting MemFree from the same file
t.ok(value > free, `is larger than os.freemem() (value: ${value}, free: ${free})`)
if (os.type() === 'Linux' && semver.lt(process.version, '18.0.0-nightly20220107')) {
// On Linux we use "MemAvailable" from /proc/meminfo as the value for
// this metric. In versions of Node.js before v18.0.0, `os.freemem()`
// reports "MemFree" from /proc/meminfo. (This changed in
// v18.0.0-nightly20220107b6b6510187 when node upgraded to libuv
// 1.43.0 to include https://github.com/libuv/libuv/pull/3351.)
t.ok(value > free, `is larger than os.freemem() (value: ${value},
free: ${free})`)
} else {
t.ok(isRoughly(value, free, 0.1), `is close to current free memory (value: ${value}, free: ${free})`)
}
Expand Down

0 comments on commit 6acaaa0

Please sign in to comment.