You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by gd...@apache.org on 2004/05/17 14:41:04 UTC

cvs commit: incubator-geronimo/modules/connector/src/test/org/apache/geronimo/connector/work PooledWorkManagerTest.java

gdamour     2004/05/17 05:41:04

  Modified:    modules/connector/src/test/org/apache/geronimo/connector/work
                        PooledWorkManagerTest.java
  Log:
  Attempt to fix the testDoWork method, which fails occasionally due the
  too short period of time within which it has to accept a Work.
  
  Revision  Changes    Path
  1.5       +91 -88    incubator-geronimo/modules/connector/src/test/org/apache/geronimo/connector/work/PooledWorkManagerTest.java
  
  Index: PooledWorkManagerTest.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/connector/src/test/org/apache/geronimo/connector/work/PooledWorkManagerTest.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- PooledWorkManagerTest.java	10 Mar 2004 09:58:34 -0000	1.4
  +++ PooledWorkManagerTest.java	17 May 2004 12:41:04 -0000	1.5
  @@ -23,9 +23,6 @@
   import javax.resource.spi.work.WorkEvent;
   import javax.resource.spi.work.WorkException;
   import javax.resource.spi.work.WorkListener;
  -import javax.resource.spi.work.WorkManager;
  -
  -import org.apache.geronimo.kernel.jmx.JMXKernel;
   
   import junit.framework.TestCase;
   
  @@ -37,95 +34,96 @@
    */
   public class PooledWorkManagerTest extends TestCase {
   
  -    private JMXKernel m_kernel;
  -    private GeronimoWorkManager m_workManager;
  -    private static final int m_nbMin = 1;
  -    private static final int m_nbMax = 1;
  -    private static final int m_timeout = 300;
  -    private static final int m_tempo = 200;
  +    private GeronimoWorkManager workManager;
   
       protected void setUp() throws Exception {
  -        m_workManager = new GeronimoWorkManager(1, 1, null);
  +        workManager = new GeronimoWorkManager(1, 1, null);
       }
  -
  +    
  +    public static void main(String[] args) throws Exception {
  +        PooledWorkManagerTest managerTest = new PooledWorkManagerTest();
  +        managerTest.setUp();
  +        while(true) {
  +            managerTest.testDoWork();
  +        }
  +    }
  +    
       public void testDoWork() throws Exception {
  -        int nbThreads = 3;
  -        AbstractDummyWork threads[] = helperTest(DummyDoWork.class, nbThreads);
  +        int nbThreads = 2;
  +        AbstractDummyWork threads[] =
  +            helperTest(DummyDoWork.class, nbThreads, 500, 600);
           int nbStopped = 0;
           int nbTimeout = 0;
           for (int i = 0; i < threads.length; i++) {
  -            if (threads[i].m_listener.m_event.getType() ==
  -                    WorkEvent.WORK_COMPLETED) {
  +            if ( null != threads[i].listener.completedEvent ) {
                   nbStopped++;
  -            } else if (threads[i].m_listener.m_event.getType() ==
  -                    WorkEvent.WORK_REJECTED) {
  +            } else if ( null != threads[i].listener.rejectedEvent ) {
                   assertTrue("Should be a time out exception.",
  -                        threads[i].m_listener.m_event.getException().
  -                        getErrorCode() == WorkException.START_TIMED_OUT);
  +                    threads[i].listener.rejectedEvent.getException().
  +                    getErrorCode() == WorkException.START_TIMED_OUT);
                   nbTimeout++;
               } else {
  -                assertTrue("Works should be either in the WORK_COMPLETED or " +
  -                        "WORK_REJECTED state", false);
  +                fail("WORK_COMPLETED or WORK_REJECTED expected");
               }
           }
  -        assertTrue("Wrong number of works in the WORK_COMPLETED state: " +
  -                "expected " + (nbThreads - 1) + "; retrieved " + nbStopped,
  -                (nbThreads - 1) == nbStopped);
  -        assertTrue("Wrong number of works in the START_TIMED_OUT state: " +
  -                "expected 1; retrieved " + nbTimeout, 1 == nbTimeout);
  +        assertEquals("Wrong number of works in the WORK_COMPLETED state",
  +            1, nbStopped);
  +        assertEquals("Wrong number of works in the START_TIMED_OUT state",
  +            1, nbTimeout);
       }
   
       public void testStartWork() throws Exception {
  -        AbstractDummyWork threads[] = helperTest(DummyStartWork.class, 2);
  +        AbstractDummyWork threads[] =
  +            helperTest(DummyStartWork.class, 2, 10000, 100);
           int nbStopped = 0;
           int nbStarted = 0;
           for (int i = 0; i < threads.length; i++) {
  -            if (threads[i].m_listener.m_event.getType() ==
  -                    WorkEvent.WORK_COMPLETED) {
  +            if ( null != threads[i].listener.completedEvent ) {
                   nbStopped++;
  -            } else if (threads[i].m_listener.m_event.getType() ==
  -                    WorkEvent.WORK_STARTED) {
  +            } else if ( null != threads[i].listener.startedEvent ) {
                   nbStarted++;
               } else {
  -                assertTrue("Works should be either in the WORK_COMPLETED or " +
  -                        "WORK_STARTED state", false);
  +                fail("WORK_COMPLETED or WORK_STARTED expected");
               }
           }
  -        assertTrue("At least one work should be in the WORK_COMPLETED state.",
  -                nbStopped == 1);
  -        assertTrue("At least one work should be in the WORK_STARTED state.",
  -                nbStarted == 1);
  +        assertEquals("At least one work should be in the WORK_COMPLETED state.",
  +            1, nbStopped);
  +        assertEquals("At least one work should be in the WORK_STARTED state.",
  +            1, nbStarted);
       }
   
       public void testScheduleWork() throws Exception {
  -        AbstractDummyWork threads[] = helperTest(DummyScheduleWork.class, 3);
  +        AbstractDummyWork threads[] =
  +            helperTest(DummyScheduleWork.class, 3, 10000, 100);
           int nbAccepted = 0;
           int nbStarted = 0;
           for (int i = 0; i < threads.length; i++) {
  -            if (threads[i].m_listener.m_event.getType() ==
  -                    WorkEvent.WORK_ACCEPTED) {
  +            if ( null != threads[i].listener.acceptedEvent ) {
                   nbAccepted++;
  -            } else if (threads[i].m_listener.m_event.getType() ==
  -                    WorkEvent.WORK_STARTED) {
  +            } else if ( null != threads[i].listener.startedEvent ) {
                   nbStarted++;
               } else {
  -                assertTrue("Works should be eithe in the WORK_ACCEPTED or" +
  -                        "  WORK_STARTED state.", false);
  +                fail("WORK_ACCEPTED or WORK_STARTED expected");
               }
           }
           assertTrue("At least one work should be in the WORK_ACCEPTED state.",
  -                nbAccepted > 0);
  +            nbAccepted > 0);
       }
   
  -    private AbstractDummyWork[] helperTest(Class aWork, int nbThreads)
  -            throws Exception {
  +    private AbstractDummyWork[] helperTest(Class aWork, int nbThreads,
  +        int aTimeOut, int aTempo)
  +        throws Exception {
           Constructor constructor = aWork.getConstructor(
  -                new Class[]{WorkManager.class, String.class});
  -        AbstractDummyWork rarThreads[] =
  -                new AbstractDummyWork[nbThreads];
  +            new Class[]{PooledWorkManagerTest.class, String.class,
  +                int.class, int.class});
  +        AbstractDummyWork rarThreads[] = new AbstractDummyWork[nbThreads];
  +        for (int i = 0; i < nbThreads; i++) {
  +            rarThreads[i] = (AbstractDummyWork)
  +                constructor.newInstance(
  +                    new Object[]{this, "Work" + i,
  +                        new Integer(aTimeOut), new Integer(aTempo)});
  +        }
           for (int i = 0; i < nbThreads; i++) {
  -            rarThreads[i] = (AbstractDummyWork) constructor.newInstance(
  -                    new Object[]{m_workManager, "Work" + i});
               rarThreads[i].start();
           }
           for (int i = 0; i < nbThreads; i++) {
  @@ -134,20 +132,20 @@
           return rarThreads;
       }
   
  -    public static abstract class AbstractDummyWork extends Thread {
  -        public DummyWorkListener m_listener;
  -        protected WorkManager m_workManager;
  -        protected String m_name;
  -
  -        public AbstractDummyWork(WorkManager aWorkManager, String aName) {
  -            m_workManager = aWorkManager;
  -            m_listener = new DummyWorkListener();
  -            m_name = aName;
  +    public abstract class AbstractDummyWork extends Thread {
  +        public final DummyWorkListener listener;
  +        protected final  String name;
  +        private final int timeout;
  +        private final int tempo;
  +        public AbstractDummyWork(String aName, int aTimeOut, int aTempo) {
  +            listener = new DummyWorkListener();
  +            timeout = aTimeOut;
  +            tempo = aTempo;
  +            name = aName;
           }
  -
           public void run() {
               try {
  -                perform(new DummyWork(m_name), m_timeout, null, m_listener);
  +                perform(new DummyWork(name, tempo), timeout, null, listener);
               } catch (Exception e) {
                   e.printStackTrace();
               }
  @@ -159,50 +157,52 @@
                   WorkListener workListener) throws Exception;
       }
   
  -    public static class DummyDoWork extends AbstractDummyWork {
  -        public DummyDoWork(WorkManager aWorkManager, String aName) {
  -            super(aWorkManager, aName);
  +    public class DummyDoWork extends AbstractDummyWork {
  +        public DummyDoWork(String aName, int aTimeOut, int aTempo) {
  +            super(aName, aTimeOut, aTempo);
           }
   
           protected void perform(Work work,
                   long startTimeout,
                   ExecutionContext execContext,
                   WorkListener workListener) throws Exception {
  -            m_workManager.doWork(work, startTimeout, execContext, workListener);
  +            workManager.doWork(work, startTimeout, execContext, workListener);
           }
       }
   
  -    public static class DummyStartWork extends AbstractDummyWork {
  -        public DummyStartWork(WorkManager aWorkManager, String aName) {
  -            super(aWorkManager, aName);
  +    public class DummyStartWork extends AbstractDummyWork {
  +        public DummyStartWork(String aName, int aTimeOut, int aTempo) {
  +            super(aName, aTimeOut, aTempo);
           }
   
           protected void perform(Work work,
                   long startTimeout,
                   ExecutionContext execContext,
                   WorkListener workListener) throws Exception {
  -            m_workManager.startWork(work, startTimeout, execContext, workListener);
  +            workManager.startWork(work, startTimeout, execContext, workListener);
           }
       }
   
  -    public static class DummyScheduleWork extends AbstractDummyWork {
  -        public DummyScheduleWork(WorkManager aWorkManager, String aName) {
  -            super(aWorkManager, aName);
  +    public class DummyScheduleWork extends AbstractDummyWork {
  +        public DummyScheduleWork(String aName, int aTimeOut, int aTempo) {
  +            super(aName, aTimeOut, aTempo);
           }
   
           protected void perform(Work work,
                   long startTimeout,
                   ExecutionContext execContext,
                   WorkListener workListener) throws Exception {
  -            m_workManager.scheduleWork(work, startTimeout, execContext, workListener);
  +            workManager.scheduleWork(work, startTimeout, execContext, workListener);
           }
       }
   
       public static class DummyWork implements Work {
  -        private String m_name;
  -
  -        public DummyWork(String aName) {
  -            m_name = aName;
  +        private final String name;
  +        private final int tempo;
  +        
  +        public DummyWork(String aName, int aTempo) {
  +            name = aName;
  +            tempo = aTempo;
           }
   
           public void release() {
  @@ -210,34 +210,37 @@
   
           public void run() {
               try {
  -                Thread.sleep(m_tempo);
  +                Thread.sleep(tempo);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
   
           public String toString() {
  -            return m_name;
  +            return name;
           }
       }
   
       public static class DummyWorkListener implements WorkListener {
  -        public WorkEvent m_event;
  -
  +        public WorkEvent acceptedEvent;
  +        public WorkEvent rejectedEvent;
  +        public WorkEvent startedEvent;
  +        public WorkEvent completedEvent;
  +        
           public void workAccepted(WorkEvent e) {
  -            m_event = e;
  +            acceptedEvent = e;
           }
   
           public void workRejected(WorkEvent e) {
  -            m_event = e;
  +            rejectedEvent = e;
           }
   
           public void workStarted(WorkEvent e) {
  -            m_event = e;
  +            startedEvent = e;
           }
   
           public void workCompleted(WorkEvent e) {
  -            m_event = e;
  +            completedEvent = e;
           }
       }
   }