opensearch-project / opensearch-js

Node.js Client for OpenSearch
https://opensearch.org/docs/latest/clients/javascript/
Apache License 2.0
188 stars 122 forks source link

Fixed flaky circuitBreaker unit test. #893

Closed nhtruong closed 3 weeks ago

nhtruong commented 3 weeks ago

Here's the actual code in Transport.js:

    const MAX_STRING_LENGTH = buffer.constants.MAX_STRING_LENGTH;
    const HEAP_SIZE_LIMIT = require('v8').getHeapStatistics().heap_size_limit;
    ...
    const shouldApplyCircuitBreaker = (contentLength) => {
      if (!this.memoryCircuitBreaker || !this.memoryCircuitBreaker.enabled) return false;
      const maxPercentage = validateMemoryPercentage(this.memoryCircuitBreaker.maxPercentage);
      const heapUsed = process.memoryUsage().heapUsed;
      return contentLength + heapUsed > HEAP_SIZE_LIMIT * maxPercentage;
    };

However the current unit test sets the contentLength off of MAX_STRING_LENGTH instead of HEAP_SIZE_LIMIT. This results in the test failing on earlier versions of node.js where heapUsed is smaller and doesn't cause contentLength + heapUsed > HEAP_SIZE_LIMIT * maxPercentage to return true.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. For more information on following Developer Certificate of Origin and signing off your commits, please check here.