Skip to content

Commit eca6e35

Browse files
committed
refactor: wip
1 parent 6ec0450 commit eca6e35

5 files changed

Lines changed: 42 additions & 100 deletions

File tree

packages/utils/src/lib/exit-process.int.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import process from 'node:process';
22
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
3-
import { SIGNAL_EXIT_CODES, installExitHandlers } from './exit-process.js';
3+
import { SIGNAL_EXIT_CODES, subscribeProcessExit } from './exit-process.js';
44

5-
describe('installExitHandlers', () => {
5+
describe('subscribeProcessExit', () => {
66
const onError = vi.fn();
77
const onExit = vi.fn();
88
const processOnSpy = vi.spyOn(process, 'on');
@@ -26,7 +26,7 @@ describe('installExitHandlers', () => {
2626
});
2727

2828
it('should install event listeners for all expected events', () => {
29-
expect(() => installExitHandlers({ onError, onExit })).not.toThrow();
29+
expect(() => subscribeProcessExit({ onError, onExit })).not.toThrow();
3030

3131
expect(processOnSpy).toHaveBeenCalledWith(
3232
'uncaughtException',
@@ -43,7 +43,7 @@ describe('installExitHandlers', () => {
4343
});
4444

4545
it('should call onError with error and kind for uncaughtException', () => {
46-
expect(() => installExitHandlers({ onError })).not.toThrow();
46+
expect(() => subscribeProcessExit({ onError })).not.toThrow();
4747

4848
const testError = new Error('Test uncaught exception');
4949

@@ -55,7 +55,7 @@ describe('installExitHandlers', () => {
5555
});
5656

5757
it('should call onError with reason and kind for unhandledRejection', () => {
58-
expect(() => installExitHandlers({ onError })).not.toThrow();
58+
expect(() => subscribeProcessExit({ onError })).not.toThrow();
5959

6060
const testReason = 'Test unhandled rejection';
6161

@@ -67,7 +67,7 @@ describe('installExitHandlers', () => {
6767
});
6868

6969
it('should call onExit and exit with code 0 for SIGINT', () => {
70-
expect(() => installExitHandlers({ onExit })).not.toThrow();
70+
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
7171

7272
(process as any).emit('SIGINT');
7373

@@ -80,7 +80,7 @@ describe('installExitHandlers', () => {
8080
});
8181

8282
it('should call onExit and exit with code 0 for SIGTERM', () => {
83-
expect(() => installExitHandlers({ onExit })).not.toThrow();
83+
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
8484

8585
(process as any).emit('SIGTERM');
8686

@@ -93,7 +93,7 @@ describe('installExitHandlers', () => {
9393
});
9494

9595
it('should call onExit and exit with code 0 for SIGQUIT', () => {
96-
expect(() => installExitHandlers({ onExit })).not.toThrow();
96+
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
9797

9898
(process as any).emit('SIGQUIT');
9999

@@ -106,7 +106,7 @@ describe('installExitHandlers', () => {
106106
});
107107

108108
it('should call onExit for successful process termination with exit code 0', () => {
109-
expect(() => installExitHandlers({ onExit })).not.toThrow();
109+
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
110110

111111
(process as any).emit('exit', 0);
112112

@@ -117,7 +117,7 @@ describe('installExitHandlers', () => {
117117
});
118118

119119
it('should call onExit for failed process termination with exit code 1', () => {
120-
expect(() => installExitHandlers({ onExit })).not.toThrow();
120+
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
121121

122122
(process as any).emit('exit', 1);
123123

packages/utils/src/lib/exit-process.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export function subscribeProcessExit(
9494
const handler = () => {
9595
close(SIGNAL_EXIT_CODES()[signal], { kind: 'signal', signal });
9696
if (exitOnSignal) {
97-
// eslint-disable-next-line n/no-process-exit
97+
// eslint-disable-next-line unicorn/no-process-exit
9898
process.exit(SIGNAL_EXIT_CODES()[signal]);
9999
}
100100
};

packages/utils/src/lib/exit-process.unit.test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import os from 'node:os';
22
import process from 'node:process';
33
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
4-
import { SIGNAL_EXIT_CODES, installExitHandlers } from './exit-process.js';
4+
import { SIGNAL_EXIT_CODES, subscribeProcessExit } from './exit-process.js';
55

6-
describe('exit-process tests', () => {
6+
describe('subscribeProcessExit', () => {
77
const onError = vi.fn();
88
const onExit = vi.fn();
99
const processOnSpy = vi.spyOn(process, 'on');
@@ -27,7 +27,7 @@ describe('exit-process tests', () => {
2727
});
2828

2929
it('should install event listeners for all expected events', () => {
30-
expect(() => installExitHandlers({ onError, onExit })).not.toThrow();
30+
expect(() => subscribeProcessExit({ onError, onExit })).not.toThrow();
3131

3232
expect(processOnSpy).toHaveBeenCalledWith(
3333
'uncaughtException',
@@ -44,7 +44,7 @@ describe('exit-process tests', () => {
4444
});
4545

4646
it('should call onError with error and kind for uncaughtException', () => {
47-
expect(() => installExitHandlers({ onError })).not.toThrow();
47+
expect(() => subscribeProcessExit({ onError })).not.toThrow();
4848

4949
const testError = new Error('Test uncaught exception');
5050

@@ -56,7 +56,7 @@ describe('exit-process tests', () => {
5656
});
5757

5858
it('should call onError with reason and kind for unhandledRejection', () => {
59-
expect(() => installExitHandlers({ onError })).not.toThrow();
59+
expect(() => subscribeProcessExit({ onError })).not.toThrow();
6060

6161
const testReason = 'Test unhandled rejection';
6262

@@ -69,7 +69,7 @@ describe('exit-process tests', () => {
6969

7070
it('should call onExit with correct code and reason for SIGINT', () => {
7171
expect(() =>
72-
installExitHandlers({ onExit, exitOnSignal: true }),
72+
subscribeProcessExit({ onExit, exitOnSignal: true }),
7373
).not.toThrow();
7474

7575
(process as any).emit('SIGINT');
@@ -85,7 +85,7 @@ describe('exit-process tests', () => {
8585

8686
it('should call onExit with correct code and reason for SIGTERM', () => {
8787
expect(() =>
88-
installExitHandlers({ onExit, exitOnSignal: true }),
88+
subscribeProcessExit({ onExit, exitOnSignal: true }),
8989
).not.toThrow();
9090

9191
(process as any).emit('SIGTERM');
@@ -101,7 +101,7 @@ describe('exit-process tests', () => {
101101

102102
it('should call onExit with correct code and reason for SIGQUIT', () => {
103103
expect(() =>
104-
installExitHandlers({ onExit, exitOnSignal: true }),
104+
subscribeProcessExit({ onExit, exitOnSignal: true }),
105105
).not.toThrow();
106106

107107
(process as any).emit('SIGQUIT');
@@ -117,7 +117,7 @@ describe('exit-process tests', () => {
117117

118118
it('should not exit process when exitOnSignal is false', () => {
119119
expect(() =>
120-
installExitHandlers({ onExit, exitOnSignal: false }),
120+
subscribeProcessExit({ onExit, exitOnSignal: false }),
121121
).not.toThrow();
122122

123123
(process as any).emit('SIGINT');
@@ -132,7 +132,7 @@ describe('exit-process tests', () => {
132132
});
133133

134134
it('should not exit process when exitOnSignal is not set', () => {
135-
expect(() => installExitHandlers({ onExit })).not.toThrow();
135+
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
136136

137137
(process as any).emit('SIGTERM');
138138

@@ -146,7 +146,7 @@ describe('exit-process tests', () => {
146146
});
147147

148148
it('should call onExit with exit code and reason for normal exit', () => {
149-
expect(() => installExitHandlers({ onExit })).not.toThrow();
149+
expect(() => subscribeProcessExit({ onExit })).not.toThrow();
150150

151151
const exitCode = 42;
152152
(process as any).emit('exit', exitCode);
@@ -159,7 +159,7 @@ describe('exit-process tests', () => {
159159

160160
it('should call onExit with fatal reason when exitOnFatal is true', () => {
161161
expect(() =>
162-
installExitHandlers({ onError, onExit, exitOnFatal: true }),
162+
subscribeProcessExit({ onError, onExit, exitOnFatal: true }),
163163
).not.toThrow();
164164

165165
const testError = new Error('Test uncaught exception');
@@ -177,7 +177,7 @@ describe('exit-process tests', () => {
177177

178178
it('should use custom fatalExitCode when exitOnFatal is true', () => {
179179
expect(() =>
180-
installExitHandlers({
180+
subscribeProcessExit({
181181
onError,
182182
onExit,
183183
exitOnFatal: true,
@@ -200,7 +200,7 @@ describe('exit-process tests', () => {
200200

201201
it('should call onExit with fatal reason for unhandledRejection when exitOnFatal is true', () => {
202202
expect(() =>
203-
installExitHandlers({ onError, onExit, exitOnFatal: true }),
203+
subscribeProcessExit({ onError, onExit, exitOnFatal: true }),
204204
).not.toThrow();
205205

206206
const testReason = 'Test unhandled rejection';
@@ -244,7 +244,7 @@ describe('exit-process tests', () => {
244244

245245
it('should call onExit only once even when close is called multiple times', () => {
246246
expect(() =>
247-
installExitHandlers({ onExit, exitOnSignal: true }),
247+
subscribeProcessExit({ onExit, exitOnSignal: true }),
248248
).not.toThrow();
249249

250250
(process as any).emit('SIGINT');

packages/utils/src/lib/profiler/profiler.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import process from 'node:process';
22
import { isEnvVarEnabled } from '../env.js';
3-
import { subscribeProcessExit } from '../exit-process';
3+
import { subscribeProcessExit } from '../exit-process.js';
44
import type { TraceEvent } from '../trace-file.type';
55
import {
66
type ActionTrackConfigs,
@@ -232,17 +232,17 @@ export class Profiler<T extends ActionTrackConfigs> {
232232

233233
// @TODO implement ShardedWAL
234234
type WalSink = {
235-
append(event: TraceEvent): void;
236-
open(): void;
237-
close(): void;
238-
isClosed(): boolean;
235+
append: (event: TraceEvent) => void;
236+
open: () => void;
237+
close: () => void;
238+
isClosed: () => boolean;
239239
};
240240

241241
export type NodeJsProfilerOptions<T extends ActionTrackConfigs> =
242242
ProfilerOptions<T> & {
243243
// @TODO implement WALFormat
244244
format: {
245-
encode(v: string | object): string;
245+
encode: (v: string | object) => string;
246246
};
247247
};
248248

@@ -275,7 +275,7 @@ export class NodeJsProfiler<T extends ActionTrackConfigs> extends Profiler<T> {
275275
protected subscribeProcessExit(): () => void {
276276
return subscribeProcessExit({
277277
onError: (err, kind) => {
278-
if (!this.isRunning()) {
278+
if (!super.isEnabled()) {
279279
return;
280280
}
281281
this.marker('Fatal Error', {
@@ -285,11 +285,11 @@ export class NodeJsProfiler<T extends ActionTrackConfigs> extends Profiler<T> {
285285
this.close();
286286
},
287287
onExit: (code, reason) => {
288-
if (!this.isRunning()) {
288+
if (!super.isEnabled()) {
289289
return;
290290
}
291291
this.marker('Process Exit', {
292-
...(code !== 0 ? { color: 'warning' } : {}),
292+
...(code === 0 ? {} : { color: 'warning' }),
293293
properties: [['reason', JSON.stringify(reason)]],
294294
tooltipText: `Process exited with code ${code}`,
295295
});
@@ -298,15 +298,6 @@ export class NodeJsProfiler<T extends ActionTrackConfigs> extends Profiler<T> {
298298
});
299299
}
300300

301-
isRunning(): boolean {
302-
return this.isEnabled() && !this.sink?.isClosed();
303-
}
304-
305-
override setEnabled(enabled: boolean): void {
306-
super.setEnabled(enabled);
307-
enabled ? this.sink?.open() : this.sink?.close();
308-
}
309-
310301
/**
311302
* Closes the profiler and releases all associated resources.
312303
* Profiling is finished forever for this instance.
@@ -315,16 +306,10 @@ export class NodeJsProfiler<T extends ActionTrackConfigs> extends Profiler<T> {
315306
* data is flushed and the WAL sink is properly closed.
316307
*/
317308
close(): void {
318-
if (!this.isEnabled()) return;
319-
this.flush();
309+
if (!this.isEnabled()) {
310+
return;
311+
}
320312
this.setEnabled(false);
321313
this.#exitHandlerSubscribscription?.();
322314
}
323-
324-
/**
325-
* Forces all buffered Performance Entries to be written to the WAL sink.
326-
*/
327-
flush(): void {
328-
// @TODO implement WAL flush, currently all entries are buffered in memory
329-
}
330315
}

packages/utils/src/lib/profiler/profiler.unit.test.ts

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ describe('Profiler', () => {
428428
expect(workFn).toHaveBeenCalled();
429429
});
430430
});
431+
431432
describe('NodeJsProfiler', () => {
432433
const mockSubscribeProcessExit = vi.mocked(subscribeProcessExit);
433434

@@ -545,60 +546,16 @@ describe('NodeJsProfiler', () => {
545546
},
546547
]);
547548
});
548-
it('shutdown method shuts down profiler', () => {
549-
profiler = createProfiler({ enabled: true });
550-
const setEnabledSpy = vi.spyOn(profiler, 'setEnabled');
551-
const sinkCloseSpy = vi.spyOn((profiler as any).sink, 'close');
552-
expect(profiler.isEnabled()).toBe(true);
553549

554-
(profiler as any).shutdown();
555-
556-
expect(setEnabledSpy).toHaveBeenCalledTimes(1);
557-
expect(setEnabledSpy).toHaveBeenCalledWith(false);
558-
expect(sinkCloseSpy).toHaveBeenCalledTimes(1);
559-
expect(profiler.isEnabled()).toBe(false);
560-
});
561550
it('exit handler shuts down profiler', () => {
562551
profiler = createProfiler({ enabled: true });
563-
const shutdownSpy = vi.spyOn(profiler, 'shutdown' as any);
552+
const setEnabledSpy = vi.spyOn(profiler, 'setEnabled');
564553
expect(profiler.isEnabled()).toBe(true);
565554

566555
capturedOnExit?.(0, { kind: 'exit' });
567556

568557
expect(profiler.isEnabled()).toBe(false);
569-
expect(shutdownSpy).toHaveBeenCalledTimes(1);
570-
});
571-
572-
it('close method shuts down profiler', () => {
573-
profiler = createProfiler({ enabled: true });
574-
const shutdownSpy = vi.spyOn(profiler, 'shutdown' as any);
575-
expect(profiler.isEnabled()).toBe(true);
576-
577-
profiler.close();
578-
579-
expect(shutdownSpy).toHaveBeenCalledTimes(1);
580-
expect(profiler.isEnabled()).toBe(false);
581-
});
582-
583-
it('error handler does nothing when profiler is disabled', () => {
584-
profiler = createProfiler({ enabled: false }); // Start disabled
585-
expect(profiler.isEnabled()).toBe(false);
586-
587-
const testError = new Error('Test error');
588-
capturedOnError?.call(profiler, testError, 'uncaughtException');
589-
590-
// Should not create any marks when disabled
591-
expect(performance.getEntriesByType('mark')).toHaveLength(0);
592-
});
593-
594-
it('exit handler does nothing when profiler is disabled', () => {
595-
profiler = createProfiler({ enabled: false }); // Start disabled
596-
expect(profiler.isEnabled()).toBe(false);
597-
598-
// Should not call shutdown when disabled
599-
const shutdownSpy = vi.spyOn(profiler, 'shutdown' as any);
600-
capturedOnExit?.(0, { kind: 'exit' });
601-
602-
expect(shutdownSpy).not.toHaveBeenCalled();
558+
expect(setEnabledSpy).toHaveBeenCalledTimes(1);
559+
expect(setEnabledSpy).toHaveBeenCalledWith(false);
603560
});
604561
});

0 commit comments

Comments
 (0)