Skip to content

Commit

Permalink
fix(ignore): cleanup logger, add waitress clear util (#1307)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerivec authored Jan 23, 2025
1 parent 6b67be8 commit dcf2592
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/adapter/zboss/adapter/zbossAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class ZBOSSAdapter extends Adapter {
}

public async addInstallCode(ieeeAddress: string, key: Buffer): Promise<void> {
logger.error(() => `NOT SUPPORTED: sendZclFrameToGroup(${ieeeAddress},${key.toString('hex')}`, NS);
logger.error(`NOT SUPPORTED: sendZclFrameToGroup(${ieeeAddress},${key.toString('hex')}`, NS);
throw new Error(`Install code is not supported for 'zboss' yet`);
}

Expand Down Expand Up @@ -431,12 +431,12 @@ export class ZBOSSAdapter extends Adapter {
}

public async sendZclFrameInterPANToIeeeAddr(zclFrame: Zcl.Frame, ieeeAddress: string): Promise<void> {
logger.error(() => `NOT SUPPORTED: sendZclFrameInterPANToIeeeAddr(${JSON.stringify(zclFrame)},${ieeeAddress})`, NS);
logger.error(`NOT SUPPORTED: sendZclFrameInterPANToIeeeAddr(${JSON.stringify(zclFrame)},${ieeeAddress})`, NS);
return;
}

public async sendZclFrameInterPANBroadcast(zclFrame: Zcl.Frame, timeout: number): Promise<ZclPayload> {
logger.error(() => `NOT SUPPORTED: sendZclFrameInterPANBroadcast(${JSON.stringify(zclFrame)},${timeout})`, NS);
logger.error(`NOT SUPPORTED: sendZclFrameInterPANBroadcast(${JSON.stringify(zclFrame)},${timeout})`, NS);
throw new Error(`Is not supported for 'zboss' yet`);
}

Expand Down
12 changes: 6 additions & 6 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ export interface Logger {
debug: (messageOrLambda: string | (() => string), namespace: string) => void;
info: (messageOrLambda: string | (() => string), namespace: string) => void;
warning: (messageOrLambda: string | (() => string), namespace: string) => void;
error: (messageOrLambda: string | (() => string), namespace: string) => void;
error: (messageOrLambda: string, namespace: string) => void;
}

export let logger: Logger = {
debug: (messageOrLambda, namespace) =>
console.debug(`${namespace}: ${typeof messageOrLambda === 'function' ? messageOrLambda() : messageOrLambda}`),
info: (messageOrLambda, namespace) => console.info(`${namespace}: ${typeof messageOrLambda === 'string' ? messageOrLambda : messageOrLambda()}`),
console.debug(`[${new Date().toISOString()}] ${namespace}: ${typeof messageOrLambda === 'function' ? messageOrLambda() : messageOrLambda}`),
info: (messageOrLambda, namespace) =>
console.info(`[${new Date().toISOString()}] ${namespace}: ${typeof messageOrLambda === 'function' ? messageOrLambda() : messageOrLambda}`),
warning: (messageOrLambda, namespace) =>
console.warn(`${namespace}: ${typeof messageOrLambda === 'function' ? messageOrLambda() : messageOrLambda}`),
error: (messageOrLambda, namespace) =>
console.error(`${namespace}: ${typeof messageOrLambda === 'function' ? messageOrLambda() : messageOrLambda}`),
console.warn(`[${new Date().toISOString()}] ${namespace}: ${typeof messageOrLambda === 'function' ? messageOrLambda() : messageOrLambda}`),
error: (message, namespace) => console.error(`[${new Date().toISOString()}] ${namespace}: ${message}`),
};

export function setLogger(l: Logger): void {
Expand Down
8 changes: 8 additions & 0 deletions src/utils/waitress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export class Waitress<TPayload, TMatcher> {
this.currentID = 0;
}

public clear(): void {
for (const [, waiter] of this.waiters) {
clearTimeout(waiter.timer);
}

this.waiters.clear();
}

public resolve(payload: TPayload): boolean {
return this.forEachMatching(payload, (waiter) => waiter.resolve(payload));
}
Expand Down
19 changes: 15 additions & 4 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ describe('Utils', () => {
expect(error2_).toStrictEqual(new Error("Timedout '5000'"));
let handled2 = waitress.reject('two', 'drop');
expect(handled2).toBe(false);

const waitClear_1 = waitress.waitFor(2, 10000).start().promise;
const waitClear_2 = waitress.waitFor(2, 10000).start().promise;

await vi.advanceTimersByTimeAsync(2000);
waitress.clear();
await vi.advanceTimersByTimeAsync(12000);

// @ts-expect-error private
expect(waitress.waiters.size).toStrictEqual(0);

vi.useRealTimers();
});

Expand Down Expand Up @@ -186,13 +197,13 @@ describe('Utils', () => {
const warningSpy = vi.spyOn(console, 'warn');
const errorSpy = vi.spyOn(console, 'error');
logger.debug('debug', 'zh');
expect(debugSpy).toHaveBeenCalledWith('zh: debug');
expect(debugSpy).toHaveBeenCalledWith(expect.stringMatching(/^\[\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\dZ\] zh: debug$/));
logger.info('info', 'zh');
expect(infoSpy).toHaveBeenCalledWith('zh: info');
expect(infoSpy).toHaveBeenCalledWith(expect.stringMatching(/^\[\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\dZ\] zh: info$/));
logger.warning('warning', 'zh');
expect(warningSpy).toHaveBeenCalledWith('zh: warning');
expect(warningSpy).toHaveBeenCalledWith(expect.stringMatching(/^\[\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\dZ\] zh: warning$/));
logger.error('error', 'zh');
expect(errorSpy).toHaveBeenCalledWith('zh: error');
expect(errorSpy).toHaveBeenCalledWith(expect.stringMatching(/^\[\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\dZ\] zh: error$/));

setLogger(mockLogger);
expect(logger).toEqual(mockLogger);
Expand Down

0 comments on commit dcf2592

Please sign in to comment.