From 6a06ca1ed113589979b64f9e7771b1198fe883e1 Mon Sep 17 00:00:00 2001 From: Mark Haslinghuis Date: Thu, 12 Jun 2025 22:57:37 +0200 Subject: [PATCH] More coderabbit --- src/js/msp/msp_queue_monitor.js | 18 +++++++++++++++++- src/js/msp/msp_stress_test.js | 11 ++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/js/msp/msp_queue_monitor.js b/src/js/msp/msp_queue_monitor.js index 68e38364..f4730d4a 100644 --- a/src/js/msp/msp_queue_monitor.js +++ b/src/js/msp/msp_queue_monitor.js @@ -46,6 +46,12 @@ export class MSPQueueMonitor { * Hook into MSP methods to collect real-time metrics */ _hookMSPMethods() { + // Check if MSP instance is already instrumented to prevent double-patching + if (this.msp._mspQueueMonitorInstrumented) { + console.warn("MSP instance is already instrumented by MSPQueueMonitor"); + return; + } + // Store original methods this.originalSendMessage = this.msp.send_message.bind(this.msp); this.originalDispatchMessage = this.msp._dispatch_message.bind(this.msp); @@ -70,6 +76,9 @@ export class MSPQueueMonitor { return this.originalRemoveRequest(requestObj); }; } + + // Mark MSP instance as instrumented + this.msp._mspQueueMonitorInstrumented = true; } /** @@ -453,7 +462,8 @@ export class MSPQueueMonitor { } // Deduct for queue size issues - const queueRatio = this.currentQueueSize / (this.msp.MAX_QUEUE_SIZE || 50); + const currentQueueSize = this.currentQueueSize || (this.msp.callbacks?.length ?? 0); + const queueRatio = currentQueueSize / (this.msp.MAX_QUEUE_SIZE || 50); if (queueRatio > 0.8) { score -= 20; } else if (queueRatio > 0.6) { @@ -596,7 +606,13 @@ export class MSPQueueMonitor { this.msp._removeRequestFromCallbacks = this.originalRemoveRequest; } + // Clear instrumentation flag + delete this.msp._mspQueueMonitorInstrumented; + this.listeners = []; + + // Clear the singleton instance to allow creating a fresh monitor later + _mspQueueMonitorInstance = null; } } diff --git a/src/js/msp/msp_stress_test.js b/src/js/msp/msp_stress_test.js index 0a630f75..2fce2916 100644 --- a/src/js/msp/msp_stress_test.js +++ b/src/js/msp/msp_stress_test.js @@ -3,12 +3,12 @@ * Comprehensive testing tool for MSP queue management, timeout handling, and performance */ -import { MSPQueueMonitor } from "./msp_queue_monitor.js"; +import { mspQueueMonitor } from "./msp_queue_monitor.js"; export class MSPStressTest { constructor(mspInstance) { this.msp = mspInstance; - this.monitor = new MSPQueueMonitor(mspInstance); + this.monitor = mspQueueMonitor; // Reuse singleton to avoid duplicate method patching this.isRunning = false; this.testResults = []; this.currentTest = null; @@ -92,6 +92,7 @@ export class MSPStressTest { } this.monitor.stopMonitoring(); + this.monitor.destroy(); // Clean up MSP method patches and restore original behavior this.testResults = results; const report = this.generateTestReport(results); @@ -117,7 +118,7 @@ export class MSPStressTest { } const results = await Promise.allSettled(promises); - const successful = results.filter((r) => r.status === "fulfilled" && !r.value.error).length; + const successful = results.filter((r) => r.status === "fulfilled" && !(r.value && r.value.error)).length; const failed = results.length - successful; return { @@ -194,9 +195,9 @@ export class MSPStressTest { } const results = await Promise.allSettled(promises); - const successful = results.filter((r) => r.status === "fulfilled" && !r.value.error).length; + const successful = results.filter((r) => r.status === "fulfilled" && !(r.value && r.value.error)).length; const duplicateErrors = results.filter( - (r) => r.status === "rejected" || r?.value?.error?.includes("duplicate"), + (r) => r.status === "rejected" || (r.value && r.value.error && r.value.error.includes("duplicate")), ).length; return {