[jboss-cvs] JBossAS SVN: r109816 - in trunk/testsuite/src/main/org/jboss/test/ejb3/async: test and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Dec 10 04:36:05 EST 2010
Author: ALRubinger
Date: 2010-12-10 04:36:05 -0500 (Fri, 10 Dec 2010)
New Revision: 109816
Added:
trunk/testsuite/src/main/org/jboss/test/ejb3/async/StatusBean.java
trunk/testsuite/src/main/org/jboss/test/ejb3/async/StatusCommonBusiness.java
Modified:
trunk/testsuite/src/main/org/jboss/test/ejb3/async/AsyncBean.java
trunk/testsuite/src/main/org/jboss/test/ejb3/async/AsyncCommonBusiness.java
trunk/testsuite/src/main/org/jboss/test/ejb3/async/AsyncTesterBean.java
trunk/testsuite/src/main/org/jboss/test/ejb3/async/AsyncTesterCommonBusiness.java
trunk/testsuite/src/main/org/jboss/test/ejb3/async/test/AsyncSessionBeanUnitTestCase.java
Log:
[EJBTHREE-1721] Add coverage for async cancel and nointerface
Modified: trunk/testsuite/src/main/org/jboss/test/ejb3/async/AsyncBean.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/ejb3/async/AsyncBean.java 2010-12-10 08:32:31 UTC (rev 109815)
+++ trunk/testsuite/src/main/org/jboss/test/ejb3/async/AsyncBean.java 2010-12-10 09:36:05 UTC (rev 109816)
@@ -1,5 +1,8 @@
package org.jboss.test.ejb3.async;
+import org.jboss.logging.Logger;
+
+import javax.annotation.Resource;
import javax.ejb.*;
import java.util.concurrent.Future;
@@ -11,8 +14,18 @@
@Stateless
@Local(AsyncLocalBusiness.class)
@Remote(AsyncRemoteBusiness.class)
+ at LocalBean
public class AsyncBean implements AsyncCommonBusiness
{
+
+ private static final Logger log = Logger.getLogger(AsyncBean.class);
+
+ @Resource
+ private SessionContext context;
+
+ @EJB
+ private StatusCommonBusiness status;
+
@Asynchronous
public Future<String> getExecutingThreadNameAsync() {
return this.getExecutingThreadName();
@@ -22,6 +35,44 @@
return this.getExecutingThreadName();
}
+ @Asynchronous
+ public Future<Void> waitTenSeconds(){
+
+ log.info("Entered request to wait one minute");
+ // Reset the singleton status flag
+ status.reset();
+
+ final long start = System.currentTimeMillis();
+ // End in 10 seconds
+ final long end = start+(10*1000);
+
+ // Loop
+ log.info("START LOOP");
+ while(System.currentTimeMillis()<end){
+
+ // If we've been cancelled, update the singleton status bit
+ if(context.wasCancelCalled())
+ {
+ status.set(true);
+ log.info("Set status to true");
+ // Break out of the loop
+ break;
+ }
+
+ // Wait a bit and check again
+ try {
+ log.info("Waiting...");
+ Thread.sleep(50);
+ } catch (final InterruptedException e) {
+ Thread.interrupted(); // Clear the flag
+ throw new RuntimeException("Should not have been interrupted");
+ }
+
+ }
+ // Return a dummy value
+ return new AsyncResult<Void>(null);
+ }
+
private Future<String> getExecutingThreadName(){
return new AsyncResult<String>(Thread.currentThread().getName());
}
Modified: trunk/testsuite/src/main/org/jboss/test/ejb3/async/AsyncCommonBusiness.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/ejb3/async/AsyncCommonBusiness.java 2010-12-10 08:32:31 UTC (rev 109815)
+++ trunk/testsuite/src/main/org/jboss/test/ejb3/async/AsyncCommonBusiness.java 2010-12-10 09:36:05 UTC (rev 109816)
@@ -47,4 +47,11 @@
* @return
*/
Future<String> getExecutingThreadNameBlocking();
+
+
+ /**
+ * Waits for a full minute before returning
+ * @return
+ */
+ Future<Void> waitTenSeconds();
}
\ No newline at end of file
Modified: trunk/testsuite/src/main/org/jboss/test/ejb3/async/AsyncTesterBean.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/ejb3/async/AsyncTesterBean.java 2010-12-10 08:32:31 UTC (rev 109815)
+++ trunk/testsuite/src/main/org/jboss/test/ejb3/async/AsyncTesterBean.java 2010-12-10 09:36:05 UTC (rev 109816)
@@ -21,12 +21,10 @@
*/
package org.jboss.test.ejb3.async;
-import org.jboss.aop.advice.Interceptor;
-import org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase;
-
-import javax.ejb.*;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.Proxy;
+import javax.ejb.EJB;
+import javax.ejb.Remote;
+import javax.ejb.Stateless;
+import java.util.concurrent.*;
import java.util.logging.Logger;
/**
@@ -59,6 +57,12 @@
@EJB
private AsyncRemoteBusiness remote;
+ @EJB
+ private AsyncBean nointerface;
+
+ @EJB
+ private StatusCommonBusiness status;
+
// --------------------------------------------------------------------------------||
// Required Implementations -------------------------------------------------------||
// --------------------------------------------------------------------------------||
@@ -68,16 +72,8 @@
* @return
*/
public boolean isLocalAsyncInvocationExecutedInNewThread() {
- final String current = Thread.currentThread().getName();
- final String used;
- try{
- used = local.getExecutingThreadNameAsync().get();
- }
- catch(final Exception e){
- throw new RuntimeException(e);
- }
- log.info("Local @Asynchronous Invocation - Current:" + current + ", Used: " + used);
- return !current.equals(used);
+ log.info("Local @Asynchronous Invocation");
+ return isAsyncInvocationExecutedInNewThread(local);
}
/**
@@ -85,15 +81,30 @@
* @return
*/
public boolean isRemoteAsyncInvocationExecutedInNewThread() {
- final String current = Thread.currentThread().getName();
+ log.info("Remote @Asynchronous Invocation");
+ return isAsyncInvocationExecutedInNewThread(remote);
+ }
+
+ /**
+ *
+ * @return
+ */
+ public boolean isNointerfaceAsyncInvocationExecutedInNewThread() {
+ log.info("No-interface @Asynchronous Invocation");
+ return isAsyncInvocationExecutedInNewThread(nointerface);
+ }
+
+ private boolean isAsyncInvocationExecutedInNewThread(final AsyncCommonBusiness bean)
+ {
+ final String current = Thread.currentThread().getName();
final String used;
try{
- used = remote.getExecutingThreadNameAsync().get();
+ used = bean.getExecutingThreadNameAsync().get();
}
catch(final Exception e){
throw new RuntimeException(e);
}
- log.info("Remote @Asynchronous Invocation - Current:" + current + ", Used: " + used);
+ log.info("@Asynchronous Invocation - Current:" + current + ", Used: " + used);
return !current.equals(used);
}
@@ -109,4 +120,67 @@
log.info("Local non- at Asynchronous Invocation - Current:" + current + ", Used: " + used);
return current.equals(used);
}
+
+
+ public boolean cancelInterruptIfRunningWorksLocal() {
+ return this.cancelInterruptIfRunningWorks(local);
+ }
+
+ public boolean cancelInterruptIfRunningWorksRemote() {
+ return this.cancelInterruptIfRunningWorks(remote);
+ }
+
+ private boolean cancelInterruptIfRunningWorks(final AsyncCommonBusiness bean)
+ { final Future<Void> future = bean.waitTenSeconds();
+
+ // At least let the request get sent along
+ try
+ {
+ Thread.sleep(300);
+ }
+ catch(final InterruptedException ie)
+ {
+ Thread.interrupted();
+ throw new RuntimeException("Should not be interrupted");
+ }
+
+ final ExecutorService es =Executors.newSingleThreadExecutor();
+ es.submit(new Callable<Void>() {
+
+ public Void call() throws Exception {
+ Thread.sleep(1000);
+ future.cancel(true);
+ return null;
+ }
+ });
+ es.shutdown();
+
+ log.info("Submitted cancel request");
+ try {
+ // Block
+ log.info("Blocking on result now");
+ future.get(20, TimeUnit.SECONDS);
+ log.info("Done? " +future.isDone());
+ log.info("Got result");
+ } catch (InterruptedException e) {
+ Thread.interrupted();
+ log.info(e.getMessage());
+ throw new RuntimeException("Should not be interrupted");
+ } catch (ExecutionException e) {
+ log.info(e.getMessage());
+ throw new RuntimeException("Should not receive execution exception");
+ }
+ catch(final TimeoutException te)
+ {
+ log.info("TIMEOUT");
+ throw new RuntimeException("Should not have timed out",te);
+ }
+ catch(final RuntimeException re){
+ log.info("RuntimeException: " + re);
+ }
+ log.info("Returning");
+
+ return status.wasCancelled();
+ }
+
}
\ No newline at end of file
Modified: trunk/testsuite/src/main/org/jboss/test/ejb3/async/AsyncTesterCommonBusiness.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/ejb3/async/AsyncTesterCommonBusiness.java 2010-12-10 08:32:31 UTC (rev 109815)
+++ trunk/testsuite/src/main/org/jboss/test/ejb3/async/AsyncTesterCommonBusiness.java 2010-12-10 09:36:05 UTC (rev 109816)
@@ -44,10 +44,27 @@
*/
boolean isRemoteAsyncInvocationExecutedInNewThread();
+ /**
+ * Returns whether or not a remote invocation is executed in a new Thread
+ */
+ boolean isNointerfaceAsyncInvocationExecutedInNewThread();
+
/**
* Returns whether a Future return type on a non-Asynchronous
* method is executed within the same Thread
* @return
*/
boolean isLocalBlockingInvocationExecutedInSameThread();
+
+ /**
+ *
+ * @return
+ */
+ boolean cancelInterruptIfRunningWorksLocal();
+
+ /**
+ *
+ * @return
+ */
+ boolean cancelInterruptIfRunningWorksRemote();
}
\ No newline at end of file
Added: trunk/testsuite/src/main/org/jboss/test/ejb3/async/StatusBean.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/ejb3/async/StatusBean.java (rev 0)
+++ trunk/testsuite/src/main/org/jboss/test/ejb3/async/StatusBean.java 2010-12-10 09:36:05 UTC (rev 109816)
@@ -0,0 +1,35 @@
+package org.jboss.test.ejb3.async;
+
+import org.jboss.logging.Logger;
+
+import javax.annotation.PostConstruct;
+import javax.ejb.Local;
+import javax.ejb.Singleton;
+
+/**
+ * Implementation of an EJB to track the status
+ * of @Asynchronous cancel methods
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ */
+ at Singleton
+ at Local(StatusCommonBusiness.class)
+public class StatusBean implements StatusCommonBusiness {
+
+ private static final Logger log = Logger.getLogger(StatusBean.class);
+ private boolean cancelled;
+
+ @PostConstruct
+ public void reset() {
+ this.set(false);
+ }
+
+ public void set(final boolean cancelled) {
+ log.info("Setting Cancelled Status: " + cancelled);
+ this.cancelled = cancelled;
+ }
+
+ public boolean wasCancelled() {
+ return cancelled;
+ }
+}
\ No newline at end of file
Added: trunk/testsuite/src/main/org/jboss/test/ejb3/async/StatusCommonBusiness.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/ejb3/async/StatusCommonBusiness.java (rev 0)
+++ trunk/testsuite/src/main/org/jboss/test/ejb3/async/StatusCommonBusiness.java 2010-12-10 09:36:05 UTC (rev 109816)
@@ -0,0 +1,54 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.ejb3.async;
+
+/**
+ * Business view of a bean with to keep track of
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @Asynchronous cancel events
+ */
+public interface StatusCommonBusiness {
+ // --------------------------------------------------------------------------------||
+ // Contracts ----------------------------------------------------------------------||
+ // --------------------------------------------------------------------------------||
+
+ /**
+ * Returns whether or not the last async call was cancelled
+ *
+ * @return
+ */
+ boolean wasCancelled();
+
+ /**
+ * Resets the cancelled flag
+ */
+ void reset();
+
+ /**
+ * Sets the status bit
+ *
+ * @param cancelled
+ */
+ void set(final boolean cancelled);
+
+}
\ No newline at end of file
Modified: trunk/testsuite/src/main/org/jboss/test/ejb3/async/test/AsyncSessionBeanUnitTestCase.java
===================================================================
--- trunk/testsuite/src/main/org/jboss/test/ejb3/async/test/AsyncSessionBeanUnitTestCase.java 2010-12-10 08:32:31 UTC (rev 109815)
+++ trunk/testsuite/src/main/org/jboss/test/ejb3/async/test/AsyncSessionBeanUnitTestCase.java 2010-12-10 09:36:05 UTC (rev 109816)
@@ -65,11 +65,28 @@
Assert.assertTrue("Local invocation not executed in new Thread",bean.isLocalAsyncInvocationExecutedInNewThread());
}
+ public void testNointerfaceAsyncInvocation()
+ throws Exception
+ {
+ final AsyncTesterCommonBusiness bean = this.getTesterBean();
+ Assert.assertTrue("Nointerface invocation not executed in new Thread",bean.isNointerfaceAsyncInvocationExecutedInNewThread());
+ }
+
public void testLocalBlockingInvocation() throws Exception{
final AsyncTesterCommonBusiness bean = this.getTesterBean();
Assert.assertTrue("Local non-async invocation not executed in same Thread",bean.isLocalBlockingInvocationExecutedInSameThread());
}
+ public void testLocalCancel() throws Exception{
+ final AsyncTesterCommonBusiness bean = this.getTesterBean();
+ Assert.assertTrue("Local async call should be able to be cancelled",bean.cancelInterruptIfRunningWorksLocal());
+ }
+
+ public void testLocalRemote() throws Exception{
+ final AsyncTesterCommonBusiness bean = this.getTesterBean();
+ Assert.assertTrue("Remote async call should be able to be cancelled",bean.cancelInterruptIfRunningWorksRemote());
+ }
+
private AsyncTesterCommonBusiness getTesterBean() throws Exception {
final InitialContext ctx = getInitialContext();
final String jndiName = "AsyncTesterBean/remote";
More information about the jboss-cvs-commits
mailing list