Skip to content

Commit

Permalink
8234703: JFR TestOutOfProcessMigration.java should clean up files
Browse files Browse the repository at this point in the history
Reviewed-by: mgronlun
  • Loading branch information
egahlin committed Nov 25, 2019
1 parent 1d2c12c commit e06c17c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 46 deletions.
33 changes: 17 additions & 16 deletions test/jdk/jdk/jfr/api/consumer/streaming/TestJVMCrash.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,27 @@
*/
public class TestJVMCrash {

public static void main(String... args) throws Exception {
public static void main(String... args) throws Exception {
int id = 1;
while (true) {
TestProcess process = new TestProcess("crash-application-" + id++);
AtomicInteger eventCounter = new AtomicInteger();
try (EventStream es = EventStream.openRepository(process.getRepository())) {
// Start from first event in repository
es.setStartTime(Instant.EPOCH);
es.onEvent(e -> {
if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) {
process.crash();
try (TestProcess process = new TestProcess("crash-application-" + id++)) {
AtomicInteger eventCounter = new AtomicInteger();
try (EventStream es = EventStream.openRepository(process.getRepository())) {
// Start from first event in repository
es.setStartTime(Instant.EPOCH);
es.onEvent(e -> {
if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) {
process.crash();
}
});
es.startAsync();
// If crash corrupts chunk in repository, retry in 30 seconds
es.awaitTermination(Duration.ofSeconds(30));
if (eventCounter.get() == TestProcess.NUMBER_OF_EVENTS) {
return;
}
});
es.startAsync();
// If crash corrupts chunk in repository, retry in 30 seconds
es.awaitTermination(Duration.ofSeconds(30));
if (eventCounter.get() == TestProcess.NUMBER_OF_EVENTS) {
return;
System.out.println("Incorrect event count. Retrying...");
}
System.out.println("Incorrect event count. Retrying...");
}
}
}
Expand Down
23 changes: 12 additions & 11 deletions test/jdk/jdk/jfr/api/consumer/streaming/TestJVMExit.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@
public class TestJVMExit {

public static void main(String... args) throws Exception {
TestProcess process = new TestProcess("exit-application");
AtomicInteger eventCounter = new AtomicInteger();
try (EventStream es = EventStream.openRepository(process.getRepository())) {
// Start from first event in repository
es.setStartTime(Instant.EPOCH);
es.onEvent(e -> {
if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) {
process.exit();
}
});
es.start();
try (TestProcess process = new TestProcess("exit-application")) {
AtomicInteger eventCounter = new AtomicInteger();
try (EventStream es = EventStream.openRepository(process.getRepository())) {
// Start from first event in repository
es.setStartTime(Instant.EPOCH);
es.onEvent(e -> {
if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) {
process.exit();
}
});
es.start();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import jdk.jfr.consumer.EventStream;
import jdk.test.lib.dcmd.CommandExecutor;
import jdk.test.lib.dcmd.PidJcmdExecutor;
import jdk.test.lib.process.OutputAnalyzer;

/**
* @test
Expand All @@ -47,23 +46,25 @@
*/
public class TestOutOfProcessMigration {
public static void main(String... args) throws Exception {
Path newRepo = Paths.get("new-repository").toAbsolutePath();

TestProcess process = new TestProcess("application");
AtomicInteger eventCounter = new AtomicInteger();
try (EventStream es = EventStream.openRepository(process.getRepository())) {
// Start from first event in repository
es.setStartTime(Instant.EPOCH);
es.onEvent(e -> {
if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) {
System.out.println("Changing repository to " + newRepo + " ...");
CommandExecutor executor = new PidJcmdExecutor(String.valueOf(process.pid()));
// This should close stream
OutputAnalyzer oa = executor.execute("JFR.configure repositorypath=" + newRepo);
System.out.println(oa);
}
});
es.start();
try (TestProcess process = new TestProcess("application")) {
AtomicInteger eventCounter = new AtomicInteger();
Path newRepo = Paths.get("new-repository").toAbsolutePath();
try (EventStream es = EventStream.openRepository(process.getRepository())) {
// Start from first event in repository
es.setStartTime(Instant.EPOCH);
es.onEvent(e -> {
if (eventCounter.incrementAndGet() == TestProcess.NUMBER_OF_EVENTS) {
System.out.println("Changing repository to " + newRepo + " ...");
CommandExecutor executor = new PidJcmdExecutor(String.valueOf(process.pid()));
// This should close stream
executor.execute("JFR.configure repositorypath=" + newRepo);
}
});
es.start();
process.exit();
// Wait for process to die, so files are cleaned up
process.awaitDeath();
}
}
}
}
20 changes: 19 additions & 1 deletion test/jdk/jdk/jfr/api/consumer/streaming/TestProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
Expand All @@ -43,7 +44,7 @@
* Requires jdk.attach module.
*
*/
public final class TestProcess {
public final class TestProcess implements AutoCloseable {

private static class TestEvent extends Event {
}
Expand Down Expand Up @@ -135,4 +136,21 @@ public void exit() {
public long pid() {
return process.pid();
}

@Override
public void close() throws Exception {
try {
if (path != null) {
Files.delete(path);
}
} catch(NoSuchFileException nfe) {
// ignore
}
}

public void awaitDeath() {
while (process.isAlive()) {
takeNap();
}
}
}

0 comments on commit e06c17c

Please sign in to comment.