Skip to content

Commit dd7a49c

Browse files
authored
Fix flaky unit tests. (#2211)
1 parent d0cdebd commit dd7a49c

File tree

7 files changed

+40
-11
lines changed

7 files changed

+40
-11
lines changed

powertools-logging/powertools-logging-log4j/src/test/java/org/apache/logging/log4j/layout/template/json/resolver/PowerToolsResolverFactoryTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ void setUp() throws IllegalAccessException, IOException {
5858

5959
@AfterEach
6060
void cleanUp() throws IOException {
61-
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
62-
FileChannel.open(Paths.get("target/ecslogfile.json"), StandardOpenOption.WRITE).truncate(0).close();
61+
try {
62+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
63+
FileChannel.open(Paths.get("target/ecslogfile.json"), StandardOpenOption.WRITE).truncate(0).close();
64+
} catch (NoSuchFileException e) {
65+
// file may not exist on the first launch
66+
}
6367
}
6468

6569
@Test

powertools-logging/powertools-logging-log4j/src/test/java/org/apache/logging/log4j/layout/template/json/resolver/PowertoolsResolverArgumentsTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@ void setUp() throws IOException {
5959

6060
@AfterEach
6161
void cleanUp() throws IOException {
62-
// Make sure file is cleaned up before running full stack logging regression
63-
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
64-
FileChannel.open(Paths.get("target/ecslogfile.json"), StandardOpenOption.WRITE).truncate(0).close();
62+
try {
63+
// Make sure file is cleaned up before running full stack logging regression
64+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
65+
FileChannel.open(Paths.get("target/ecslogfile.json"), StandardOpenOption.WRITE).truncate(0).close();
66+
} catch (NoSuchFileException e) {
67+
// may not be there in the first run
68+
}
6569
}
6670

6771
@Test

powertools-logging/powertools-logging-log4j/src/test/java/software/amazon/lambda/powertools/logging/log4j/BufferingAppenderTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ void setUp() throws IOException {
3636

3737
@AfterEach
3838
void cleanUp() throws IOException {
39-
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
39+
try {
40+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
41+
} catch (NoSuchFileException e) {
42+
// may not be there in the first run
43+
}
4044
}
4145

4246
@Test

powertools-logging/powertools-logging-logback/src/test/java/software/amazon/lambda/powertools/logging/internal/LambdaEcsEncoderTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ void setUp() throws IllegalAccessException, IOException {
6363

6464
@AfterEach
6565
void cleanUp() throws IOException {
66-
FileChannel.open(Paths.get("target/ecslogfile.json"), StandardOpenOption.WRITE).truncate(0).close();
66+
try {
67+
FileChannel.open(Paths.get("target/ecslogfile.json"), StandardOpenOption.WRITE).truncate(0).close();
68+
} catch (NoSuchFileException e) {
69+
// file may not exist on the first launch
70+
}
6771
}
6872

6973
@Test

powertools-logging/powertools-logging-logback/src/test/java/software/amazon/lambda/powertools/logging/internal/LambdaJsonEncoderTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ void setUp() throws IllegalAccessException, IOException {
8585

8686
@AfterEach
8787
void cleanUp() throws IOException {
88-
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
88+
try {
89+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
90+
} catch (NoSuchFileException e) {
91+
// file may not exist on the first launch
92+
}
8993
}
9094

9195
@Test

powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/internal/KeyBufferTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.io.PrintStream;
2323
import java.nio.channels.FileChannel;
24+
import java.nio.file.NoSuchFileException;
2425
import java.nio.file.Paths;
2526
import java.nio.file.StandardOpenOption;
2627
import java.util.Deque;
@@ -44,15 +45,19 @@ void setUp() throws IOException {
4445
// Clean up log file before each test
4546
try {
4647
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
47-
} catch (IOException e) {
48+
} catch (NoSuchFileException e) {
4849
// may not be there in the first run
4950
}
5051
}
5152

5253
@AfterEach
5354
void cleanUp() throws IOException {
5455
// Make sure file is cleaned up after each test
55-
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
56+
try {
57+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
58+
} catch (NoSuchFileException e) {
59+
// may not be there in the first run
60+
}
5661
}
5762

5863
@Test

powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/internal/LambdaLoggingAspectTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ void setUp() throws IllegalAccessException, NoSuchMethodException, InvocationTar
126126
@AfterEach
127127
void cleanUp() throws IOException {
128128
// Make sure file is cleaned up before running full stack logging regression
129-
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
129+
try {
130+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
131+
} catch (NoSuchFileException e) {
132+
// may not be there in the first run
133+
}
130134
}
131135

132136
@Test

0 commit comments

Comments
 (0)