[fix][test] Fix flaky test NarUnpackerTest (#21328)
(cherry picked from commit e76a86e3cd1c362e9daa1c88eb8b888e6ab38ab4)
# Conflicts:
# pulsar-common/src/test/java/org/apache/pulsar/common/nar/NarUnpackerTest.java
diff --git a/pulsar-common/src/test/java/org/apache/pulsar/common/nar/NarUnpackerTest.java b/pulsar-common/src/test/java/org/apache/pulsar/common/nar/NarUnpackerTest.java
index 1d2acd0..8e7e38d 100644
--- a/pulsar-common/src/test/java/org/apache/pulsar/common/nar/NarUnpackerTest.java
+++ b/pulsar-common/src/test/java/org/apache/pulsar/common/nar/NarUnpackerTest.java
@@ -46,7 +46,7 @@
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);
@@ -58,12 +58,20 @@
}
@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);
+ }
}
}
@@ -111,7 +119,7 @@
@Test
void shouldExtractFilesOnceInDifferentProcess() throws InterruptedException {
- int processes = 10;
+ int processes = 5;
String javaExePath = findJavaExe().getAbsolutePath();
CountDownLatch countDownLatch = new CountDownLatch(processes);
AtomicInteger exceptionCounter = new AtomicInteger();
@@ -122,7 +130,9 @@
// 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
@@ -130,6 +140,7 @@
// 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();