Skip to content

Commit e59edd0

Browse files
authored
Merge pull request #13 from epferrari/log-at-highest-severity
Log at highest level that has its threshold crossed
2 parents 7269cda + b9ed06b commit e59edd0

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

src/strongbus.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,13 +408,13 @@ export class Bus<TEventMap extends object = object> implements Scannable<TEventM
408408
private addListener(event: EventKeys<TEventMap>|Events.WILDCARD, handler: EventHandlers.GenericHandler): Events.Subscription {
409409
const {thresholds, logger} = this.options;
410410
const handlers = this.bus.get(event);
411-
const n: number = handlers?.size || 0;
412-
if(n > thresholds.info) {
413-
logger.info(`${this.name} has ${n} listeners for "${event}", ${thresholds.info} max listeners expected.`);
411+
const n: number = handlers?.size + 1 || 1;
412+
if(n > thresholds.error) {
413+
logger.error(`Potential Memory Leak. ${this.name} has ${n} listeners for "${event}", exceeds threshold set to ${thresholds.error}`);
414414
} else if(n > thresholds.warn) {
415415
logger.warn(`Potential Memory Leak. ${this.name} has ${n} listeners for "${event}", exceeds threshold set to ${thresholds.warn}`);
416-
} else if(n > thresholds.error) {
417-
logger.error(`Potential Memory Leak. ${this.name} has ${n} listeners for "${event}", exceeds threshold set to ${thresholds.error}`);
416+
} else if(n > thresholds.info) {
417+
logger.info(`${this.name} has ${n} listeners for "${event}", ${thresholds.info} max listeners expected.`);
418418
}
419419
this.willAddListener(event);
420420
const {added} = addListener(this.bus, event, handler);

src/strongbus_spec.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {sleep} from 'jaasync/lib/cancelable';
22

33
import * as Strongbus from './';
44
import {Scanner} from './scanner';
5+
import {Logger} from './types/logger';
56
import {EventKeys} from './types/utility';
67

78
type TestEventMap = {
@@ -92,6 +93,70 @@ describe('Strongbus.Bus', () => {
9293
});
9394
});
9495

96+
describe('listener logging thresholds', () => {
97+
let logger: jasmine.SpyObj<Logger>;
98+
99+
function addListeners(numListenersToAdd: number) {
100+
for(let i = 0; i < numListenersToAdd; i++) {
101+
bus.on('bar', () => true);
102+
}
103+
}
104+
105+
beforeEach(() => {
106+
logger = jasmine.createSpyObj('logger', ['info', 'warn', 'error']);
107+
});
108+
109+
it('logs if the number of listeners exceeds the thresholds', () => {
110+
bus = new Strongbus.Bus<TestEventMap>({
111+
logger,
112+
thresholds: {
113+
info: 10,
114+
warn: 20,
115+
error: 30
116+
}
117+
});
118+
// no logging up to the threshold
119+
addListeners(10);
120+
expect(logger.info).not.toHaveBeenCalled();
121+
expect(logger.warn).not.toHaveBeenCalled();
122+
expect(logger.error).not.toHaveBeenCalled();
123+
124+
// 11th listener triggers info
125+
addListeners(1);
126+
expect(bus.listeners.get('bar').size).toEqual(11);
127+
expect(logger.info).toHaveBeenCalledTimes(1);
128+
expect(logger.warn).not.toHaveBeenCalled();
129+
expect(logger.error).not.toHaveBeenCalled();
130+
131+
// 21st listener triggers warn
132+
addListeners(10);
133+
expect(logger.info).toHaveBeenCalledTimes(10);
134+
expect(logger.warn).toHaveBeenCalledTimes(1);
135+
expect(logger.error).not.toHaveBeenCalled();
136+
137+
// 31st listener triggers error
138+
addListeners(10);
139+
expect(logger.info).toHaveBeenCalledTimes(10);
140+
expect(logger.warn).toHaveBeenCalledTimes(10);
141+
expect(logger.error).toHaveBeenCalledTimes(1);
142+
});
143+
144+
it('logs at the highest severity that passes the threshold', () => {
145+
bus = new Strongbus.Bus<TestEventMap>({
146+
logger,
147+
thresholds: {
148+
// only warn-level specified, info is at default of 100
149+
warn: 20
150+
}
151+
});
152+
153+
addListeners(21);
154+
expect(logger.info).not.toHaveBeenCalled();
155+
expect(logger.warn).toHaveBeenCalledTimes(1);
156+
expect(logger.error).not.toHaveBeenCalled();
157+
});
158+
});
159+
95160
describe('#on', () => {
96161
it('subscribes handler to an event as a key of its typemap', () => {
97162
const handleFoo = jasmine.createSpy('handleFoo') as (fooPayload: string) => void;

0 commit comments

Comments
 (0)