Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CheckpointException/RestoreException.getNestedExceptions() #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,16 @@
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<version.testng>7.10.1</version.testng>
</properties>

<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${version.testng}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -64,6 +71,15 @@
<pushChanges>false</pushChanges>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<!-- Disabling CRTrace to avoid "Corrupted channel by directly writing to native stream in forked JVM" -->
<argLine>-XX:CREngine=simengine -XX:CRaCCheckpointTo=ignored -XX:-CRTrace</argLine>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
46 changes: 31 additions & 15 deletions src/main/java/org/crac/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ static class Compat {

protected final Method checkpointRestore;
protected final Method register;
protected final Method getNestedExceptions;

protected final Object globalContext;

Expand All @@ -130,6 +131,14 @@ protected Compat(String pkg)
register = clsContext.getMethod("register", clsResource);

globalContext = clsCore.getMethod("getGlobalContext").invoke(null);

Method getNestedExceptions;
try {
getNestedExceptions = clsCheckpointException.getMethod("getNestedExceptions");
} catch (NoSuchMethodException e) {
getNestedExceptions = clsCheckpointException.getMethod("getSuppressed");
}
this.getNestedExceptions = getNestedExceptions;
}

public void checkpointRestore() throws
Expand All @@ -140,22 +149,29 @@ public void checkpointRestore() throws
try {
checkpointRestore.invoke(null);
} catch (InvocationTargetException | IllegalAccessException ite) {
if (clsCheckpointException.isInstance(ite.getCause())) {
CheckpointException checkpointException = new CheckpointException();
for (Throwable t : ite.getCause().getSuppressed()) {
checkpointException.addSuppressed(t);
}
throw checkpointException;
} else if (clsRestoreException.isInstance(ite.getCause())) {
RestoreException restoreException = new RestoreException();
for (Throwable t : ite.getCause().getSuppressed()) {
restoreException.addSuppressed(t);
try {
if (clsCheckpointException.isInstance(ite.getCause())) {
CheckpointException checkpointException = new CheckpointException();
for (Throwable t : (Throwable[]) getNestedExceptions.invoke(ite.getCause())) {
checkpointException.addSuppressed(t);
}
throw checkpointException;
} else if (clsRestoreException.isInstance(ite.getCause())) {
RestoreException restoreException = new RestoreException();
for (Throwable t : (Throwable[]) getNestedExceptions.invoke(ite.getCause())) {
restoreException.addSuppressed(t);
}
throw restoreException;
} else {
CheckpointException checkpointException = new CheckpointException();
checkpointException.addSuppressed(ite);
throw checkpointException;
}
throw restoreException;
} else {
CheckpointException checkpointException = new CheckpointException();
checkpointException.addSuppressed(ite);
throw checkpointException;
} catch (InvocationTargetException | IllegalAccessException e) {
CheckpointException ex = new CheckpointException("Cannot remap original exceptions");
ex.addSuppressed(e);
ex.addSuppressed(ite);
throw ex;
}
}
}
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/org/crac/TestExceptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.crac;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.lang.ref.Reference;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;

public class TestExceptions {
@BeforeMethod
public void beforeMethod() {
// Ensure that any resource is garbage-collected
System.gc();
}

@Test
public void testCheckpointException() throws RestoreException {
Resource resource = new Resource() {
@Override
public void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
throw new Exception("FOO");
}

@Override
public void afterRestore(Context<? extends Resource> context) {
}
};
Core.getGlobalContext().register(resource);
try{
Core.checkpointRestore();
fail("Expecting checkpoint exception");
} catch (CheckpointException e) {
assertEquals(e.getSuppressed().length, 1);
assertEquals(e.getSuppressed()[0].getMessage(), "FOO");
}
Reference.reachabilityFence(resource);
}

@Test
public void testRestoreException() throws CheckpointException {
Resource resource = new Resource() {
@Override
public void beforeCheckpoint(Context<? extends Resource> context) throws Exception {
}

@Override
public void afterRestore(Context<? extends Resource> context) throws Exception {
throw new Exception("BAR");
}
};
Core.getGlobalContext().register(resource);
try{
Core.checkpointRestore();
fail("Expecting restore exception");
} catch (RestoreException e) {
assertEquals(e.getSuppressed().length, 1);
assertEquals(e.getSuppressed()[0].getMessage(), "BAR");
}
Reference.reachabilityFence(resource);
}
}