From 96b0d77aa66e383cbd1f4c39adde333b9fe17218 Mon Sep 17 00:00:00 2001 From: Lucas Holmquist Date: Tue, 12 Sep 2023 11:16:45 -0400 Subject: [PATCH] fix: exclude the rolling event emitter tests from the browser tests --- test/browser/generate-index.sh | 2 +- test/enable-disable-test.js | 75 --------------------------- test/rolling-event-emitter-test.js | 81 ++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 76 deletions(-) create mode 100644 test/rolling-event-emitter-test.js diff --git a/test/browser/generate-index.sh b/test/browser/generate-index.sh index 560b0080..e92861bc 100755 --- a/test/browser/generate-index.sh +++ b/test/browser/generate-index.sh @@ -3,7 +3,7 @@ echo $PWD cd test -file_list=$(ls -1 | grep .js) +file_list=$(ls -1 | grep .js | grep -v rolling-event-emitter) cd .. requires="" diff --git a/test/enable-disable-test.js b/test/enable-disable-test.js index 55aabd38..fa2fe744 100644 --- a/test/enable-disable-test.js +++ b/test/enable-disable-test.js @@ -58,78 +58,3 @@ test('When disabled the circuit should always be closed', t => { .then(t.end); }); }); - -test('When disabled, the event emitter listener should be removed', t => { - t.plan(2); - const emitter = new EventEmitter(); - const breaker = new CircuitBreaker(passFail, { - rotateBucketController: emitter - }); - t.equals(emitter.listeners('rotate').length, 1, 'listener attached automatically'); - breaker.disable(); - t.equals(emitter.listeners('rotate').length, 0, 'listener removed when breaker disabled'); - breaker.shutdown(); - t.end(); -}); - -test('Event listener should be removed only for the breaker that is disabled', t => { - t.plan(2); - const emitter = new EventEmitter(); - const breakerToBeDisabled = new CircuitBreaker(passFail, { - rotateBucketController: emitter - }); - const breakerNotToBeDisabled = new CircuitBreaker(passFail, { - rotateBucketController: emitter - }); - t.equals(emitter.listeners('rotate').length, 2, '1 listener attached for each breaker'); - breakerToBeDisabled.disable(); - t.equals(emitter.listeners('rotate').length, 1, '1 listener should be disabled and 1 should remain'); - breakerToBeDisabled.shutdown(); - breakerNotToBeDisabled.shutdown(); - t.end(); -}); - -test('Event listener should be re-added when circuit is re-enabled', t => { - t.plan(3); - const emitter = new EventEmitter(); - const breaker = new CircuitBreaker(passFail, { - rotateBucketController: emitter - }); - t.equals(emitter.listeners('rotate').length, 1, 'listener attached automatically'); - breaker.disable(); - t.equals(emitter.listeners('rotate').length, 0, 'listener removed when breaker disabled'); - breaker.enable(); - t.equals(emitter.listeners('rotate').length, 1, 'listener re-attached when breaker re-enabled'); - breaker.shutdown(); - t.end(); -}); - -test('Listener should not be attached with a call to enable if there is already a listener', t => { - t.plan(2); - const emitter = new EventEmitter(); - const breaker = new CircuitBreaker(passFail, { - rotateBucketController: emitter - }); - t.equals(emitter.listeners('rotate').length, 1, 'listener attached automatically'); - breaker.enable(); - t.equals(emitter.listeners('rotate').length, 1, 'listener should not be added again'); - breaker.shutdown(); - t.end(); -}); - -test('Listener should not be attached with a call to enable if there is already a listener and there is another breaker in the mix', t => { - t.plan(2); - const emitter = new EventEmitter(); - const breaker = new CircuitBreaker(passFail, { - rotateBucketController: emitter - }); - const anotherBreaker = new CircuitBreaker(passFail, { - rotateBucketController: emitter - }); - t.equals(emitter.listeners('rotate').length, 2, 'listener attached automatically'); - breaker.enable(); - t.equals(emitter.listeners('rotate').length, 2, 'listener should not be added again'); - breaker.shutdown(); - anotherBreaker.shutdown(); - t.end(); -}); diff --git a/test/rolling-event-emitter-test.js b/test/rolling-event-emitter-test.js new file mode 100644 index 00000000..b928ec94 --- /dev/null +++ b/test/rolling-event-emitter-test.js @@ -0,0 +1,81 @@ +'use strict'; + +const test = require('tape'); +const CircuitBreaker = require('../'); +const { passFail } = require('./common'); +const EventEmitter = require('events').EventEmitter; + +test('When disabled, the event emitter listener should be removed', t => { + t.plan(2); + const emitter = new EventEmitter(); + const breaker = new CircuitBreaker(passFail, { + rotateBucketController: emitter + }); + t.equals(emitter.listeners('rotate').length, 1, 'listener attached automatically'); + breaker.disable(); + t.equals(emitter.listeners('rotate').length, 0, 'listener removed when breaker disabled'); + breaker.shutdown(); + t.end(); +}); + +test('Event listener should be removed only for the breaker that is disabled', t => { + t.plan(2); + const emitter = new EventEmitter(); + const breakerToBeDisabled = new CircuitBreaker(passFail, { + rotateBucketController: emitter + }); + const breakerNotToBeDisabled = new CircuitBreaker(passFail, { + rotateBucketController: emitter + }); + t.equals(emitter.listeners('rotate').length, 2, '1 listener attached for each breaker'); + breakerToBeDisabled.disable(); + t.equals(emitter.listeners('rotate').length, 1, '1 listener should be disabled and 1 should remain'); + breakerToBeDisabled.shutdown(); + breakerNotToBeDisabled.shutdown(); + t.end(); +}); + +test('Event listener should be re-added when circuit is re-enabled', t => { + t.plan(3); + const emitter = new EventEmitter(); + const breaker = new CircuitBreaker(passFail, { + rotateBucketController: emitter + }); + t.equals(emitter.listeners('rotate').length, 1, 'listener attached automatically'); + breaker.disable(); + t.equals(emitter.listeners('rotate').length, 0, 'listener removed when breaker disabled'); + breaker.enable(); + t.equals(emitter.listeners('rotate').length, 1, 'listener re-attached when breaker re-enabled'); + breaker.shutdown(); + t.end(); +}); + +test('Listener should not be attached with a call to enable if there is already a listener', t => { + t.plan(2); + const emitter = new EventEmitter(); + const breaker = new CircuitBreaker(passFail, { + rotateBucketController: emitter + }); + t.equals(emitter.listeners('rotate').length, 1, 'listener attached automatically'); + breaker.enable(); + t.equals(emitter.listeners('rotate').length, 1, 'listener should not be added again'); + breaker.shutdown(); + t.end(); +}); + +test('Listener should not be attached with a call to enable if there is already a listener and there is another breaker in the mix', t => { + t.plan(2); + const emitter = new EventEmitter(); + const breaker = new CircuitBreaker(passFail, { + rotateBucketController: emitter + }); + const anotherBreaker = new CircuitBreaker(passFail, { + rotateBucketController: emitter + }); + t.equals(emitter.listeners('rotate').length, 2, 'listener attached automatically'); + breaker.enable(); + t.equals(emitter.listeners('rotate').length, 2, 'listener should not be added again'); + breaker.shutdown(); + anotherBreaker.shutdown(); + t.end(); +});