From 0d95c67f4455c4cd1d94ea2e396e8cbfa0f09017 Mon Sep 17 00:00:00 2001 From: Leonardo Venturini Date: Wed, 16 Oct 2024 15:17:14 -0400 Subject: [PATCH] test garbage collector --- async-interceptor.js | 3 +- save-output.js | 1 - tests/async-interceptor.test.js | 55 +++++++++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/async-interceptor.js b/async-interceptor.js index e507b6f..ef1d6b8 100644 --- a/async-interceptor.js +++ b/async-interceptor.js @@ -1,7 +1,7 @@ import async_hooks from 'node:async_hooks'; // 50% of map limit -export const GC_LIMIT = Math.pow(2, 23); +export const GC_LIMIT = Meteor.isPackageTest ? 7 : Math.pow(2, 23); export const AsyncResourceMap = new Map(); @@ -17,7 +17,6 @@ function captureResource(asyncId, type) { stack = `${type}\n${stack}`; if (AsyncResourceMap.size > GC_LIMIT) { - console.log('Meteor Perf: Reached AsyncResourceMap limit, garbage collecting'); garbageCollectAsyncResources(); } diff --git a/save-output.js b/save-output.js index 14a20f6..84cefb2 100644 --- a/save-output.js +++ b/save-output.js @@ -16,7 +16,6 @@ export function saveOutput() { async_traces.push({ count: info.count, - types: [...info.types], stack, }); }); diff --git a/tests/async-interceptor.test.js b/tests/async-interceptor.test.js index 366d5ee..475fce5 100644 --- a/tests/async-interceptor.test.js +++ b/tests/async-interceptor.test.js @@ -1,5 +1,5 @@ import { expect } from 'chai'; -import { AsyncInterceptor, AsyncResourceMap, stackTrace } from '../async-interceptor'; +import { AsyncInterceptor, AsyncResourceMap, GC_LIMIT, stackTrace } from '../async-interceptor'; import Benchmarkify from "benchmarkify"; const benchmark = new Benchmarkify("Meteor Perf", { chartImage: true }).printHeader(); @@ -14,7 +14,6 @@ benchmark.createSuite("Stack Trace", { time: 1000 }) }).join('\n'); }); - await benchmark.run(); describe('Async Interceptor', () => { @@ -35,4 +34,54 @@ describe('Async Interceptor', () => { expect(AsyncResourceMap.size).to.be.greaterThan(0); }) -}) \ No newline at end of file + + it('should not capture async operations when disabled', async () => { + AsyncInterceptor.disable(); + + AsyncResourceMap.clear(); + + await new Promise((resolve) => { + setTimeout(resolve, 100); + }); + + expect(AsyncResourceMap.size).to.equal(0); + }); + + it('should garbage collect async resources', async () => { + AsyncInterceptor.enable(); + + for (let i = 0; i < GC_LIMIT + 1; i++) { + await new Promise((resolve) => { + setTimeout(resolve, 1); + }); + await new Promise((resolve) => { + setTimeout(resolve, 1); + }); + await new Promise((resolve) => { + setTimeout(resolve, 1); + }); + await new Promise((resolve) => { + setTimeout(resolve, 1); + }); + await new Promise((resolve) => { + setTimeout(resolve, 1); + }); + await new Promise((resolve) => { + setTimeout(resolve, 1); + }); + await new Promise((resolve) => { + setTimeout(resolve, 1); + }); + await new Promise((resolve) => { + setTimeout(resolve, 1); + }); + await new Promise((resolve) => { + setTimeout(resolve, 1); + }); + } + + AsyncInterceptor.disable(); + + expect(AsyncResourceMap.size).to.be.lessThanOrEqual(GC_LIMIT); + }) +}); \ No newline at end of file