From b34ee02abb8be29454ce53909d438c5f99ec03f0 Mon Sep 17 00:00:00 2001 From: Dzianis Dashkevich Date: Thu, 6 Jun 2024 14:24:49 -0400 Subject: [PATCH] chore: add tests --- package.json | 4 +- src/retry.js | 12 ++- test/index.js | 18 ++++- test/interceptors.js | 163 ++++++++++++++++++++++++++++++++++++++ test/retry.js | 182 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 375 insertions(+), 4 deletions(-) create mode 100644 test/interceptors.js create mode 100644 test/retry.js diff --git a/package.json b/package.json index 3e72025..ad1df90 100644 --- a/package.json +++ b/package.json @@ -46,9 +46,11 @@ "start": "npm run build -- -w", "build": "babel-config-cjs src -d lib", "test:index": "run-browser test/index.js -b -m test/mock-server.js | tap-spec", + "test:interceptors": "run-browser test/interceptors.js -b -m test/mock-server.js | tap-spec", + "test:retry": "run-browser test/retry.js -b -m test/mock-server.js | tap-spec", "test:http-handler": "run-browser test/http-handler.js -b -m test/mock-server.js | tap-spec", "pretest": "npm run build", - "test": "npm run test:index && npm run test:http-handler", + "test": "npm run test:index && npm run test:http-handler && npm run test:interceptors && npm run test:retry", "browser": "run-browser -m test/mock-server.js test/index.js" } } diff --git a/src/retry.js b/src/retry.js index ee5037a..98f0457 100644 --- a/src/retry.js +++ b/src/retry.js @@ -93,6 +93,14 @@ class Retry { return this.currentDelay_; } + getCurrentMinPossibleDelay() { + return (1 - this.fuzzFactor_) * this.currentDelay_; + } + + getCurrentMaxPossibleDelay() { + return (1 + this.fuzzFactor_) * this.currentDelay_; + } + /** * For example fuzzFactor is 0.1 * This means ±10% deviation @@ -101,8 +109,8 @@ class Retry { * @private */ getCurrentFuzzedDelay() { - const lowValue = (1 - this.fuzzFactor_) * this.currentDelay_; - const highValue = (1 + this.fuzzFactor_) * this.currentDelay_; + const lowValue = this.getCurrentMinPossibleDelay(); + const highValue = this.getCurrentMaxPossibleDelay(); return lowValue + Math.random() * (highValue - lowValue); } diff --git a/test/index.js b/test/index.js index fe84126..c6ca9ba 100644 --- a/test/index.js +++ b/test/index.js @@ -11,7 +11,8 @@ test("constructs and calls callback without throwing", { timeout: 500 }, functio }) }) -test("[func] Can GET a url (cross-domain)", { timeout: 2000 }, function(assert) { +// skipping this test because test url is no longer valid +test.skip("[func] Can GET a url (cross-domain)", { timeout: 2000 }, function(assert) { xhr({ uri: "http://www.mocky.io/v2/55a02cb72651260b1a94f024", useXDR: true @@ -381,3 +382,18 @@ test('httpHandler is available on XHR', function(assert) { assert.ok(xhr.httpHandler) assert.end(); }); + +test('requestInterceptorStorage is available on XHR', function(assert) { + assert.ok(xhr.requestInterceptorsStorage); + assert.end(); +}); + +test('responseInterceptorStorage is available on XHR', function(assert) { + assert.ok(xhr.responseInterceptorsStorage); + assert.end(); +}); + +test('retryManager is available on XHR', function(assert) { + assert.ok(xhr.retryManager); + assert.end(); +}); diff --git a/test/interceptors.js b/test/interceptors.js new file mode 100644 index 0000000..985f5cc --- /dev/null +++ b/test/interceptors.js @@ -0,0 +1,163 @@ +var window = require("global/window") +var test = require("tape") + +var InterceptorsStorage = require('../lib/interceptors'); + +test("should ignore duplicate interceptors", function(assert) { + var interceptorsStorage = new InterceptorsStorage(); + + function interceptor(request) { + return request; + } + + assert.equal(interceptorsStorage.addInterceptor('license', interceptor), true); + assert.equal(interceptorsStorage.addInterceptor('license', interceptor), false); + assert.end(); +}); + +test("should execute registered interceptors", function(assert) { + var interceptorsStorage = new InterceptorsStorage(); + + var count = 0; + + function interceptor(request) { + assert.equal(request.data, 'license-data'); + count++; + return request; + } + + interceptorsStorage.addInterceptor('license', interceptor); + interceptorsStorage.execute('license', { data: 'license-data' }); + assert.equal(count, 1); + assert.end(); +}); + + +test("should not execute removed interceptors", function(assert) { + var interceptorsStorage = new InterceptorsStorage(); + + var count = 0; + + function interceptor(request) { + assert.equal(request.data, 'license-data'); + count++; + return request; + } + + interceptorsStorage.addInterceptor('license', interceptor); + interceptorsStorage.removeInterceptor('license', interceptor); + interceptorsStorage.execute('license', { data: 'license-data' }); + assert.equal(count, 0); + assert.end(); +}); + +test("should return interceptors by type", function(assert) { + var interceptorsStorage = new InterceptorsStorage(); + + function interceptorOne(request) { + return request; + } + + function interceptorTwo(request) { + return request; + } + + interceptorsStorage.addInterceptor('license', interceptorOne); + interceptorsStorage.addInterceptor('segment', interceptorTwo); + + var setLicense = interceptorsStorage.getForType('license'); + var setSegment = interceptorsStorage.getForType('segment'); + + assert.equal(setLicense.size, 1); + assert.equal(setSegment.size, 1); + assert.end(); +}); + +test("should clear interceptors by type", function(assert) { + var interceptorsStorage = new InterceptorsStorage(); + + function interceptorOne(request) { + return request; + } + + function interceptorTwo(request) { + return request; + } + + interceptorsStorage.addInterceptor('license', interceptorOne); + interceptorsStorage.addInterceptor('segment', interceptorTwo); + + interceptorsStorage.clearInterceptorsByType('license'); + + var setLicense = interceptorsStorage.getForType('license'); + var setSegment = interceptorsStorage.getForType('segment'); + + assert.equal(setLicense.size, 0); + assert.equal(setSegment.size, 1); + assert.end(); +}); + + +test("should clear all interceptors", function(assert) { + var interceptorsStorage = new InterceptorsStorage(); + + function interceptorOne(request) { + return request; + } + + function interceptorTwo(request) { + return request; + } + + interceptorsStorage.addInterceptor('license', interceptorOne); + interceptorsStorage.addInterceptor('segment', interceptorTwo); + + interceptorsStorage.clear(); + + var setLicense = interceptorsStorage.getForType('license'); + var setSegment = interceptorsStorage.getForType('segment'); + + assert.equal(setLicense.size, 0); + assert.equal(setSegment.size, 0); + assert.end(); +}); + + +test("should set enabled", function(assert) { + var interceptorsStorage = new InterceptorsStorage(); + + assert.equal(interceptorsStorage.getIsEnabled(), false); + interceptorsStorage.enable(); + assert.equal(interceptorsStorage.getIsEnabled(), true); + interceptorsStorage.disable(); + assert.equal(interceptorsStorage.getIsEnabled(), false); + assert.end(); +}); + + +test("should reset", function(assert) { + var interceptorsStorage = new InterceptorsStorage(); + + function interceptorOne(request) { + return request; + } + + function interceptorTwo(request) { + return request; + } + + interceptorsStorage.addInterceptor('license', interceptorOne); + interceptorsStorage.addInterceptor('segment', interceptorTwo); + interceptorsStorage.enable(); + + assert.equal( interceptorsStorage.getForType('license').size, 1); + assert.equal( interceptorsStorage.getForType('segment').size, 1); + assert.equal(interceptorsStorage.getIsEnabled(), true); + + interceptorsStorage.reset(); + + assert.equal( interceptorsStorage.getForType('license').size, 0); + assert.equal( interceptorsStorage.getForType('segment').size, 0); + assert.equal(interceptorsStorage.getIsEnabled(), false); + assert.end(); +}); diff --git a/test/retry.js b/test/retry.js new file mode 100644 index 0000000..ff7921e --- /dev/null +++ b/test/retry.js @@ -0,0 +1,182 @@ +var window = require("global/window") +var test = require("tape") + +var RetryManager = require('../lib/retry'); + +test("should set enabled", function(assert) { + var retryManager = new RetryManager(); + + assert.equal(retryManager.getIsEnabled(), false); + retryManager.enable(); + assert.equal(retryManager.getIsEnabled(), true); + retryManager.disable(); + assert.equal(retryManager.getIsEnabled(), false); + assert.end(); +}); + +test("should set maxAttempts", function(assert) { + var retryManager = new RetryManager(); + + assert.equal(retryManager.getMaxAttempts(), 1); + retryManager.setMaxAttempts(2); + assert.equal(retryManager.getMaxAttempts(), 2); + assert.end(); +}); + +test("should set delayFactor", function(assert) { + var retryManager = new RetryManager(); + + assert.equal(retryManager.getDelayFactor(), 0.1); + retryManager.setDelayFactor(0.2); + assert.equal(retryManager.getDelayFactor(), 0.2); + assert.end(); +}); + + +test("should set fuzzFactor", function(assert) { + var retryManager = new RetryManager(); + + assert.equal(retryManager.getFuzzFactor(), 0.1); + retryManager.setFuzzFactor(0.2); + assert.equal(retryManager.getFuzzFactor(), 0.2); + assert.end(); +}); + +test("should set initialDelay", function(assert) { + var retryManager = new RetryManager(); + + assert.equal(retryManager.getInitialDelay(), 1000); + retryManager.setInitialDelay(2000); + assert.equal(retryManager.getInitialDelay(), 2000); + assert.end(); +}); + +test("should reset", function(assert) { + var retryManager = new RetryManager(); + + assert.equal(retryManager.getInitialDelay(), 1000); + retryManager.setInitialDelay(2000); + assert.equal(retryManager.getInitialDelay(), 2000); + + assert.equal(retryManager.getFuzzFactor(), 0.1); + retryManager.setFuzzFactor(0.2); + assert.equal(retryManager.getFuzzFactor(), 0.2); + + assert.equal(retryManager.getDelayFactor(), 0.1); + retryManager.setDelayFactor(0.2); + assert.equal(retryManager.getDelayFactor(), 0.2); + + assert.equal(retryManager.getMaxAttempts(), 1); + retryManager.setMaxAttempts(2); + assert.equal(retryManager.getMaxAttempts(), 2); + + assert.equal(retryManager.getIsEnabled(), false); + retryManager.enable(); + assert.equal(retryManager.getIsEnabled(), true); + + retryManager.reset(); + + assert.equal(retryManager.getInitialDelay(), 1000); + assert.equal(retryManager.getFuzzFactor(), 0.1); + assert.equal(retryManager.getDelayFactor(), 0.1); + assert.equal(retryManager.getMaxAttempts(), 1); + assert.equal(retryManager.getIsEnabled(), false); + assert.end(); +}); + + +test("should create Retry instance", function(assert) { + var retryManager = new RetryManager(); + + retryManager.setMaxAttempts(3); + + var retry = retryManager.createRetry(); + + var shouldRetry; + var currentDelay; + var currentFuzzedDelay; + var currentMaxPossibleDelay; + var currentMinPossibleDelay; + + + shouldRetry = retry.shouldRetry(); + currentDelay = retry.getCurrentDelay(); + currentFuzzedDelay = retry.getCurrentFuzzedDelay(); + currentMinPossibleDelay = retry.getCurrentMinPossibleDelay(); + currentMaxPossibleDelay = retry.getCurrentMaxPossibleDelay(); + + assert.equal(shouldRetry, true); + assert.equal(currentDelay, 1000); + assert.equal(currentMinPossibleDelay, 900); + assert.equal(currentMaxPossibleDelay, 1100); + assert.ok( + currentFuzzedDelay >= currentMinPossibleDelay && currentFuzzedDelay <= currentMaxPossibleDelay, + "Current Fuzzed Received: " + currentFuzzedDelay); + + retry.moveToNextAttempt(); + + shouldRetry = retry.shouldRetry(); + currentDelay = retry.getCurrentDelay(); + currentFuzzedDelay = retry.getCurrentFuzzedDelay(); + currentMinPossibleDelay = retry.getCurrentMinPossibleDelay(); + currentMaxPossibleDelay = retry.getCurrentMaxPossibleDelay(); + + assert.equal(shouldRetry, true); + assert.equal(currentDelay, 1100); + assert.equal(currentMinPossibleDelay, 990); + assert.equal(currentMaxPossibleDelay, 1210); + assert.ok( + currentFuzzedDelay >= currentMinPossibleDelay && currentFuzzedDelay <= currentMaxPossibleDelay, + "Current Fuzzed Received: " + currentFuzzedDelay); + + retry.moveToNextAttempt(); + + shouldRetry = retry.shouldRetry(); + + assert.equal(shouldRetry, false); + + assert.end(); +}); + + +test("should create Retry instance with overwritten properties", function(assert) { + var retryManager = new RetryManager(); + + retryManager.setMaxAttempts(3); + + var retry = retryManager.createRetry({ + maxAttempts: 2, + delayFactor: 0.2, + fuzzFactor: 0.2, + initialDelay: 3000, + }); + + var shouldRetry; + var currentDelay; + var currentFuzzedDelay; + var currentMaxPossibleDelay; + var currentMinPossibleDelay; + + + shouldRetry = retry.shouldRetry(); + currentDelay = retry.getCurrentDelay(); + currentFuzzedDelay = retry.getCurrentFuzzedDelay(); + currentMinPossibleDelay = retry.getCurrentMinPossibleDelay(); + currentMaxPossibleDelay = retry.getCurrentMaxPossibleDelay(); + + assert.equal(shouldRetry, true); + assert.equal(currentDelay, 3000); + assert.equal(currentMinPossibleDelay, 2400); + assert.equal(currentMaxPossibleDelay, 3600); + assert.ok( + currentFuzzedDelay >= currentMinPossibleDelay && currentFuzzedDelay <= currentMaxPossibleDelay, + "Current Fuzzed Received: " + currentFuzzedDelay); + + retry.moveToNextAttempt(); + + shouldRetry = retry.shouldRetry(); + + assert.equal(shouldRetry, false); + + assert.end(); +});