Skip to content

Commit

Permalink
[fix][test] Fix flaky test NarUnpackerTest (apache#21328)
Browse files Browse the repository at this point in the history
(cherry picked from commit e76a86e)
  • Loading branch information
lhotari authored and nikhil-ctds committed Dec 6, 2023
1 parent 46c8f34 commit d4e3a35
Showing 1 changed file with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.testng.annotations.Test;

@Slf4j
@Test
public class NarUnpackerTest {
File sampleZipFile;
File extractDirectory;
Expand All @@ -46,7 +47,7 @@ public class NarUnpackerTest {
public void createSampleZipFile() throws IOException {
sampleZipFile = Files.createTempFile("sample", ".zip").toFile();
try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(sampleZipFile))) {
for (int i = 0; i < 10000; i++) {
for (int i = 0; i < 5000; i++) {
ZipEntry e = new ZipEntry("hello" + i + ".txt");
out.putNextEntry(e);
byte[] msg = "hello world!".getBytes(StandardCharsets.UTF_8);
Expand All @@ -58,12 +59,20 @@ public void createSampleZipFile() throws IOException {
}

@AfterMethod(alwaysRun = true)
void deleteSampleZipFile() throws IOException {
if (sampleZipFile != null) {
sampleZipFile.delete();
void deleteSampleZipFile() {
if (sampleZipFile != null && sampleZipFile.exists()) {
try {
sampleZipFile.delete();
} catch (Exception e) {
log.warn("Failed to delete file {}", sampleZipFile, e);
}
}
if (extractDirectory != null) {
FileUtils.deleteFile(extractDirectory, true);
if (extractDirectory != null && extractDirectory.exists()) {
try {
FileUtils.deleteFile(extractDirectory, true);
} catch (IOException e) {
log.warn("Failed to delete directory {}", extractDirectory, e);
}
}
}

Expand Down Expand Up @@ -111,7 +120,7 @@ public static void main(String[] args) {

@Test
void shouldExtractFilesOnceInDifferentProcess() throws InterruptedException {
int processes = 10;
int processes = 5;
String javaExePath = findJavaExe().getAbsolutePath();
CountDownLatch countDownLatch = new CountDownLatch(processes);
AtomicInteger exceptionCounter = new AtomicInteger();
Expand All @@ -122,14 +131,17 @@ void shouldExtractFilesOnceInDifferentProcess() throws InterruptedException {
// fork a new process with the same classpath
Process process = new ProcessBuilder()
.command(javaExePath,
"-Xmx64m",
"-Xmx96m",
"-XX:TieredStopAtLevel=1",
"-Dlog4j2.disable.jmx=true",
"-cp",
System.getProperty("java.class.path"),
// use NarUnpackerWorker as the main class
NarUnpackerWorker.class.getName(),
// pass arguments to use for testing
sampleZipFile.getAbsolutePath(),
extractDirectory.getAbsolutePath())
.redirectErrorStream(true)
.start();
String output = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8);
int retval = process.waitFor();
Expand All @@ -147,7 +159,7 @@ void shouldExtractFilesOnceInDifferentProcess() throws InterruptedException {
}
}).start();
}
assertTrue(countDownLatch.await(30, TimeUnit.SECONDS));
assertTrue(countDownLatch.await(30, TimeUnit.SECONDS), "All processes should finish before timeout");
assertEquals(exceptionCounter.get(), 0);
assertEquals(extractCounter.get(), 1);
}
Expand Down

0 comments on commit d4e3a35

Please sign in to comment.