Skip to content

Commit

Permalink
Merge pull request #13 from epferrari/log-at-highest-severity
Browse files Browse the repository at this point in the history
Log at highest level that has its threshold crossed
  • Loading branch information
epferrari authored Dec 16, 2020
2 parents 7269cda + b9ed06b commit e59edd0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/strongbus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,13 @@ export class Bus<TEventMap extends object = object> implements Scannable<TEventM
private addListener(event: EventKeys<TEventMap>|Events.WILDCARD, handler: EventHandlers.GenericHandler): Events.Subscription {
const {thresholds, logger} = this.options;
const handlers = this.bus.get(event);
const n: number = handlers?.size || 0;
if(n > thresholds.info) {
logger.info(`${this.name} has ${n} listeners for "${event}", ${thresholds.info} max listeners expected.`);
const n: number = handlers?.size + 1 || 1;
if(n > thresholds.error) {
logger.error(`Potential Memory Leak. ${this.name} has ${n} listeners for "${event}", exceeds threshold set to ${thresholds.error}`);
} else if(n > thresholds.warn) {
logger.warn(`Potential Memory Leak. ${this.name} has ${n} listeners for "${event}", exceeds threshold set to ${thresholds.warn}`);
} else if(n > thresholds.error) {
logger.error(`Potential Memory Leak. ${this.name} has ${n} listeners for "${event}", exceeds threshold set to ${thresholds.error}`);
} else if(n > thresholds.info) {
logger.info(`${this.name} has ${n} listeners for "${event}", ${thresholds.info} max listeners expected.`);
}
this.willAddListener(event);
const {added} = addListener(this.bus, event, handler);
Expand Down
65 changes: 65 additions & 0 deletions src/strongbus_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {sleep} from 'jaasync/lib/cancelable';

import * as Strongbus from './';
import {Scanner} from './scanner';
import {Logger} from './types/logger';
import {EventKeys} from './types/utility';

type TestEventMap = {
Expand Down Expand Up @@ -92,6 +93,70 @@ describe('Strongbus.Bus', () => {
});
});

describe('listener logging thresholds', () => {
let logger: jasmine.SpyObj<Logger>;

function addListeners(numListenersToAdd: number) {
for(let i = 0; i < numListenersToAdd; i++) {
bus.on('bar', () => true);
}
}

beforeEach(() => {
logger = jasmine.createSpyObj('logger', ['info', 'warn', 'error']);
});

it('logs if the number of listeners exceeds the thresholds', () => {
bus = new Strongbus.Bus<TestEventMap>({
logger,
thresholds: {
info: 10,
warn: 20,
error: 30
}
});
// no logging up to the threshold
addListeners(10);
expect(logger.info).not.toHaveBeenCalled();
expect(logger.warn).not.toHaveBeenCalled();
expect(logger.error).not.toHaveBeenCalled();

// 11th listener triggers info
addListeners(1);
expect(bus.listeners.get('bar').size).toEqual(11);
expect(logger.info).toHaveBeenCalledTimes(1);
expect(logger.warn).not.toHaveBeenCalled();
expect(logger.error).not.toHaveBeenCalled();

// 21st listener triggers warn
addListeners(10);
expect(logger.info).toHaveBeenCalledTimes(10);
expect(logger.warn).toHaveBeenCalledTimes(1);
expect(logger.error).not.toHaveBeenCalled();

// 31st listener triggers error
addListeners(10);
expect(logger.info).toHaveBeenCalledTimes(10);
expect(logger.warn).toHaveBeenCalledTimes(10);
expect(logger.error).toHaveBeenCalledTimes(1);
});

it('logs at the highest severity that passes the threshold', () => {
bus = new Strongbus.Bus<TestEventMap>({
logger,
thresholds: {
// only warn-level specified, info is at default of 100
warn: 20
}
});

addListeners(21);
expect(logger.info).not.toHaveBeenCalled();
expect(logger.warn).toHaveBeenCalledTimes(1);
expect(logger.error).not.toHaveBeenCalled();
});
});

describe('#on', () => {
it('subscribes handler to an event as a key of its typemap', () => {
const handleFoo = jasmine.createSpy('handleFoo') as (fooPayload: string) => void;
Expand Down

0 comments on commit e59edd0

Please sign in to comment.