From b9ed06b02aa3cee4e78afee86176fdadd607e66b Mon Sep 17 00:00:00 2001 From: Kurt Preston Date: Tue, 15 Dec 2020 18:23:32 -0600 Subject: [PATCH] Log at highest level that has its threshold crossed --- src/strongbus.ts | 10 +++---- src/strongbus_spec.ts | 65 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/src/strongbus.ts b/src/strongbus.ts index c0aef45..a81e1af 100644 --- a/src/strongbus.ts +++ b/src/strongbus.ts @@ -408,13 +408,13 @@ export class Bus implements Scannable|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); diff --git a/src/strongbus_spec.ts b/src/strongbus_spec.ts index 3a2aa60..484ef15 100644 --- a/src/strongbus_spec.ts +++ b/src/strongbus_spec.ts @@ -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 = { @@ -92,6 +93,70 @@ describe('Strongbus.Bus', () => { }); }); + describe('listener logging thresholds', () => { + let logger: jasmine.SpyObj; + + 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({ + 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({ + 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;