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/12 12:02:44 UTC

svn commit: r647411 - in /incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work: JSR237MyFailingRunnable.java JSR237MyRunnable.java JSR237MyRunnerListener.java Jsr237WorkSchedulerTestCase.java

Author: mcombellack
Date: Sat Apr 12 03:02:40 2008
New Revision: 647411

URL: http://svn.apache.org/viewvc?rev=647411&view=rev
Log:
Added unit tests for Jsr237WorkScheduler

Added:
    incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/JSR237MyFailingRunnable.java   (with props)
    incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/JSR237MyRunnable.java   (with props)
    incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/JSR237MyRunnerListener.java   (with props)
    incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/Jsr237WorkSchedulerTestCase.java   (with props)

Added: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/JSR237MyFailingRunnable.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/JSR237MyFailingRunnable.java?rev=647411&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/JSR237MyFailingRunnable.java (added)
+++ incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/JSR237MyFailingRunnable.java Sat Apr 12 03:02:40 2008
@@ -0,0 +1,42 @@
+/*
+ * 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;
+
+/**
+ * Simple Runnable that throws an IllegalArgumentException
+ *
+ * @version $Date$ $Revision$
+ */
+public class JSR237MyFailingRunnable extends JSR237MyRunnable {
+
+    /**
+     * Constructor
+     */
+    public JSR237MyFailingRunnable() {
+        super(-1);
+    }
+
+    /**
+     * Sleeps for a period of time defined by sleepTime
+     */
+    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/JSR237MyFailingRunnable.java
------------------------------------------------------------------------------
    svn:eol-style = native

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

Added: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/JSR237MyRunnable.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/JSR237MyRunnable.java?rev=647411&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/JSR237MyRunnable.java (added)
+++ incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/JSR237MyRunnable.java Sat Apr 12 03:02:40 2008
@@ -0,0 +1,71 @@
+/*
+ * 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;
+
+/**
+ * Simple Runnable that is used for testing Jsr237WorkScheduler
+ *
+ * @version $Date$ $Revision$
+ */
+public class JSR237MyRunnable implements Runnable {
+
+    /**
+     * 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 JSR237MyRunnable(long sleepTime) {
+        this.sleepTime = sleepTime;
+    }
+
+    /**
+     * 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/JSR237MyRunnable.java
------------------------------------------------------------------------------
    svn:eol-style = native

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

Added: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/JSR237MyRunnerListener.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/JSR237MyRunnerListener.java?rev=647411&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/JSR237MyRunnerListener.java (added)
+++ incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/JSR237MyRunnerListener.java Sat Apr 12 03:02:40 2008
@@ -0,0 +1,154 @@
+/*
+ * 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.apache.tuscany.sca.work.NotificationListener;
+
+/**
+ * Simple NotificationListener that is used for testing Jsr237WorkScheduler
+ *
+ * @version $Date$ $Revision$
+ */
+public class JSR237MyRunnerListener implements NotificationListener<JSR237MyRunnable> {
+
+    /**
+     * 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 workFailed() method calls
+     */
+    private AtomicInteger workFailedCallCount = new AtomicInteger();
+
+    /**
+     * Count of workRejected() method calls
+     */
+    private AtomicInteger workRejectedCallCount = new AtomicInteger();
+
+    /**
+     * List of all exceptions thrown by Work items
+     */
+    private List<Throwable> workExceptions = Collections.synchronizedList(new ArrayList<Throwable>());
+
+    /**
+     * {@inheritDoc}
+     */
+    public void workAccepted(JSR237MyRunnable work) {
+        workAcceptedCallCount.incrementAndGet();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void workCompleted(JSR237MyRunnable work) {
+        workCompletedCallCount.incrementAndGet();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void workFailed(JSR237MyRunnable work, Throwable error) {
+        workExceptions.add(error);
+        workFailedCallCount.incrementAndGet();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void workRejected(JSR237MyRunnable work) {
+        workRejectedCallCount.incrementAndGet();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void workStarted(JSR237MyRunnable work) {
+        workStartedCallCount.incrementAndGet();
+    }
+
+    /**
+     * 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 workFailed()
+     *
+     * @return The number of calls to workFailed()
+     */
+    public int getWorkFailedCallCount() {
+        return workFailedCallCount.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<Throwable> getWorkExceptions() {
+        return Collections.unmodifiableList(workExceptions);
+    }
+}

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

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

Added: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/Jsr237WorkSchedulerTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/Jsr237WorkSchedulerTestCase.java?rev=647411&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/Jsr237WorkSchedulerTestCase.java (added)
+++ incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/Jsr237WorkSchedulerTestCase.java Sat Apr 12 03:02:40 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;
+
+/**
+ * Test case for Jsr237WorkScheduler
+ *
+ * @version $Date$ $Revision$
+ */
+public class Jsr237WorkSchedulerTestCase {
+
+    /**
+     * Wait up to 20 seconds for the Work units to complete
+     */
+    private static final long WAIT_TIMEOUT = 20000;
+
+    /**
+     * This is the shared instance of the ThreadPoolWorkManager used by the tests
+     */
+    private static Jsr237WorkScheduler workSchedular = null;
+
+    /**
+     * Setup the Jsr237WorkScheduler
+     */
+    @BeforeClass
+    public static void setup() {
+        workSchedular = new Jsr237WorkScheduler();
+    }
+
+    /**
+     * Make sure that the Jsr237WorkScheduler is stopped after running the tests
+     */
+    @AfterClass
+    public static void destroy() {
+        if (workSchedular != null) {
+            workSchedular.destroy();
+        }
+    }
+
+    /**
+     * Tests running a single fast job on the Jsr237WorkScheduler
+     */
+    @Test
+    public void testSingleFastJob() {
+        // Create the work and register it
+        JSR237MyRunnable fast = new JSR237MyRunnable(10);
+        JSR237MyRunnerListener listener = new JSR237MyRunnerListener();
+        workSchedular.scheduleWork(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 Jsr237WorkScheduler
+     */
+    @Test
+    public void testSingleFailingJob() {
+        // Create the work and register it
+        JSR237MyFailingRunnable fail = new JSR237MyFailingRunnable();
+        JSR237MyRunnerListener listener = new JSR237MyRunnerListener();
+        workSchedular.scheduleWork(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(0, listener.getWorkCompletedCallCount());
+        Assert.assertEquals(1, listener.getWorkFailedCallCount());
+        Assert.assertEquals(1, listener.getWorkExceptions().size());
+    }
+
+    /**
+     * Tests running a mixture of fast and slow jobs on the Jsr237WorkScheduler
+     */
+    @Test
+    public void testMultipleJobs() {
+        // Create the work and register it
+        JSR237MyRunnable fast1 = new JSR237MyRunnable(50);
+        JSR237MyRunnable fast2 = new JSR237MyRunnable(100);
+        JSR237MyRunnable fast3 = new JSR237MyRunnable(200);
+        JSR237MyRunnable slow1= new JSR237MyRunnable(2000);
+        JSR237MyRunnable slow2 = new JSR237MyRunnable(2000);
+        JSR237MyRunnerListener listener = new JSR237MyRunnerListener();
+        workSchedular.scheduleWork(fast1, listener);
+        workSchedular.scheduleWork(fast2, listener);
+        workSchedular.scheduleWork(fast3, listener);
+        workSchedular.scheduleWork(slow1, listener);
+        workSchedular.scheduleWork(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
+     * Jsr237WorkScheduler
+     */
+    @Test
+    public void testMultipleJobsSomeFail() {
+        // Create the work and register it
+        JSR237MyRunnable fast1 = new JSR237MyRunnable(50);
+        JSR237MyRunnable fast2 = new JSR237MyRunnable(100);
+        JSR237MyRunnable fast3 = new JSR237MyRunnable(200);
+        JSR237MyRunnable slow1= new JSR237MyRunnable(2000);
+        JSR237MyRunnable slow2 = new JSR237MyRunnable(2000);
+        JSR237MyFailingRunnable fail1 = new JSR237MyFailingRunnable();
+        JSR237MyFailingRunnable fail2 = new JSR237MyFailingRunnable();
+        JSR237MyRunnerListener listener = new JSR237MyRunnerListener();
+        workSchedular.scheduleWork(fast1, listener);
+        workSchedular.scheduleWork(fast2, listener);
+        workSchedular.scheduleWork(fail1, listener);
+        workSchedular.scheduleWork(fast3, listener);
+        workSchedular.scheduleWork(slow1, listener);
+        workSchedular.scheduleWork(fail2, listener);
+        workSchedular.scheduleWork(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(5, listener.getWorkCompletedCallCount());
+        Assert.assertEquals(2, listener.getWorkFailedCallCount());
+        Assert.assertEquals(2, listener.getWorkExceptions().size());
+    }
+
+    /**
+     * Tests running a single job that has no listener
+     */
+    @Test
+    public void testSingleFastJobWithNoListener() {
+        // Create the work and register it
+        JSR237MyRunnable fast = new JSR237MyRunnable(10);
+        workSchedular.scheduleWork(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);
+            }
+        }
+    }
+
+    /**
+     * Tests scheduling a null as the work item
+     */
+    @Test
+    public void testNullWork() {
+        try {
+            workSchedular.scheduleWork(null);
+            Assert.fail("Should have thrown IllegalArgumentException ");
+        } catch (IllegalArgumentException ex) {
+            // As expected
+            Assert.assertTrue(ex.toString().indexOf("null") != -1);
+        }
+    }
+
+    /**
+     * 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(JSR237MyRunnerListener listener, int completedWorkItemsToWaitFor) {
+        long startTime = System.currentTimeMillis();
+        while (true) {
+            int completedCount = listener.getWorkCompletedCallCount() + listener.getWorkFailedCallCount();
+            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/Jsr237WorkSchedulerTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/core/src/test/java/org/apache/tuscany/sca/core/work/Jsr237WorkSchedulerTestCase.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