incubator-ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r942367 - in /incubator/ace/trunk/ace-scheduler: pom.xml src/test/java/org/apache/ace/scheduler/ src/test/java/org/apache/ace/scheduler/ExecuterTest.java src/test/java/org/apache/ace/scheduler/SchedulerTest.java
Date Sat, 08 May 2010 11:43:40 GMT
Author: marrs
Date: Sat May  8 11:43:39 2010
New Revision: 942367

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

Added:
    incubator/ace/trunk/ace-scheduler/src/test/java/org/apache/ace/scheduler/
    incubator/ace/trunk/ace-scheduler/src/test/java/org/apache/ace/scheduler/ExecuterTest.java
    incubator/ace/trunk/ace-scheduler/src/test/java/org/apache/ace/scheduler/SchedulerTest.java
Modified:
    incubator/ace/trunk/ace-scheduler/pom.xml

Modified: incubator/ace/trunk/ace-scheduler/pom.xml
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-scheduler/pom.xml?rev=942367&r1=942366&r2=942367&view=diff
==============================================================================
--- incubator/ace/trunk/ace-scheduler/pom.xml (original)
+++ incubator/ace/trunk/ace-scheduler/pom.xml Sat May  8 11:43:39 2010
@@ -67,6 +67,10 @@
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.dependencymanager</artifactId>
         </dependency>
+        <dependency>
+            <groupId>${groupId}</groupId>
+            <artifactId>ace-util</artifactId>
+            <version>${version}</version>
+        </dependency>
     </dependencies>
-
 </project>
\ No newline at end of file

Added: incubator/ace/trunk/ace-scheduler/src/test/java/org/apache/ace/scheduler/ExecuterTest.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-scheduler/src/test/java/org/apache/ace/scheduler/ExecuterTest.java?rev=942367&view=auto
==============================================================================
--- incubator/ace/trunk/ace-scheduler/src/test/java/org/apache/ace/scheduler/ExecuterTest.java
(added)
+++ incubator/ace/trunk/ace-scheduler/src/test/java/org/apache/ace/scheduler/ExecuterTest.java
Sat May  8 11:43:39 2010
@@ -0,0 +1,72 @@
+/*
+ * 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.ace.scheduler;
+
+import static org.apache.ace.test.utils.TestUtils.UNIT;
+
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
+
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class ExecuterTest {
+
+    private Semaphore m_sem;
+
+    @BeforeMethod(groups = { UNIT })
+    public void setup() {
+    }
+
+    /* start task, verify if it has run */
+    @Test(groups = { UNIT })
+    public void testExecute() throws Exception {
+        m_sem = new Semaphore(1);
+        Executer executer = new Executer(new Runnable() {
+            @Override
+            public void run() {
+                m_sem.release();
+            }
+        });
+        executer.start(100);
+        m_sem.acquire();
+        assert m_sem.tryAcquire(2, TimeUnit.SECONDS);
+    }
+
+    /* start task, stop it, verify if it executed only once */
+    @Test(groups = { UNIT })
+    public void testStop() throws Exception {
+        m_sem = new Semaphore(2);
+        Executer executer = new Executer(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    m_sem.tryAcquire(1, TimeUnit.SECONDS);
+                }
+                catch (InterruptedException e) {
+                    e.printStackTrace();
+                }
+            }
+        });
+        executer.start(10);
+        executer.stop();
+        Thread.sleep(100);
+        assert m_sem.tryAcquire(1, TimeUnit.SECONDS);
+    }
+}

