Skip to content

Commit

Permalink
Merge pull request #1 from lholmquist/gjethwani-event-emitter
Browse files Browse the repository at this point in the history
fix: exclude the rolling event emitter tests from the browser tests
  • Loading branch information
gjethwani authored Sep 13, 2023
2 parents f4e139d + 96b0d77 commit d3fbe16
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 76 deletions.
2 changes: 1 addition & 1 deletion test/browser/generate-index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=""

Expand Down
75 changes: 0 additions & 75 deletions test/enable-disable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
81 changes: 81 additions & 0 deletions test/rolling-event-emitter-test.js
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit d3fbe16

Please sign in to comment.