Skip to content

Commit 2ca2027

Browse files
authored
chore(maintenance): simplify Utility class (#3682)
1 parent 70c8245 commit 2ca2027

File tree

5 files changed

+46
-180
lines changed

5 files changed

+46
-180
lines changed

Diff for: packages/commons/src/Utility.ts

+2-36
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,14 @@
5252
*/
5353
export class Utility {
5454
protected coldStart = true;
55-
private readonly defaultServiceName: string = 'service_undefined';
55+
protected readonly defaultServiceName: string = 'service_undefined';
5656

5757
/**
5858
* Get the cold start status of the current execution environment.
5959
*
60-
* @example
61-
* ```typescript
62-
* import { Utility } from '@aws-lambda-powertools/commons';
63-
*
64-
* const utility = new Utility();
65-
* utility.isColdStart(); // true
66-
* utility.isColdStart(); // false
67-
* ```
68-
*
6960
* The method also flips the cold start status to `false` after the first invocation.
7061
*/
71-
public getColdStart(): boolean {
62+
protected getColdStart(): boolean {
7263
if (this.coldStart) {
7364
this.coldStart = false;
7465

@@ -78,31 +69,6 @@ export class Utility {
7869
return false;
7970
}
8071

81-
/**
82-
* Get the cold start status of the current execution environment.
83-
*
84-
* @example
85-
* ```typescript
86-
* import { Utility } from '@aws-lambda-powertools/commons';
87-
*
88-
* const utility = new Utility();
89-
* utility.isColdStart(); // true
90-
* utility.isColdStart(); // false
91-
* ```
92-
*
93-
* @see {@link getColdStart}
94-
*/
95-
public isColdStart(): boolean {
96-
return this.getColdStart();
97-
}
98-
99-
/**
100-
* Get the default service name.
101-
*/
102-
protected getDefaultServiceName(): string {
103-
return this.defaultServiceName;
104-
}
105-
10672
/**
10773
* Validate that the service name provided is valid.
10874
* Used internally during initialization.

Diff for: packages/commons/tests/unit/Utility.test.ts

+40-140
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,57 @@
1-
import { beforeEach, describe, expect, it, vi } from 'vitest';
1+
import { describe, expect, it } from 'vitest';
22
import { Utility } from '../../src/index.js';
33

44
describe('Class: Utility', () => {
5-
beforeEach(() => {
6-
vi.clearAllMocks();
7-
vi.resetModules();
8-
});
5+
class TestUtility extends Utility {
6+
public dummyMethod(): boolean {
7+
return this.getColdStart();
8+
}
9+
public isColdStart(): boolean {
10+
return this.coldStart;
11+
}
12+
public getServiceName(): string {
13+
return this.defaultServiceName;
14+
}
15+
public validateServiceName(serviceName: string): boolean {
16+
return this.isValidServiceName(serviceName);
17+
}
18+
}
919

10-
describe('Method: getDefaultServiceName', () => {
11-
it('returns the default service name', () => {
12-
class PowerTool extends Utility {
13-
public dummyMethod(): string {
14-
return this.getDefaultServiceName();
15-
}
16-
}
20+
it('returns the correct cold start value', () => {
21+
// Prepare
22+
const utility = new TestUtility();
1723

18-
const powertool = new PowerTool();
19-
const result = powertool.dummyMethod();
20-
expect(result).toBe('service_undefined');
21-
});
24+
// Act & Assess
25+
expect(utility.dummyMethod()).toBe(true);
26+
expect(utility.dummyMethod()).toBe(false);
27+
expect(utility.dummyMethod()).toBe(false);
2228
});
2329

24-
describe('Method: getColdStart', () => {
25-
it('it returns true the first time, then false afterwards, when called multiple times', () => {
26-
// Prepare
27-
const utility = new Utility();
28-
const getColdStartSpy = vi.spyOn(utility, 'getColdStart');
29-
30-
// Act
31-
utility.getColdStart();
32-
utility.getColdStart();
33-
utility.getColdStart();
34-
utility.getColdStart();
35-
utility.getColdStart();
36-
37-
// Assess
38-
expect(getColdStartSpy).toHaveBeenCalledTimes(5);
39-
expect(getColdStartSpy.mock.results).toEqual([
40-
expect.objectContaining({ value: true }),
41-
expect.objectContaining({ value: false }),
42-
expect.objectContaining({ value: false }),
43-
expect.objectContaining({ value: false }),
44-
expect.objectContaining({ value: false }),
45-
]);
46-
});
47-
48-
it('returns the correct values when subclassed', () => {
49-
// Prepare
50-
class PowerTool extends Utility {
51-
public dummyMethod(): boolean {
52-
return this.getColdStart();
53-
}
54-
}
55-
const powertool = new PowerTool();
56-
const dummyMethodSpy = vi.spyOn(powertool, 'dummyMethod');
57-
const getColdStartSpy = vi.spyOn(powertool, 'getColdStart');
30+
it('flips the cold start value', () => {
31+
// Prepare
32+
const utility = new TestUtility();
5833

59-
// Act
60-
powertool.dummyMethod();
61-
powertool.dummyMethod();
62-
powertool.dummyMethod();
63-
powertool.dummyMethod();
64-
powertool.dummyMethod();
34+
// Act
35+
utility.dummyMethod();
6536

66-
// Assess
67-
expect(dummyMethodSpy).toHaveBeenCalledTimes(5);
68-
expect(getColdStartSpy).toHaveBeenCalledTimes(5);
69-
expect(dummyMethodSpy.mock.results).toEqual([
70-
expect.objectContaining({ value: true }),
71-
expect.objectContaining({ value: false }),
72-
expect.objectContaining({ value: false }),
73-
expect.objectContaining({ value: false }),
74-
expect.objectContaining({ value: false }),
75-
]);
76-
});
37+
// Assess
38+
expect(utility.isColdStart()).toBe(false);
7739
});
7840

79-
describe('Method: isColdStart', () => {
80-
it('returns true the first time, then false afterwards when called multiple times', () => {
81-
// Prepare
82-
const utility = new Utility();
83-
const isColdStartSpy = vi.spyOn(utility, 'isColdStart');
41+
it('returns the correct default service name', () => {
42+
// Prepare
43+
const utility = new TestUtility();
8444

85-
// Act
86-
utility.isColdStart();
87-
utility.isColdStart();
88-
utility.isColdStart();
89-
utility.isColdStart();
90-
utility.isColdStart();
91-
92-
// Assess
93-
expect(isColdStartSpy).toHaveBeenCalledTimes(5);
94-
expect(isColdStartSpy.mock.results).toEqual([
95-
expect.objectContaining({ value: true }),
96-
expect.objectContaining({ value: false }),
97-
expect.objectContaining({ value: false }),
98-
expect.objectContaining({ value: false }),
99-
expect.objectContaining({ value: false }),
100-
]);
101-
});
102-
103-
it('returns the correct values when subclassed', () => {
104-
// Prepare
105-
class PowerTool extends Utility {
106-
public dummyMethod(): boolean {
107-
return this.isColdStart();
108-
}
109-
}
110-
const powertool = new PowerTool();
111-
const dummyMethodSpy = vi.spyOn(powertool, 'dummyMethod');
112-
const isColdStartSpy = vi.spyOn(powertool, 'isColdStart');
113-
114-
// Act
115-
powertool.dummyMethod();
116-
powertool.dummyMethod();
117-
powertool.dummyMethod();
118-
powertool.dummyMethod();
119-
powertool.dummyMethod();
120-
121-
// Assess
122-
expect(dummyMethodSpy).toHaveBeenCalledTimes(5);
123-
expect(isColdStartSpy).toHaveBeenCalledTimes(5);
124-
expect(dummyMethodSpy.mock.results).toEqual([
125-
expect.objectContaining({ value: true }),
126-
expect.objectContaining({ value: false }),
127-
expect.objectContaining({ value: false }),
128-
expect.objectContaining({ value: false }),
129-
expect.objectContaining({ value: false }),
130-
]);
131-
});
45+
// Assess
46+
expect(utility.getServiceName()).toBe('service_undefined');
13247
});
13348

134-
describe('Method: isValidServiceName', () => {
135-
class PowerTool extends Utility {
136-
public dummyMethod(name: string): boolean {
137-
return this.isValidServiceName(name);
138-
}
139-
}
140-
it('allows valid strings', () => {
141-
const powertool = new PowerTool();
142-
const goodName = 'serverlessAirline';
143-
144-
const result = powertool.dummyMethod(goodName);
145-
146-
expect(result).toBe(true);
147-
});
148-
149-
it("doesn't allow empty strings", () => {
150-
const tooShort = '';
151-
const powertool = new PowerTool();
152-
const result = powertool.dummyMethod(tooShort);
49+
it('validates service name', () => {
50+
// Prepare
51+
const utility = new TestUtility();
15352

154-
expect(result).toBe(false);
155-
});
53+
// Act & Assess
54+
expect(utility.validateServiceName('serverlessAirline')).toBe(true);
55+
expect(utility.validateServiceName('')).toBe(false);
15656
});
15757
});

Diff for: packages/logger/src/Logger.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ class Logger extends Utility implements LoggerInterface {
12591259
serviceName ||
12601260
this.getCustomConfigService()?.getServiceName() ||
12611261
this.getEnvVarsService().getServiceName() ||
1262-
this.getDefaultServiceName(),
1262+
this.defaultServiceName,
12631263
});
12641264
persistentKeys && this.appendPersistentKeys(persistentKeys);
12651265
}

Diff for: packages/metrics/src/Metrics.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ class Metrics extends Utility implements MetricsInterface {
373373
* ```
374374
*/
375375
public captureColdStartMetric(): void {
376-
if (!this.isColdStart()) return;
376+
if (!this.getColdStart()) return;
377377
const singleMetric = this.singleMetric();
378378

379379
if (this.defaultDimensions.service) {
@@ -975,7 +975,7 @@ class Metrics extends Utility implements MetricsInterface {
975975
((service ||
976976
this.getCustomConfigService()?.getServiceName() ||
977977
this.getEnvVarsService().getServiceName()) as string) ||
978-
this.getDefaultServiceName();
978+
this.defaultServiceName;
979979
if (targetService.length > 0) {
980980
this.setDefaultDimensions({ service: targetService });
981981
}

Diff for: packages/tracer/src/Tracer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ class Tracer extends Utility implements TracerInterface {
914914

915915
return;
916916
}
917-
this.serviceName = this.getDefaultServiceName();
917+
this.serviceName = this.defaultServiceName;
918918
}
919919

920920
/**

0 commit comments

Comments
 (0)