Skip to content

Commit a285935

Browse files
committed
fix: tests were leaking EventEmitter handlers
1 parent d3a3489 commit a285935

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

test/lease-manager.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ describe('LeaseManager', () => {
108108
let LeaseManager: typeof leaseTypes.LeaseManager;
109109
let leaseManager: leaseTypes.LeaseManager;
110110

111+
let fakeLog: FakeLog | undefined;
112+
111113
before(() => {
112114
LeaseManager = proxyquire('../src/lease-manager.js', {
113115
os: fakeos,
@@ -121,6 +123,7 @@ describe('LeaseManager', () => {
121123
});
122124

123125
afterEach(() => {
126+
fakeLog?.remove();
124127
leaseManager.clear();
125128
sandbox.restore();
126129
});
@@ -197,20 +200,20 @@ describe('LeaseManager', () => {
197200
fakeMessage.id = 'a';
198201
fakeMessage.ackId = 'b';
199202

200-
const fakeLog = new FakeLog(leaseTypes.logs.callbackDelivery);
203+
fakeLog = new FakeLog(leaseTypes.logs.callbackDelivery);
201204

202205
leaseManager.setOptions({
203206
allowExcessMessages: true,
204207
});
205208

206209
subscriber.on('message', () => {
207-
assert.strictEqual(fakeLog.called, true);
210+
assert.strictEqual(fakeLog!.called, true);
208211
assert.strictEqual(
209-
fakeLog.fields!.severity,
212+
fakeLog!.fields!.severity,
210213
loggingUtils.LogSeverity.INFO,
211214
);
212-
assert.strictEqual(fakeLog.args![1] as string, 'a');
213-
assert.strictEqual(fakeLog.args![2] as string, 'b');
215+
assert.strictEqual(fakeLog!.args![1] as string, 'a');
216+
assert.strictEqual(fakeLog!.args![2] as string, 'b');
214217
done();
215218
});
216219

@@ -222,7 +225,7 @@ describe('LeaseManager', () => {
222225
fakeMessage.id = 'a';
223226
fakeMessage.ackId = 'b';
224227

225-
const fakeLog = new FakeLog(leaseTypes.logs.callbackExceptions);
228+
fakeLog = new FakeLog(leaseTypes.logs.callbackExceptions);
226229

227230
leaseManager.setOptions({
228231
allowExcessMessages: true,
@@ -285,7 +288,7 @@ describe('LeaseManager', () => {
285288
const pendingStub = sandbox.stub(leaseManager, 'pending');
286289
pendingStub.get(() => 0);
287290
leaseManager.setOptions({allowExcessMessages: false});
288-
const fakeLog = new FakeLog(leaseTypes.logs.subscriberFlowControl);
291+
fakeLog = new FakeLog(leaseTypes.logs.subscriberFlowControl);
289292

290293
leaseManager.add(fakeMessage);
291294
assert.strictEqual(fakeLog.called, true);
@@ -392,7 +395,7 @@ describe('LeaseManager', () => {
392395
const removeStub = sandbox.stub(leaseManager, 'remove');
393396
const modAckStub = sandbox.stub(goodMessage, 'modAck');
394397

395-
const fakeLog = new FakeLog(leaseTypes.logs.expiry);
398+
fakeLog = new FakeLog(leaseTypes.logs.expiry);
396399

397400
leaseManager.add(goodMessage as {} as Message);
398401
clock.tick(halfway);
@@ -474,7 +477,7 @@ describe('LeaseManager', () => {
474477
});
475478

476479
it('should log if it was full and is now empty', () => {
477-
const fakeLog = new FakeLog(leaseTypes.logs.subscriberFlowControl);
480+
fakeLog = new FakeLog(leaseTypes.logs.subscriberFlowControl);
478481
const pendingStub = sandbox.stub(leaseManager, 'pending');
479482
pendingStub.get(() => 0);
480483
leaseManager.add(new FakeMessage() as {} as Message);
@@ -613,7 +616,7 @@ describe('LeaseManager', () => {
613616
const pending = new FakeMessage() as {} as Message;
614617

615618
leaseManager.setOptions({allowExcessMessages: false, maxMessages: 1});
616-
const fakeLog = new FakeLog(leaseTypes.logs.subscriberFlowControl);
619+
fakeLog = new FakeLog(leaseTypes.logs.subscriberFlowControl);
617620

618621
leaseManager.add(temp);
619622
leaseManager.add(pending);

test/message-queues.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ describe('MessageQueues', () => {
415415
messages.forEach(message => ackQueue.add(message as Message));
416416
await ackQueue.flush('logtest');
417417

418+
fakeLog.remove();
419+
418420
assert.strictEqual(fakeLog.called, true);
419421
assert.strictEqual(
420422
fakeLog.fields!.severity,
@@ -674,6 +676,8 @@ describe('MessageQueues', () => {
674676
messages.forEach(message => modAckQueue.add(message as Message));
675677
await modAckQueue.flush('logtest');
676678

679+
fakeLog.remove();
680+
677681
assert.strictEqual(fakeLog.called, true);
678682
assert.strictEqual(
679683
fakeLog.fields!.severity,

test/publisher/message-queues.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ describe('Message Queues', () => {
184184
sandbox.stub(topic, 'request');
185185
const fakeLog = new FakeLog(q.logs.publishBatch);
186186
void queue._publish(messages, callbacks, 0, 'test');
187+
fakeLog.remove();
188+
187189
assert.strictEqual(fakeLog.called, true);
188190
assert.strictEqual(
189191
fakeLog.fields!.severity,

test/subscriber.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ describe('Subscriber', () => {
206206
let Subscriber: typeof s.Subscriber;
207207
let subscriber: s.Subscriber;
208208

209+
let fakeLog: FakeLog | undefined;
210+
209211
beforeEach(() => {
210212
sandbox = sinon.createSandbox();
211213
fakeProjectify = {
@@ -243,6 +245,8 @@ describe('Subscriber', () => {
243245
});
244246

245247
afterEach(async () => {
248+
fakeLog?.remove();
249+
fakeLog = undefined;
246250
sandbox.restore();
247251
await subscriber.close();
248252
tracing.setGloballyEnabled(false);
@@ -441,7 +445,7 @@ describe('Subscriber', () => {
441445
});
442446

443447
it('should log on ack completion', async () => {
444-
const fakeLog = new FakeLog(s.logs.ackNack);
448+
fakeLog = new FakeLog(s.logs.ackNack);
445449

446450
await subscriber.ack(message);
447451

@@ -459,7 +463,7 @@ describe('Subscriber', () => {
459463

460464
message.received = 0;
461465
sandbox.stub(histogram, 'percentile').withArgs(99).returns(10);
462-
const fakeLog = new FakeLog(s.logs.slowAck);
466+
fakeLog = new FakeLog(s.logs.slowAck);
463467

464468
await subscriber.ack(message);
465469

@@ -926,7 +930,7 @@ describe('Subscriber', () => {
926930
});
927931

928932
it('should log on ack completion', async () => {
929-
const fakeLog = new FakeLog(s.logs.ackNack);
933+
fakeLog = new FakeLog(s.logs.ackNack);
930934

931935
await subscriber.nack(message);
932936

test/test-utils.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,23 @@ export class FakeLog {
6363
fields?: loggingUtils.LogFields;
6464
args?: unknown[];
6565
called = false;
66+
log: loggingUtils.AdhocDebugLogFunction;
67+
listener: (lf: loggingUtils.LogFields, a: unknown[]) => void;
6668

6769
constructor(log: loggingUtils.AdhocDebugLogFunction) {
68-
log.on('log', (lf, a) => {
70+
this.log = log;
71+
this.listener = (lf: loggingUtils.LogFields, a: unknown[]) => {
6972
this.fields = lf;
7073
this.args = a;
7174
this.called = true;
72-
});
75+
};
76+
this.log.on('log', this.listener);
77+
}
78+
79+
remove() {
80+
// This really ought to be properly exposed, but since it's not, we'll
81+
// do this for now to keep the tests from being leaky.
82+
const instance = (this.log as loggingUtils.AdhocDebugLogFunction).instance;
83+
instance.off('log', this.listener);
7384
}
7485
}

0 commit comments

Comments
 (0)