From b7ba0688539ab173bda8f73c39ba9f254870f408 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Wed, 28 Dec 2022 22:28:47 +0800 Subject: [PATCH] test: fix memory test assertion --- memory-testing/test-util.mjs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/memory-testing/test-util.mjs b/memory-testing/test-util.mjs index 11166c81..659f2494 100644 --- a/memory-testing/test-util.mjs +++ b/memory-testing/test-util.mjs @@ -44,14 +44,19 @@ export async function createSuite(testFile, maxMemoryUsage) { const stats = await container.stats() let shouldAssertMemoryUsage = false - - const initialMemoryUsage = await new Promise((resolve, reject) => { + let initialMemoryUsage + await new Promise((resolve, reject) => { + const initialDate = Date.now() stats.on('data', (d) => { const { memory_stats } = JSON.parse(d.toString('utf8')) - resolve(memory_stats.usage) + if (Date.now() - initialDate > 10000 && !shouldAssertMemoryUsage) { + resolve() + initialMemoryUsage = memory_stats.usage + shouldAssertMemoryUsage = true + } if (shouldAssertMemoryUsage && memory_stats?.usage) { const memoryGrowth = memory_stats.usage - initialMemoryUsage - if (memoryGrowth > maxMemoryUsage ?? initialMemoryUsage) { + if (memoryGrowth > (maxMemoryUsage ?? initialMemoryUsage)) { console.info( chalk.redBright( `Potential memory leak, memory growth: ${prettyBytes( @@ -72,8 +77,6 @@ export async function createSuite(testFile, maxMemoryUsage) { await sleep(60000) - shouldAssertMemoryUsage = true - try { await container.stop() await container.remove()