fix: fix worker usage statistics handling
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 27 Aug 2023 20:09:42 +0000 (22:09 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 27 Aug 2023 20:09:42 +0000 (22:09 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
.c8rc.json
CHANGELOG.md
src/utils.ts
tests/utils.test.js

index a6da2792307b6fea19313da25a353042c8d3d56c..ab1b20fa6e8670179e6a804576b2dcf7987f48d6 100644 (file)
@@ -3,5 +3,5 @@
   "lines": 92,
   "statements": 92,
   "functions": 95,
-  "branches": 90
+  "branches": 92
 }
index f4c452a7b421f2477c216f0fd51cd204cafa9b1a..6cd21047cc543086772a815d6aba4bf7911ca5b6 100644 (file)
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
+### Fixed
+
+- Ensure unused worker usage statistics are deleted at runtime.
+
 ### Changed
 
 - Rename worker choice strategy options `choiceRetries` to `retries`.
index 38e15eb6d4c257779a2a68590d578b8246b6cfbc..82c38c09e7161fd06e2c6f1628fdaad1e8f4c63f 100644 (file)
@@ -257,9 +257,13 @@ export const updateMeasurementStatistics = (
       measurementStatistics.history.push(measurementValue)
       if (measurementRequirements.average) {
         measurementStatistics.average = average(measurementStatistics.history)
+      } else if (measurementStatistics.average != null) {
+        delete measurementStatistics.average
       }
       if (measurementRequirements.median) {
         measurementStatistics.median = median(measurementStatistics.history)
+      } else if (measurementStatistics.median != null) {
+        delete measurementStatistics.median
       }
     }
   }
index ef7688d6f337514bea5a405b18c608819b8d66ac..2c41b6920657944f9c498329233b470f2b99ce43 100644 (file)
@@ -232,6 +232,41 @@ describe('Utils test suite', () => {
       average: 0.002,
       history: new CircularArray(DEFAULT_CIRCULAR_ARRAY_SIZE, 0.001, 0.003)
     })
+    updateMeasurementStatistics(
+      measurementStatistics,
+      { aggregate: true, average: false, median: true },
+      0.006
+    )
+    expect(measurementStatistics).toStrictEqual({
+      aggregate: 0.04,
+      maximum: 0.02,
+      minimum: 0.001,
+      median: 0.003,
+      history: new CircularArray(
+        DEFAULT_CIRCULAR_ARRAY_SIZE,
+        0.001,
+        0.003,
+        0.006
+      )
+    })
+    updateMeasurementStatistics(
+      measurementStatistics,
+      { aggregate: true, average: true, median: false },
+      0.01
+    )
+    expect(measurementStatistics).toStrictEqual({
+      aggregate: 0.05,
+      maximum: 0.02,
+      minimum: 0.001,
+      average: 0.005,
+      history: new CircularArray(
+        DEFAULT_CIRCULAR_ARRAY_SIZE,
+        0.001,
+        0.003,
+        0.006,
+        0.01
+      )
+    })
   })
 
   it('Verify secureRandom() behavior', () => {