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

fix(jakarta): introduces locks to avoid race conditions #321

Merged
merged 3 commits into from
Mar 22, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
import org.jboss.arquillian.container.spi.client.protocol.metadata.HTTPContext;
import org.jboss.arquillian.container.test.spi.ContainerMethodExecutor;
Expand Down Expand Up @@ -78,6 +81,8 @@ public TestResult invoke(final TestMethodExecutor testMethodExecutor) {
Class<?> testClass = testMethodExecutor.getInstance().getClass();

Timer eventTimer = null;
Lock timerLock = new ReentrantLock();
AtomicBoolean isCanceled = new AtomicBoolean();
try {
String urlEncodedMethodName = URLEncoder.encode(testMethodExecutor.getMethodName(), "UTF-8");
final String url = targetBaseURI.toASCIIString() + ARQUILLIAN_SERVLET_MAPPING
Expand All @@ -88,14 +93,20 @@ public TestResult invoke(final TestMethodExecutor testMethodExecutor) {
+ "?outputMode=serializedObject&className=" + testClass.getName() + "&methodName="
+ urlEncodedMethodName + "&cmd=event";

eventTimer = createCommandServicePullTimer(eventUrl);
eventTimer = createCommandServicePullTimer(eventUrl, timerLock, isCanceled);
return executeWithRetry(url, TestResult.class);
} catch (Exception e) {
throw new IllegalStateException("Error launching test " + testClass.getName() + " "
+ testMethodExecutor.getMethod(), e);
} finally {
if (eventTimer != null) {
eventTimer.cancel();
timerLock.lock();
try {
isCanceled.set(true);
} finally {
timerLock.unlock();
}
}
}
}
Expand Down Expand Up @@ -188,7 +199,8 @@ protected <T> T execute(String url, Class<T> returnType, Object requestObject) t
protected void prepareHttpConnection(HttpURLConnection connection) {
}

protected Timer createCommandServicePullTimer(final String eventUrl) {
protected Timer createCommandServicePullTimer(final String eventUrl,
final Lock timerLock, final AtomicBoolean isCanceled) {
if (config.getPullInMilliSeconds() == null || config.getPullInMilliSeconds() <= 0) {
log.warning("The Servlet Protocol has been configured with a pullInMilliSeconds interval of " +
config.getPullInMilliSeconds() + ". The effect of this is that the Command Service has been disabled." +
Expand All @@ -200,7 +212,11 @@ protected Timer createCommandServicePullTimer(final String eventUrl) {
eventTimer.schedule(new TimerTask() {
@Override
public void run() {
timerLock.lock();
try {
if (isCanceled.get()) {
return;
}
Object o = execute(eventUrl, Object.class, null);
if (o != null) {
if (o instanceof Command) {
Expand All @@ -214,6 +230,8 @@ public void run() {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
timerLock.unlock();
}
}
}, 0, config.getPullInMilliSeconds());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
import org.jboss.arquillian.container.spi.client.protocol.metadata.HTTPContext;
import org.jboss.arquillian.container.test.spi.ContainerMethodExecutor;
Expand Down Expand Up @@ -78,6 +81,8 @@ public TestResult invoke(final TestMethodExecutor testMethodExecutor) {
Class<?> testClass = testMethodExecutor.getInstance().getClass();

Timer eventTimer = null;
Lock timerLock = new ReentrantLock();
AtomicBoolean isCanceled = new AtomicBoolean();
try {
String urlEncodedMethodName = URLEncoder.encode(testMethodExecutor.getMethodName(), "UTF-8");
final String url = targetBaseURI.toASCIIString() + ARQUILLIAN_SERVLET_MAPPING
Expand All @@ -88,14 +93,21 @@ public TestResult invoke(final TestMethodExecutor testMethodExecutor) {
+ "?outputMode=serializedObject&className=" + testClass.getName() + "&methodName="
+ urlEncodedMethodName + "&cmd=event";

eventTimer = createCommandServicePullTimer(eventUrl);
eventTimer = createCommandServicePullTimer(eventUrl, timerLock, isCanceled);
return executeWithRetry(url, TestResult.class);
} catch (Exception e) {
throw new IllegalStateException("Error launching test " + testClass.getName() + " "
+ testMethodExecutor.getMethod(), e);
} finally {
if (eventTimer != null) {
eventTimer.cancel();
timerLock.lock();
try {
isCanceled.set(true);

} finally {
timerLock.unlock();
}
}
}
}
Expand Down Expand Up @@ -188,7 +200,8 @@ protected <T> T execute(String url, Class<T> returnType, Object requestObject) t
protected void prepareHttpConnection(HttpURLConnection connection) {
}

protected Timer createCommandServicePullTimer(final String eventUrl) {
protected Timer createCommandServicePullTimer(final String eventUrl,
final Lock timerLock, final AtomicBoolean isCanceled) {
if (config.getPullInMilliSeconds() == null || config.getPullInMilliSeconds() <= 0) {
log.warning("The Servlet Protocol has been configured with a pullInMilliSeconds interval of " +
config.getPullInMilliSeconds() + ". The effect of this is that the Command Service has been disabled." +
Expand All @@ -200,7 +213,11 @@ protected Timer createCommandServicePullTimer(final String eventUrl) {
eventTimer.schedule(new TimerTask() {
@Override
public void run() {
timerLock.lock();
try {
if (isCanceled.get()) {
return;
}
Object o = execute(eventUrl, Object.class, null);
if (o != null) {
if (o instanceof Command) {
Expand All @@ -214,6 +231,8 @@ public void run() {
}
} catch (Exception e) {
e.printStackTrace();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this a little bit concerning, but that's not part of this PR anyway ;)

} finally {
timerLock.unlock();
}
}
}, 0, config.getPullInMilliSeconds());
Expand Down