You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by mc...@apache.org on 2008/04/11 21:26:16 UTC

svn commit: r647273 - in /incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work: ./ FailingWork.java TestWorkListener.java ThreadPoolWorkManagerTestCase.java TimeDelayWork.java

Author: mcombellack
Date: Fri Apr 11 12:26:13 2008
New Revision: 647273

URL: http://svn.apache.org/viewvc?rev=647273&view=rev
Log:
Added unit test cases for ThreadPoolWorkManager

Added:
    incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/
    incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/FailingWork.java   (with props)
    incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TestWorkListener.java   (with props)
    incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/ThreadPoolWorkManagerTestCase.java   (with props)
    incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TimeDelayWork.java   (with props)

Added: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/FailingWork.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/FailingWork.java?rev=647273&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/FailingWork.java (added)
+++ incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/FailingWork.java Fri Apr 11 12:26:13 2008
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.tuscany.sca.core.work;
+
+import commonj.work.Work;
+
+/**
+ * Simple Work item that will throw an exception
+ * 
+ * @version $Date$ $Revision$
+ */
+public class FailingWork implements Work {
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isDaemon() {
+        return false;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void release() {
+    }
+
+    /**
+     * Throws an IllegalArgumentException
+     */
+    public void run() {
+        System.out.println("Starting " + this + " and throwing an Exception");
+        throw new IllegalArgumentException("Sample exception from " + this);
+    }
+}

Propchange: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/FailingWork.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/FailingWork.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TestWorkListener.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TestWorkListener.java?rev=647273&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TestWorkListener.java (added)
+++ incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TestWorkListener.java Fri Apr 11 12:26:13 2008
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.tuscany.sca.core.work;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.Assert;
+
+import commonj.work.WorkEvent;
+import commonj.work.WorkException;
+import commonj.work.WorkListener;
+
+/**
+ * A simple WorkListener that tracks invocations to it.
+ * 
+ * @version $Date $Revision$
+ */
+public class TestWorkListener implements WorkListener {
+
+    /**
+     * Count of workAccepted() method calls
+     */
+    private AtomicInteger workAcceptedCallCount = new AtomicInteger();
+
+    /**
+     * Count of workStarted() method calls
+     */
+    private AtomicInteger workStartedCallCount = new AtomicInteger();
+
+    /**
+     * Count of workCompleted() method calls
+     */
+    private AtomicInteger workCompletedCallCount = new AtomicInteger();
+
+    /**
+     * Count of workRejected() method calls
+     */
+    private AtomicInteger workRejectedCallCount = new AtomicInteger();
+
+    /**
+     * List of all exceptions thrown by Work items
+     */
+    private List<WorkException> workExceptions = Collections.synchronizedList(new ArrayList<WorkException>());
+
+    /**
+     * {@inheritDoc}
+     */
+    public void workAccepted(WorkEvent work) {
+        workAcceptedCallCount.incrementAndGet();
+
+        // Validate the WorkEvent
+        Assert.assertNotNull(work.getWorkItem());
+        Assert.assertEquals(WorkEvent.WORK_ACCEPTED, work.getType());
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void workStarted(WorkEvent work) {
+        workStartedCallCount.incrementAndGet();
+
+        // Validate the WorkEvent
+        Assert.assertNotNull(work.getWorkItem());
+        Assert.assertEquals(WorkEvent.WORK_STARTED, work.getType());
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void workCompleted(WorkEvent work) {
+        if (work.getException() != null) {
+            workExceptions.add(work.getException());
+        }
+
+        // Validate the WorkEvent
+        Assert.assertNotNull(work.getWorkItem());
+        Assert.assertEquals(WorkEvent.WORK_COMPLETED, work.getType());
+
+        workCompletedCallCount.incrementAndGet();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void workRejected(WorkEvent work) {
+        workRejectedCallCount.incrementAndGet();
+
+        // Validate the WorkEvent
+        Assert.assertNotNull(work.getWorkItem());
+        Assert.assertEquals(WorkEvent.WORK_REJECTED, work.getType());
+    }
+
+    /**
+     * Returns the number of calls to workAccepted()
+     * 
+     * @return The number of calls to workAccepted()
+     */
+    public int getWorkAcceptedCallCount() {
+        return workAcceptedCallCount.get();
+    }
+
+    /**
+     * Returns the number of calls to workStarted()
+     * 
+     * @return The number of calls to workStarted()
+     */
+    public int getWorkStartedCallCount() {
+        return workStartedCallCount.get();
+    }
+
+    /**
+     * Returns the number of calls to workCompleted()
+     * 
+     * @return The number of calls to workCompleted()
+     */
+    public int getWorkCompletedCallCount() {
+        return workCompletedCallCount.get();
+    }
+
+    /**
+     * Returns the number of calls to workRejected()
+     * 
+     * @return The number of calls to workRejected()
+     */
+    public int getWorkRejectedCallCount() {
+        return workRejectedCallCount.get();
+    }
+
+    /**
+     * Returns a List of all exceptions that are thrown by the Work items
+     * 
+     * @return A List of all exceptions that are thrown by the Work items
+     */
+    public List<WorkException> getWorkExceptions() {
+        return Collections.unmodifiableList(workExceptions);
+    }
+}

Propchange: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TestWorkListener.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TestWorkListener.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/ThreadPoolWorkManagerTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/ThreadPoolWorkManagerTestCase.java?rev=647273&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/ThreadPoolWorkManagerTestCase.java (added)
+++ incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/ThreadPoolWorkManagerTestCase.java Fri Apr 11 12:26:13 2008
@@ -0,0 +1,240 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.tuscany.sca.core.work;
+
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * This test case will test the ThreadPoolWorkManager
+ * 
+ * @version $Date$ $Revision$
+ */
+public class ThreadPoolWorkManagerTestCase {
+
+    /**
+     * Wait up to 20 seconds for the Work units to complete
+     */
+    private static final long WAIT_TIMEOUT = 20000 + 100000000;
+
+    /**
+     * This is the shared instance of the ThreadPoolWorkManager used by the tests
+     */
+    private static ThreadPoolWorkManager workManager = null;
+
+    /**
+     * Setup the ThreadPoolWorkManager
+     */
+    @BeforeClass
+    public static void setup() {
+        workManager = new ThreadPoolWorkManager(10);
+    }
+
+    /**
+     * Make sure that the ThreadPoolWorkManager is stopped after running the tests
+     */
+    @AfterClass
+    public static void destroy() {
+        if (workManager != null) {
+            workManager.destroy();
+        }
+    }
+
+    /**
+     * Tests running a single fast job on the ThreadPoolWorkManager
+     */
+    @Test
+    public void testSingleFastJob() {
+        // Create the work and register it
+        TimeDelayWork fast = new TimeDelayWork(10);
+        TestWorkListener listener = new TestWorkListener();
+        workManager.schedule(fast, listener);
+
+        // Wait for the 1 job to complete
+        waitForWorkToComplete(listener, 1);
+
+        // Test that the job completed successfully.
+        Assert.assertEquals(1, listener.getWorkAcceptedCallCount());
+        Assert.assertEquals(0, listener.getWorkRejectedCallCount());
+        Assert.assertEquals(1, listener.getWorkStartedCallCount());
+        Assert.assertEquals(1, listener.getWorkCompletedCallCount());
+        Assert.assertEquals(0, listener.getWorkExceptions().size());
+    }
+
+    /**
+     * Tests running a single job that fails on the ThreadPoolWorkManager
+     */
+    @Test
+    public void testSingleFailingJob() {
+        // Create the work and register it
+        FailingWork fail = new FailingWork();
+        TestWorkListener listener = new TestWorkListener();
+        workManager.schedule(fail, listener);
+
+        // Wait for the 1 job to complete
+        waitForWorkToComplete(listener, 1);
+
+        // Test that the job completed successfully.
+        Assert.assertEquals(1, listener.getWorkAcceptedCallCount());
+        Assert.assertEquals(0, listener.getWorkRejectedCallCount());
+        Assert.assertEquals(1, listener.getWorkStartedCallCount());
+        Assert.assertEquals(1, listener.getWorkCompletedCallCount());
+        Assert.assertEquals(1, listener.getWorkExceptions().size());
+    }
+
+    /**
+     * Tests running a mixture of fast and slow jobs on the ThreadPoolWorkManager
+     */
+    @Test
+    public void testMultipleJobs() {
+        // Create the work and register it
+        TimeDelayWork fast1 = new TimeDelayWork(50);
+        TimeDelayWork fast2 = new TimeDelayWork(100);
+        TimeDelayWork fast3 = new TimeDelayWork(200);
+        TimeDelayWork slow1= new TimeDelayWork(2000);
+        TimeDelayWork slow2 = new TimeDelayWork(2000);
+        TestWorkListener listener = new TestWorkListener();
+        workManager.schedule(fast1, listener);
+        workManager.schedule(fast2, listener);
+        workManager.schedule(fast3, listener);
+        workManager.schedule(slow1, listener);
+        workManager.schedule(slow2, listener);
+
+        // Wait for the 5 jobs to complete
+        waitForWorkToComplete(listener, 5);
+
+        // Test that the job completed successfully.
+        Assert.assertEquals(5, listener.getWorkAcceptedCallCount());
+        Assert.assertEquals(0, listener.getWorkRejectedCallCount());
+        Assert.assertEquals(5, listener.getWorkStartedCallCount());
+        Assert.assertEquals(5, listener.getWorkCompletedCallCount());
+        Assert.assertEquals(0, listener.getWorkExceptions().size());
+    }
+
+    /**
+     * Tests running a mixture of fast and slow jobs some of which fail on the 
+     * ThreadPoolWorkManager
+     */
+    @Test
+    public void testMultipleJobsSomeFail() {
+        // Create the work and register it
+        TimeDelayWork fast1 = new TimeDelayWork(50);
+        TimeDelayWork fast2 = new TimeDelayWork(100);
+        TimeDelayWork fast3 = new TimeDelayWork(200);
+        TimeDelayWork slow1= new TimeDelayWork(2000);
+        TimeDelayWork slow2 = new TimeDelayWork(2000);
+        FailingWork fail1 = new FailingWork();
+        FailingWork fail2 = new FailingWork();
+        TestWorkListener listener = new TestWorkListener();
+        workManager.schedule(fast1, listener);
+        workManager.schedule(fast2, listener);
+        workManager.schedule(fail1, listener);
+        workManager.schedule(fast3, listener);
+        workManager.schedule(slow1, listener);
+        workManager.schedule(fail2, listener);
+        workManager.schedule(slow2, listener);
+
+        // Wait for the 7 jobs to complete
+        waitForWorkToComplete(listener, 7);
+
+        // Test that the job completed successfully.
+        Assert.assertEquals(7, listener.getWorkAcceptedCallCount());
+        Assert.assertEquals(0, listener.getWorkRejectedCallCount());
+        Assert.assertEquals(7, listener.getWorkStartedCallCount());
+        Assert.assertEquals(7, listener.getWorkCompletedCallCount());
+        Assert.assertEquals(2, listener.getWorkExceptions().size());
+    }
+
+    /**
+     * Tests creating a ThreadPoolWorkManager with invalid pool sizes of -10 to 0
+     * inclusive
+     */
+    @Test
+    public void testThreadPoolWorkManagerLessThan1Size() {
+        for (int i = 0; i >= -10; i--) {
+            try {
+                new ThreadPoolWorkManager(i);
+                Assert.fail("Should have thrown IllegalArgumentException");
+            } catch (IllegalArgumentException ex) {
+                Assert.assertTrue(ex.toString().indexOf(Integer.toString(i)) != -1);
+            }
+        }
+    }
+
+    
+    @Test
+    public void testSingleFastJobWithNoListener() {
+        // Create the work and register it
+        TimeDelayWork fast = new TimeDelayWork(10);
+        workManager.schedule(fast);
+
+        // Wait for the job to complete
+        long startTime = System.currentTimeMillis();
+        while (true) {
+            int completedCount = fast.getRunCompletedCount();
+            if (completedCount == 1) {
+                break;
+            }
+
+            if (System.currentTimeMillis() - startTime > WAIT_TIMEOUT) {
+                Assert.fail("Only " + completedCount + " work items completed before timeout");
+                return;
+            }
+
+            // Lets wait for the job to complete
+            try {
+                Thread.sleep(25);
+            } catch (InterruptedException ex) {
+                Assert.fail("Unexpected exception: " + ex);
+            }
+        }
+
+        // Make sure we have got one completed run
+        Assert.assertEquals(1, fast.getRunCompletedCount());
+    }
+    /**
+     * Waits for the specified number of jobs to complete or the timeout to fire.
+     * 
+     * @param listener The listener to use to track Work unit completion
+     * @param completedWorkItemsToWaitFor The number of Work items to complete
+     */
+    private void waitForWorkToComplete(TestWorkListener listener, int completedWorkItemsToWaitFor) {
+        long startTime = System.currentTimeMillis();
+        while (true) {
+            int completedCount = listener.getWorkCompletedCallCount();
+            if (completedCount == completedWorkItemsToWaitFor) {
+                return;
+            }
+
+            if (System.currentTimeMillis() - startTime > WAIT_TIMEOUT) {
+                Assert.fail("Only " + completedCount + " work items completed before timeout");
+                return;
+            }
+
+            // Lets wait for more jobs to complete
+            try {
+                Thread.sleep(25);
+            } catch (InterruptedException ex) {
+                Assert.fail("Unexpected exception: " + ex);
+            }
+        }
+    }
+}

Propchange: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/ThreadPoolWorkManagerTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/ThreadPoolWorkManagerTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TimeDelayWork.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TimeDelayWork.java?rev=647273&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TimeDelayWork.java (added)
+++ incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TimeDelayWork.java Fri Apr 11 12:26:13 2008
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.tuscany.sca.core.work;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import commonj.work.Work;
+
+/**
+ * Simple Work item that will sleep in the run() method for the specified 
+ * period of time
+ * 
+ * @version $Date$ $Revision$
+ */
+public class TimeDelayWork implements Work {
+
+    /**
+     * Count of workAccepted() method calls
+     */
+    private AtomicInteger runCompletedCount = new AtomicInteger();
+
+    /**
+     * The amount of time to sleep in the Run loop
+     */
+    private final long sleepTime;
+
+    /**
+     * Constructor
+     * 
+     * @param sleepTime The amount of time to sleep (in milliseconds) in the run() method
+     */
+    public TimeDelayWork(long sleepTime) {
+        this.sleepTime = sleepTime;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isDaemon() {
+        return false;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void release() {
+    }
+
+    /**
+     * Sleeps for a period of time defined by sleepTime
+     */
+    public void run() {
+        System.out.println("Starting " + this);
+        try {
+            Thread.sleep(sleepTime);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        System.out.println("Done " + this);
+        runCompletedCount.incrementAndGet();
+    }
+
+    /**
+     * Returns the number of completed calls to run()
+     * 
+     * @return The number of completed calls to run()
+     */
+    public int getRunCompletedCount() {
+        return runCompletedCount.get();
+    }
+}

Propchange: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TimeDelayWork.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/TimeDelayWork.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org