Skip to content

Commit

Permalink
[JBJCA-833] WorkManager test suite refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Rastseluev committed Jun 20, 2012
1 parent 15cc35e commit ce51b35
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 73 deletions.
Expand Up @@ -326,23 +326,11 @@ public void doWork(Work work, long startTimeout, ExecutionContext execContext, W
if (trace)
log.tracef("doWork(%s, %s, %s, %s)", work, startTimeout, execContext, workListener);

if (isShutdown())
{
statistics.deltaDoWorkRejected();
throw new WorkRejectedException(bundle.workmanagerShutdown());
}

WorkException exception = null;
WorkWrapper wrapper = null;
try
{
if (work == null)
throw new WorkRejectedException(bundle.workIsNull());

if (startTimeout < 0)
throw new WorkRejectedException(bundle.startTimeoutIsNegative(startTimeout));

checkAndVerifyWork(work, execContext);
doFirstChecks(work, startTimeout, execContext);

if (execContext == null)
{
Expand Down Expand Up @@ -435,25 +423,13 @@ public long startWork(Work work, long startTimeout, ExecutionContext execContext
{
log.tracef("startWork(%s, %s, %s, %s)", work, startTimeout, execContext, workListener);

if (isShutdown())
{
statistics.deltaStartWorkRejected();
throw new WorkRejectedException(bundle.workmanagerShutdown());
}

WorkException exception = null;
WorkWrapper wrapper = null;
try
{
if (work == null)
throw new WorkRejectedException(bundle.workIsNull());

if (startTimeout < 0)
throw new WorkRejectedException(bundle.startTimeoutIsNegative(startTimeout));

long started = System.currentTimeMillis();

checkAndVerifyWork(work, execContext);
doFirstChecks(work, startTimeout, execContext);

if (execContext == null)
{
Expand Down Expand Up @@ -548,23 +524,11 @@ public void scheduleWork(Work work, long startTimeout, ExecutionContext execCont
{
log.tracef("scheduleWork(%s, %s, %s, %s)", work, startTimeout, execContext, workListener);

if (isShutdown())
{
statistics.deltaScheduleWorkRejected();
throw new WorkRejectedException(bundle.workmanagerShutdown());
}

WorkException exception = null;
WorkWrapper wrapper = null;
try
{
if (work == null)
throw new WorkRejectedException(bundle.workIsNull());

if (startTimeout < 0)
throw new WorkRejectedException(bundle.startTimeoutIsNegative(startTimeout));

checkAndVerifyWork(work, execContext);
doFirstChecks(work, startTimeout, execContext);

if (execContext == null)
{
Expand Down Expand Up @@ -635,6 +599,27 @@ public void scheduleWork(Work work, long startTimeout, ExecutionContext execCont
}
}

/**
* Do first checks for work starting methods
* @param work to check
* @param startTimeout to check
* @param execContext to check
* @throws WorkException in case of check don't pass
*/
public void doFirstChecks(Work work, long startTimeout, ExecutionContext execContext) throws WorkException
{
if (isShutdown())
throw new WorkRejectedException(bundle.workmanagerShutdown());

if (work == null)
throw new WorkRejectedException(bundle.workIsNull());

if (startTimeout < 0)
throw new WorkRejectedException(bundle.startTimeoutIsNegative(startTimeout));

checkAndVerifyWork(work, execContext);
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -927,14 +912,6 @@ else if (isHintContext(contextType))
isHintcontext = true;
}
}
// Normally, this must not be happened!i just safe check!
else
{
fireWorkContextSetupFailed(context, WorkContextErrorCodes.UNSUPPORTED_CONTEXT_TYPE);

throw new WorkCompletedException(bundle.unsupportedWorkContextClass(context.getClass().getName()),
WorkContextErrorCodes.UNSUPPORTED_CONTEXT_TYPE);
}
}

// Add workcontext instance to the work
Expand Down
Expand Up @@ -26,6 +26,8 @@
import javax.resource.spi.work.WorkEvent;
import javax.resource.spi.work.WorkException;

import static org.junit.Assert.*;

/**
* MyWorkAdapter
*/
Expand All @@ -34,37 +36,42 @@ public class MyWorkAdapter extends WorkAdapter

/** event source */
private Object source;

/** event work */
private Work work;

/** start duration time */
private long startDuration;

/** exception */
private WorkException exception;

private CallbackCount callbackCount;

/**
* accept work
*
* @param e workEvent
*/
@Override
public void workAccepted(WorkEvent e)
public void workAccepted(WorkEvent e)
{
if (e.getType() != WorkEvent.WORK_ACCEPTED)
fail("Wrong accepted type");
source = e.getSource();
work = e.getWork();
startDuration = e.getStartDuration();
exception = e.getException();

if (callbackCount != null)
{
synchronized (this)
synchronized (this)
{
callbackCount.setAcceptCount(callbackCount.getAcceptCount() + 1);
}
}

super.workCompleted(e);
super.workAccepted(e);
}

/**
Expand All @@ -75,17 +82,20 @@ public void workAccepted(WorkEvent e)
@Override
public void workStarted(WorkEvent e)
{
if (e.getType() != WorkEvent.WORK_STARTED)
fail("Wrong started type");

if (callbackCount != null)
{
synchronized (this)
synchronized (this)
{
callbackCount.setStartCount(callbackCount.getStartCount() + 1);
}
}

super.workStarted(e);
}

/**
* start work
*
Expand All @@ -94,22 +104,25 @@ public void workStarted(WorkEvent e)
@Override
public void workRejected(WorkEvent e)
{
if (e.getType() != WorkEvent.WORK_REJECTED)
fail("Wrong rejected type");

source = e.getSource();
work = e.getWork();
startDuration = e.getStartDuration();
exception = e.getException();

if (callbackCount != null)
{
synchronized (this)
synchronized (this)
{
callbackCount.setRejectedCount(callbackCount.getRejectedCount() + 1);
}
}

super.workRejected(e);
}

/**
* complete work
*
Expand All @@ -118,9 +131,12 @@ public void workRejected(WorkEvent e)
@Override
public void workCompleted(WorkEvent e)
{
if (e.getType() != WorkEvent.WORK_COMPLETED)
fail("Wrong completed type");

if (callbackCount != null)
{
synchronized (this)
synchronized (this)
{
callbackCount.setCompletedCount(callbackCount.getCompletedCount() + 1);
}
Expand All @@ -138,7 +154,7 @@ public Object getSource()
{
return source;
}

/**
* get event work
*
Expand All @@ -148,7 +164,7 @@ public Work getWork()
{
return work;
}

/**
* get start duration time
*
Expand All @@ -158,6 +174,7 @@ public long getStartDuration()
{
return startDuration;
}

/**
* get exception
* @return exception
Expand All @@ -166,7 +183,7 @@ public WorkException getException()
{
return exception;
}

/**
* set callback reference
* @param callbackCount complete count
Expand Down
Expand Up @@ -24,6 +24,8 @@

import org.jboss.jca.core.api.workmanager.WorkManager;
import org.jboss.jca.core.api.workmanager.WorkManagerStatistics;
import org.jboss.jca.core.workmanager.spec.chapter10.common.CallbackCount;
import org.jboss.jca.core.workmanager.spec.chapter10.common.MyWorkAdapter;
import org.jboss.jca.core.workmanager.spec.chapter10.common.SimpleWork;
import org.jboss.jca.embedded.arquillian.Inject;

Expand All @@ -47,7 +49,7 @@
@RunWith(Arquillian.class)
public class WorkManagerPrepareShutdownTestCase
{

private static final Logger LOG = Logger.getLogger(WorkManagerPrepareShutdownTestCase.class);

/**
Expand All @@ -68,12 +70,16 @@ public void testPrepareShutdown() throws Throwable
assertFalse(workManager.isShutdown());

Work work = new SimpleWork();
MyWorkAdapter wa = new MyWorkAdapter();
CallbackCount callbackCount = new CallbackCount();
wa.setCallbackCount(callbackCount);

workManager.prepareShutdown();
assertTrue(workManager.isShutdown());

try
{
workManager.doWork(work);
workManager.doWork(work, WorkManager.INDEFINITE, null, wa);
fail("exception should be thrown");
}
catch (WorkRejectedException e)
Expand All @@ -82,7 +88,7 @@ public void testPrepareShutdown() throws Throwable
}
try
{
workManager.startWork(work);
workManager.startWork(work, WorkManager.INDEFINITE, null, wa);
fail("exception should be thrown");
}
catch (WorkRejectedException e)
Expand All @@ -91,21 +97,21 @@ public void testPrepareShutdown() throws Throwable
}
try
{
workManager.scheduleWork(work);
workManager.scheduleWork(work, WorkManager.INDEFINITE, null, wa);
fail("exception should be thrown");
}
catch (WorkRejectedException e)
{
//Expected
}

assertEquals(0, stat.getWorkActive());
assertEquals(0, stat.getDoWorkAccepted());
assertEquals(1, stat.getDoWorkRejected());
assertEquals(1, stat.getScheduleWorkRejected());
assertEquals(0, stat.getScheduleWorkAccepted());
assertEquals(0, stat.getStartWorkAccepted());
assertEquals(1, stat.getStartWorkRejected());
assertEquals("should be same", 3, callbackCount.getRejectedCount());
assertEquals("should be same", 0, stat.getWorkActive());
assertEquals("should be same", 0, stat.getDoWorkAccepted());
assertEquals("should be same", 1, stat.getDoWorkRejected());
assertEquals("should be same", 1, stat.getScheduleWorkRejected());
assertEquals("should be same", 0, stat.getScheduleWorkAccepted());
assertEquals("should be same", 0, stat.getStartWorkAccepted());
assertEquals("should be same", 1, stat.getStartWorkRejected());
LOG.info(stat.toString());

}
Expand Down

0 comments on commit ce51b35

Please sign in to comment.