Added: incubator/ace/trunk/ace-scheduler/src/test/java/org/apache/ace/scheduler/SchedulerTest.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-scheduler/src/test/java/org/apache/ace/scheduler/SchedulerTest.java?rev=942367&view=auto
==============================================================================
--- incubator/ace/trunk/ace-scheduler/src/test/java/org/apache/ace/scheduler/SchedulerTest.java
(added)
+++ incubator/ace/trunk/ace-scheduler/src/test/java/org/apache/ace/scheduler/SchedulerTest.java
Sat May  8 11:43:39 2010
@@ -0,0 +1,146 @@
+/*
+ * 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.ace.scheduler;
+
+import static org.apache.ace.test.utils.TestUtils.UNIT;
+
+import java.util.Properties;
+
+import org.apache.ace.test.utils.TestUtils;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.log.LogService;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class SchedulerTest {
+
+    private Scheduler m_scheduler;
+
+    @BeforeMethod(alwaysRun = true)
+    protected void setUp() throws Exception {
+        m_scheduler = new Scheduler();
+        TestUtils.configureObject(m_scheduler, LogService.class);
+    }
+
+    @Test(groups = { UNIT }, expectedExceptions = IllegalArgumentException.class)
+    public synchronized void testIllegalCreation() {
+        new SchedulerTask(null);
+    }
+
+    @Test(groups = { UNIT })
+    public synchronized void testUpdate() throws Exception {
+        Properties props = new Properties();
+        props.put("local.mock.task1", 1000l);
+        props.put("local.mock.task2", 2000l);
+        props.put("local.mock.task3", 3000l);
+        m_scheduler.updated(props);
+        assert m_scheduler.m_tasks.size() == props.size() : "Exactly three schedules should
be known to the scheduler";
+        assert ((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).getCurrentRecipe().equals(new
Long(1000)) : "The schedule for mock task 1 should specify interval 1000, but it specifies
" + ((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).getCurrentRecipe();
+
+        props.put("local.mock.task1", 4000l);
+        m_scheduler.updated(props);
+        assert ((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).getCurrentRecipe().equals(new
Long(4000)) : "The schedule for mock task 1 should specify interval 4000, but it specifies
" + ((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).getCurrentRecipe();
+        assert !((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).isScheduled()
: "Since we have not provided a runnable for the scheduler, the tasks should not be scheduled.";
+    }
+
+    @Test(groups = { UNIT }, expectedExceptions = ConfigurationException.class)
+    public synchronized void testIllegalUpdate() throws Exception {
+        Properties props = new Properties();
+        props.put("local.mock.task1", "invalidValue");
+        m_scheduler.updated(props);
+        m_scheduler.addRunnable("local.mock.task1", new Runnable() {
+            @Override
+            public void run() {
+            }}, "Dummy testing task", null, false);
+    }
+
+    @Test(groups = { UNIT })
+    public synchronized void testAddTask() throws Exception {
+        assert m_scheduler.m_tasks.isEmpty();
+        m_scheduler.addRunnable("local.mock.task1", new Runnable() {
+            @Override
+            public void run() {
+            }}, "Dummy testing task", null, false);
+        assert m_scheduler.m_tasks.size() == 1 : "Exactly one task should be known to the
scheduler";
+        SchedulerTask task = (SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1");
+        assert "local.mock.task1".equals(task.getName()) : "Task that was just added has
a different name than expected";
+    }
+
+    @Test(groups = { UNIT })
+    public synchronized void testRemoveTask() throws Exception {
+        m_scheduler.addRunnable("local.mock.task1", new Runnable() {
+            @Override
+            public void run() {
+            }}, "Dummy testing task", null, false);
+        m_scheduler.removeRunnable("nonExistent");
+        assert m_scheduler.m_tasks.size() == 1 : "Number of tasks known to the scheduler
should still be one after removing a non-existing task";
+        m_scheduler.removeRunnable("local.mock.task1");
+        assert m_scheduler.m_tasks.isEmpty() : "Number of tasks known to the scheduler should
be zero after removing the task we just added";
+    }
+
+    @Test(groups = { UNIT })
+    public synchronized void testProcessTask() throws Exception {
+        Properties props = new Properties();
+        props.put("local.mock.task1", 1000);
+        m_scheduler.updated(props);
+
+        m_scheduler.addRunnable("local.mock.task1", new Runnable() {
+            @Override
+            public void run() {
+            }}, "Dummy testing task", null, false);
+
+        assert ((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).isScheduled()
: "An executer should exist after adding a matching task and scheduling-recipe";
+    }
+
+    @Test(groups = { UNIT })
+    public synchronized void testSchedulePrevailanceAndRemoval() throws Exception {
+        Properties props = new Properties();
+        props.put("local.mock.task1", 1000l);
+        m_scheduler.updated(props);
+
+        assert ((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).getCurrentRecipe().equals(new
Long(1000)) : "The schedule for mock task 1 should specify interval 1000, but it specifies
" + ((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).getCurrentRecipe();
+        assert !((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).isScheduled()
: "Since we have not provided a runnable for the scheduler, the tasks should not be scheduled.";
+
+        m_scheduler.addRunnable("local.mock.task1", new Runnable() {
+            @Override
+            public void run() {
+            }}, "Dummy testing task", 2000l, true);
+
+        assert ((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).getCurrentRecipe().equals(new
Long(2000)) : "The schedule for mock task 1 should specify interval 2000, but it specifies
" + ((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).getCurrentRecipe();
+        assert ((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).isScheduled()
: "Since we have now provided a runnable for the scheduler, the tasks should be scheduled.";
+
+        m_scheduler.addRunnable("local.mock.task1", new Runnable() {
+            @Override
+            public void run() {
+            }}, "Dummy testing task", 2000l, false);
+
+        assert ((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).getCurrentRecipe().equals(new
Long(1000)) : "The schedule for mock task 1 should specify interval 1000, but it specifies
" + ((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).getCurrentRecipe();
+        assert ((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).isScheduled()
: "Since we have now provided a runnable for the scheduler, the tasks should be scheduled.";
+
+        props = new Properties();
+        m_scheduler.updated(props);
+
+        assert ((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).getCurrentRecipe().equals(new
Long(2000)) : "The schedule for mock task 1 should specify interval 2000, but it specifies
" + ((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).getCurrentRecipe();
+        assert ((SchedulerTask) m_scheduler.m_tasks.get("local.mock.task1")).isScheduled()
: "Since we have now provided a runnable for the scheduler, the tasks should be scheduled.";
+
+        m_scheduler.removeRunnable("local.mock.task1");
+
+        assert m_scheduler.m_tasks.size() == 0 : "We have now removed all information about
mock task 1, so it should be gone now.";
+    }
+}



Mime
View raw message