Skip to content

Commit 1dcf438

Browse files
authoredMar 17, 2025··
test: fix transient fails in log sampling tests (#3736)
1 parent cdd613b commit 1dcf438

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed
 

‎packages/logger/tests/e2e/sampleRate.decorator.test.FunctionCode.ts

+1-12
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,12 @@ const LOG_MSG = process.env.LOG_MSG || 'Hello World';
99
const logger = new Logger({
1010
sampleRateValue: SAMPLE_RATE,
1111
});
12-
let firstInvocation = true;
1312

1413
class Lambda implements LambdaInterface {
15-
private readonly logMsg: string;
14+
private readonly logMsg = LOG_MSG;
1615

17-
public constructor() {
18-
this.logMsg = LOG_MSG;
19-
}
20-
21-
// Decorate your handler class method
2216
@logger.injectLambdaContext()
2317
public async handler(_event: TestEvent, context: Context): TestOutput {
24-
if (firstInvocation) {
25-
firstInvocation = false;
26-
} else {
27-
logger.refreshSampleRateCalculation();
28-
}
2918
this.printLogInAllLevels();
3019

3120
return {

‎packages/testing/src/TestStack.ts

+47-3
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,63 @@ class TestStack {
6666
Service: 'Powertools-for-AWS-e2e-tests',
6767
},
6868
});
69+
let lastCreateLog = 0;
70+
let lastDestroyLog = 0;
71+
const creationDeleteLogFrequency = 10000; // 10 seconds
72+
const that = this;
6973
this.#cli = new Toolkit({
7074
color: false,
7175
ioHost: {
76+
/**
77+
* Log messages to the console depending on the log level.
78+
*
79+
* If the `RUNNER_DEBUG` environment variable is set to `1`, all messages are logged.
80+
*
81+
* Otherwise, we log messages that are either warnings or errors as well as periodic
82+
* updates on the stack creation and destruction process.
83+
*
84+
* @param msg - Message to log sent by the CDK CLI
85+
*/
7286
async notify(msg) {
87+
if (process.env.RUNNER_DEBUG === '1') {
88+
testConsole.log(msg);
89+
return;
90+
}
91+
if (msg.message.includes('destroyed') && msg.message.includes('✅')) {
92+
testConsole.log(msg.message);
93+
return;
94+
}
95+
if (msg.message.includes('✅') && !msg.message.includes('deployed')) {
96+
testConsole.log(`${that.testName} deployed successfully`);
97+
return;
98+
}
99+
if (msg.message.includes('CREATE_IN_PROGRESS')) {
100+
if (Date.now() - lastCreateLog < creationDeleteLogFrequency) {
101+
return;
102+
}
103+
lastCreateLog = Date.now();
104+
testConsole.log(`${that.testName} stack is being created...`);
105+
return;
106+
}
107+
if (msg.message.includes('DELETE_IN_PROGRESS')) {
108+
if (Date.now() - lastDestroyLog < creationDeleteLogFrequency) {
109+
return;
110+
}
111+
lastDestroyLog = Date.now();
112+
testConsole.log(`${that.testName} stack is being destroyed...`);
113+
return;
114+
}
115+
if (['warning', 'error'].includes(msg.level)) {
116+
testConsole.log(msg);
117+
}
118+
},
119+
async requestResponse(msg) {
73120
if (
74121
process.env.RUNNER_DEBUG === '1' ||
75122
['warning', 'error'].includes(msg.level)
76123
) {
77124
testConsole.log(msg);
78125
}
79-
},
80-
async requestResponse(msg) {
81-
testConsole.log(msg);
82126
return msg.defaultResponse;
83127
},
84128
},

0 commit comments

Comments
 (0)
Please sign in to comment.