Skip to content

Commit

Permalink
test: map reference breaking
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jun 7, 2024
1 parent 472ae80 commit 6572e57
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/lifecycleEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import { Logger } from './logger/logger';

// Data of any type can be passed to the callback. Can be cast to any type that is given in emit().
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type callback = (data: any) => Promise<void>;
export type callback = (data: any) => Promise<void>;
type ListenerMap = Map<string, callback>;
type UniqueListenerMap = Map<string, ListenerMap>;
export type UniqueListenerMap = Map<string, ListenerMap>;

declare const global: {
salesforceCoreLifecycle?: Lifecycle;
Expand Down Expand Up @@ -51,7 +51,7 @@ export class Lifecycle {

private constructor(
private readonly listeners: Dictionary<callback[]> = {},
private readonly uniqueListeners: Map<string, Map<string, callback>> = new Map<string, Map<string, callback>>()
private readonly uniqueListeners: UniqueListenerMap = new Map<string, Map<string, callback>>()
) {}

/**
Expand Down Expand Up @@ -238,5 +238,5 @@ export class Lifecycle {
}

const cloneListeners: (listeners: ListenerMap) => ListenerMap = (listeners) => new Map(Array.from(listeners.entries()));
const cloneUniqueListeners = (uniqueListeners: UniqueListenerMap): UniqueListenerMap =>
export const cloneUniqueListeners = (uniqueListeners: UniqueListenerMap): UniqueListenerMap =>
new Map(Array.from(uniqueListeners.entries()).map(([key, value]) => [key, cloneListeners(value)]));
28 changes: 27 additions & 1 deletion test/unit/lifecycleEventsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { Duration, sleep } from '@salesforce/kit/lib/duration';
import { spyMethod } from '@salesforce/ts-sinon';
import * as chai from 'chai';
import { Lifecycle } from '../../src/lifecycleEvents';
import { Lifecycle, callback, cloneUniqueListeners } from '../../src/lifecycleEvents';
import { TestContext } from '../../src/testSetup';
import { Logger } from '../../src/logger/logger';

Expand Down Expand Up @@ -257,3 +257,29 @@ describe('lifecycleEvents', () => {
lifecycle2.removeAllListeners('test7');
});
});

describe('listener map cloning', () => {
const cb = (): Promise<void> => Promise.resolve();
it('clones map, breaking event name reference', () => {
const map1 = new Map<string, Map<string, callback>>();
map1.set('evt', new Map([['uniqueId', cb]]));

const map2 = cloneUniqueListeners(map1);
chai.expect(map2).to.deep.equal(map1);
map1.delete('evt');
chai.expect(map1.has('evt')).to.be.false;
chai.expect(map2.has('evt')).to.be.true;
});
it('clones map, breaking uniqueId reference', () => {
const map1 = new Map<string, Map<string, callback>>();
map1.set('evt', new Map([['uniqueId', cb]]));

const map2 = cloneUniqueListeners(map1);
chai.expect(map2).to.deep.equal(map1);
map2.get('evt')?.set('uniqueId2', cb);
chai.expect(map1.has('evt')).to.be.true;
chai.expect(map2.has('evt')).to.be.true;
chai.expect(map1.get('evt')?.has('uniqueId2')).to.be.false;
chai.expect(map2.get('evt')?.has('uniqueId2')).to.be.true;
});
});

2 comments on commit 6572e57

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - ubuntu-latest

Benchmark suite Current: 6572e57 Previous: 472ae80 Ratio
Child logger creation 477661 ops/sec (±1.96%) 481148 ops/sec (±0.95%) 1.01
Logging a string on root logger 766683 ops/sec (±8.25%) 829596 ops/sec (±6.24%) 1.08
Logging an object on root logger 625753 ops/sec (±6.14%) 588018 ops/sec (±7.41%) 0.94
Logging an object with a message on root logger 5388 ops/sec (±213.19%) 6081 ops/sec (±211.48%) 1.13
Logging an object with a redacted prop on root logger 403423 ops/sec (±12.18%) 427055 ops/sec (±11.28%) 1.06
Logging a nested 3-level object on root logger 386832 ops/sec (±5.30%) 362855 ops/sec (±8.03%) 0.94

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - windows-latest

Benchmark suite Current: 6572e57 Previous: 472ae80 Ratio
Child logger creation 324970 ops/sec (±1.75%) 329736 ops/sec (±0.72%) 1.01
Logging a string on root logger 746579 ops/sec (±5.89%) 802178 ops/sec (±4.92%) 1.07
Logging an object on root logger 584705 ops/sec (±6.29%) 578979 ops/sec (±5.94%) 0.99
Logging an object with a message on root logger 7177 ops/sec (±201.24%) 5015 ops/sec (±207.58%) 0.70
Logging an object with a redacted prop on root logger 425337 ops/sec (±13.95%) 454361 ops/sec (±15.05%) 1.07
Logging a nested 3-level object on root logger 307176 ops/sec (±8.78%) 317849 ops/sec (±5.54%) 1.03

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.