You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2014/10/23 19:41:14 UTC

svn commit: r1633899 - in /sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/it: DeprecatedJobsTest.java JobHandlingTest.java

Author: cziegeler
Date: Thu Oct 23 17:41:14 2014
New Revision: 1633899

URL: http://svn.apache.org/r1633899
Log:
SLING-4095 : Clean up code and logging statements

Added:
    sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/it/DeprecatedJobsTest.java   (with props)
Modified:
    sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/it/JobHandlingTest.java

Added: sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/it/DeprecatedJobsTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/it/DeprecatedJobsTest.java?rev=1633899&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/it/DeprecatedJobsTest.java (added)
+++ sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/it/DeprecatedJobsTest.java Thu Oct 23 17:41:14 2014
@@ -0,0 +1,196 @@
+/*
+ * 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.sling.event.it;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Map;
+
+import org.apache.sling.event.impl.Barrier;
+import org.apache.sling.event.impl.jobs.config.ConfigurationConstants;
+import org.apache.sling.event.impl.support.ResourceHelper;
+import org.apache.sling.event.jobs.Job;
+import org.apache.sling.event.jobs.JobManager;
+import org.apache.sling.event.jobs.JobUtil;
+import org.apache.sling.event.jobs.QueueConfiguration;
+import org.apache.sling.event.jobs.consumer.JobConsumer;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerMethod;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventHandler;
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerMethod.class)
+public class DeprecatedJobsTest extends AbstractJobHandlingTest {
+
+    public static final String TOPIC = "sling/test";
+
+    private String queueConfPid;
+
+    @Override
+    @Before
+    public void setup() throws IOException {
+        super.setup();
+
+        // create test queue
+        final org.osgi.service.cm.Configuration config = this.configAdmin.createFactoryConfiguration("org.apache.sling.event.jobs.QueueConfiguration", null);
+        Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put(ConfigurationConstants.PROP_NAME, "test");
+        props.put(ConfigurationConstants.PROP_TYPE, QueueConfiguration.Type.UNORDERED.name());
+        props.put(ConfigurationConstants.PROP_TOPICS, new String[] {TOPIC, TOPIC + "2"});
+        props.put(ConfigurationConstants.PROP_RETRIES, 2);
+        props.put(ConfigurationConstants.PROP_RETRY_DELAY, 2000L);
+        config.update(props);
+
+        this.queueConfPid = config.getPid();
+        this.sleep(1000L);
+    }
+
+    @After
+    public void cleanUp() throws IOException {
+        this.removeConfiguration(this.queueConfPid);
+        super.cleanup();
+    }
+
+    /**
+     * Helper method to create a job event.
+     */
+    private Event getJobEvent(String id) {
+        final Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put(ResourceHelper.PROPERTY_JOB_TOPIC, "sling/test");
+        if ( id != null ) {
+            props.put(JobUtil.PROPERTY_JOB_NAME, id);
+        }
+
+        return new Event(JobUtil.TOPIC_JOB, props);
+    }
+
+    /**
+     * Test simple job execution.
+     * The job is executed once and finished successfully.
+     */
+    @Test(timeout = DEFAULT_TEST_TIMEOUT)
+    public void testSimpleJobExecutionUsingBridge() throws Exception {
+        final Barrier cb = new Barrier(2);
+
+        final ServiceRegistration reg = this.registerEventHandler(TOPIC,
+                new EventHandler() {
+                    @Override
+                    public void handleEvent(Event event) {
+                        JobUtil.acknowledgeJob(event);
+                        JobUtil.finishedJob(event);
+                        cb.block();
+                    }
+
+                 });
+
+        try {
+            this.eventAdmin.sendEvent(getJobEvent(null));
+            assertTrue("No event received in the given time.", cb.block(5));
+            cb.reset();
+            assertFalse("Unexpected event received in the given time.", cb.block(5));
+        } finally {
+            reg.unregister();
+        }
+    }
+
+    /**
+     * Test simple job execution with job id.
+     * The job is executed once and finished successfully.
+     */
+    @Test(timeout = DEFAULT_TEST_TIMEOUT)
+    public void testSimpleJobWithIdExecution() throws Exception {
+        final Barrier cb = new Barrier(2);
+        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
+                new JobConsumer() {
+
+                    @Override
+                    public JobResult process(Job job) {
+                        cb.block();
+                        return JobResult.OK;
+                    }
+                });
+        try {
+            final JobManager jobManager = this.getJobManager();
+            jobManager.addJob(TOPIC, "myid1", null);
+            assertTrue("No event received in the given time.", cb.block(5));
+            cb.reset();
+            assertFalse("Unexpected event received in the given time.", cb.block(5));
+        } finally {
+            jcReg.unregister();
+        }
+    }
+
+    /**
+     * Test force canceling a job
+     * The job execution always fails
+     */
+    @Test(timeout = DEFAULT_TEST_TIMEOUT)
+    public void testForceCancelJob() throws Exception {
+        final Barrier cb = new Barrier(2);
+        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
+                new JobConsumer() {
+
+                    @Override
+                    public JobResult process(Job job) {
+                        cb.block();
+                        sleep(1000);
+                        return JobResult.FAILED;
+                    }
+                });
+        try {
+            final JobManager jobManager = this.getJobManager();
+            jobManager.addJob(TOPIC, "myid3", null);
+            cb.block();
+
+            assertEquals(1, jobManager.findJobs(JobManager.QueryType.ALL, "sling/test", -1, (Map<String, Object>[])null).size());
+            // job is currently sleeping, but force cancel always waits!
+            final Event e = jobManager.findJob("sling/test", Collections.singletonMap(JobUtil.PROPERTY_JOB_NAME, (Object)"myid3"));
+            assertNotNull(e);
+            jobManager.forceRemoveJob((String)e.getProperty(ResourceHelper.PROPERTY_JOB_ID));
+            // the job is now removed
+            assertEquals(0, jobManager.findJobs(JobManager.QueryType.ALL, "sling/test", -1, (Map<String, Object>[])null).size());
+            final Collection<Job> col = jobManager.findJobs(JobManager.QueryType.HISTORY, "sling/test", -1, (Map<String, Object>[])null);
+            try {
+                assertEquals(1, col.size());
+            } finally {
+                for(final Job j : col) {
+                    jobManager.removeJobById(j.getId());
+                }
+            }
+        } finally {
+            jcReg.unregister();
+        }
+    }
+}
\ No newline at end of file

Propchange: sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/it/DeprecatedJobsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/it/DeprecatedJobsTest.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/it/DeprecatedJobsTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/it/JobHandlingTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/it/JobHandlingTest.java?rev=1633899&r1=1633898&r2=1633899&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/it/JobHandlingTest.java (original)
+++ sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/it/JobHandlingTest.java Thu Oct 23 17:41:14 2014
@@ -36,11 +36,8 @@ import java.util.concurrent.atomic.Atomi
 
 import org.apache.sling.event.impl.Barrier;
 import org.apache.sling.event.impl.jobs.config.ConfigurationConstants;
-import org.apache.sling.event.impl.support.ResourceHelper;
 import org.apache.sling.event.jobs.Job;
 import org.apache.sling.event.jobs.JobManager;
-import org.apache.sling.event.jobs.JobProcessor;
-import org.apache.sling.event.jobs.JobUtil;
 import org.apache.sling.event.jobs.NotificationConstants;
 import org.apache.sling.event.jobs.QueueConfiguration;
 import org.apache.sling.event.jobs.consumer.JobConsumer;
@@ -92,48 +89,6 @@ public class JobHandlingTest extends Abs
     }
 
     /**
-     * Helper method to create a job event.
-     */
-    private Event getJobEvent(String id) {
-        final Dictionary<String, Object> props = new Hashtable<String, Object>();
-        props.put(ResourceHelper.PROPERTY_JOB_TOPIC, "sling/test");
-        if ( id != null ) {
-            props.put(JobUtil.PROPERTY_JOB_NAME, id);
-        }
-
-        return new Event(JobUtil.TOPIC_JOB, props);
-    }
-
-    /**
-     * Test simple job execution.
-     * The job is executed once and finished successfully.
-     */
-    @Test(timeout = DEFAULT_TEST_TIMEOUT)
-    public void testSimpleJobExecutionUsingBridge() throws Exception {
-        final Barrier cb = new Barrier(2);
-
-        final ServiceRegistration reg = this.registerEventHandler(TOPIC,
-                new EventHandler() {
-                    @Override
-                    public void handleEvent(Event event) {
-                        JobUtil.acknowledgeJob(event);
-                        JobUtil.finishedJob(event);
-                        cb.block();
-                    }
-
-                 });
-
-        try {
-            this.eventAdmin.sendEvent(getJobEvent(null));
-            assertTrue("No event received in the given time.", cb.block(5));
-            cb.reset();
-            assertFalse("Unexpected event received in the given time.", cb.block(5));
-        } finally {
-            reg.unregister();
-        }
-    }
-
-    /**
      * Test simple job execution.
      * The job is executed once and finished successfully.
      */
@@ -152,7 +107,7 @@ public class JobHandlingTest extends Abs
                  });
 
         try {
-            this.eventAdmin.sendEvent(getJobEvent(null));
+            this.getJobManager().addJob(TOPIC, null);
             assertTrue("No event received in the given time.", cb.block(5));
             cb.reset();
             assertFalse("Unexpected event received in the given time.", cb.block(5));
@@ -180,7 +135,7 @@ public class JobHandlingTest extends Abs
                 });
 
         try {
-            this.eventAdmin.sendEvent(getJobEvent(null));
+            this.getJobManager().addJob(TOPIC, null);
             assertTrue("No event received in the given time.", cb.block(5));
             cb.reset();
             assertFalse("Unexpected event received in the given time.", cb.block(5));
@@ -236,33 +191,6 @@ public class JobHandlingTest extends Abs
     }
 
     /**
-     * Test simple job execution with job id.
-     * The job is executed once and finished successfully.
-     */
-    @Test(timeout = DEFAULT_TEST_TIMEOUT)
-    public void testSimpleJobWithIdExecution() throws Exception {
-        final Barrier cb = new Barrier(2);
-        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
-                new JobConsumer() {
-
-                    @Override
-                    public JobResult process(Job job) {
-                        cb.block();
-                        return JobResult.OK;
-                    }
-                });
-        try {
-            final JobManager jobManager = this.getJobManager();
-            jobManager.addJob(TOPIC, "myid1", null);
-            assertTrue("No event received in the given time.", cb.block(5));
-            cb.reset();
-            assertFalse("Unexpected event received in the given time.", cb.block(5));
-        } finally {
-            jcReg.unregister();
-        }
-    }
-
-    /**
      * Test canceling a job
      * The job execution always fails
      */
@@ -282,24 +210,23 @@ public class JobHandlingTest extends Abs
                 });
         try {
             final JobManager jobManager = this.getJobManager();
-            jobManager.addJob(TOPIC, "myid2", null);
+            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"myid2"));
             cb.block();
 
             assertEquals(1, jobManager.findJobs(JobManager.QueryType.ALL, TOPIC, -1, (Map<String, Object>[])null).size());
             // job is currently waiting, therefore cancel fails
-            final Event e1 = jobManager.findJob(TOPIC, Collections.singletonMap(JobUtil.PROPERTY_JOB_NAME, (Object)"myid2"));
+            final Job e1 = jobManager.getJob(TOPIC, Collections.singletonMap("id", (Object)"myid2"));
             assertNotNull(e1);
-            assertFalse(jobManager.removeJob((String)e1.getProperty(ResourceHelper.PROPERTY_JOB_ID)));
             cb2.block(); // and continue job
 
             sleep(200);
 
             // the job is now in the queue again
-            final Event e2 = jobManager.findJob(TOPIC, Collections.singletonMap(JobUtil.PROPERTY_JOB_NAME, (Object)"myid2"));
+            final Job e2 = jobManager.getJob(TOPIC, Collections.singletonMap("id", (Object)"myid2"));
             assertNotNull(e2);
-            assertTrue(jobManager.removeJob((String)e2.getProperty(ResourceHelper.PROPERTY_JOB_ID)));
-            assertEquals(0, jobManager.findJobs(JobManager.QueryType.ALL, "sling/test", -1, (Map<String, Object>[])null).size());
-            final Collection<Job> col = jobManager.findJobs(JobManager.QueryType.HISTORY, "sling/test", -1, (Map<String, Object>[])null);
+            assertTrue(jobManager.removeJobById(e2.getId()));
+            assertEquals(0, jobManager.findJobs(JobManager.QueryType.ALL, TOPIC, -1, (Map<String, Object>[])null).size());
+            final Collection<Job> col = jobManager.findJobs(JobManager.QueryType.HISTORY, TOPIC, -1, (Map<String, Object>[])null);
             try {
                 assertEquals(1, col.size());
             } finally {
@@ -345,48 +272,6 @@ public class JobHandlingTest extends Abs
     }
 
     /**
-     * Test force canceling a job
-     * The job execution always fails
-     */
-    @Test(timeout = DEFAULT_TEST_TIMEOUT)
-    public void testForceCancelJob() throws Exception {
-        final Barrier cb = new Barrier(2);
-        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
-                new JobConsumer() {
-
-                    @Override
-                    public JobResult process(Job job) {
-                        cb.block();
-                        sleep(1000);
-                        return JobResult.FAILED;
-                    }
-                });
-        try {
-            final JobManager jobManager = this.getJobManager();
-            jobManager.addJob(TOPIC, "myid3", null);
-            cb.block();
-
-            assertEquals(1, jobManager.findJobs(JobManager.QueryType.ALL, "sling/test", -1, (Map<String, Object>[])null).size());
-            // job is currently sleeping, but force cancel always waits!
-            final Event e = jobManager.findJob("sling/test", Collections.singletonMap(JobUtil.PROPERTY_JOB_NAME, (Object)"myid3"));
-            assertNotNull(e);
-            jobManager.forceRemoveJob((String)e.getProperty(ResourceHelper.PROPERTY_JOB_ID));
-            // the job is now removed
-            assertEquals(0, jobManager.findJobs(JobManager.QueryType.ALL, "sling/test", -1, (Map<String, Object>[])null).size());
-            final Collection<Job> col = jobManager.findJobs(JobManager.QueryType.HISTORY, "sling/test", -1, (Map<String, Object>[])null);
-            try {
-                assertEquals(1, col.size());
-            } finally {
-                for(final Job j : col) {
-                    jobManager.removeJobById(j.getId());
-                }
-            }
-        } finally {
-            jcReg.unregister();
-        }
-    }
-
-    /**
      * Reschedule test.
      * The job is rescheduled two times before it fails.
      */
@@ -451,7 +336,7 @@ public class JobHandlingTest extends Abs
                     @Override
                     public JobResult process(Job job) {
                         // events 1 and 4 finish the first time
-                        final String id = job.getName();
+                        final String id = (String)job.getProperty("id");
                         if ( "1".equals(id) || "4".equals(id) ) {
                             return JobResult.OK;
 
@@ -488,7 +373,7 @@ public class JobHandlingTest extends Abs
 
                     @Override
                     public void handleEvent(Event event) {
-                        final String id = (String)event.getProperty(JobUtil.NOTIFICATION_PROPERTY_JOB_NAME);
+                        final String id = (String)event.getProperty("id");
                         cancelled.add(id);
                     }
                 });
@@ -497,7 +382,7 @@ public class JobHandlingTest extends Abs
 
                     @Override
                     public void handleEvent(Event event) {
-                        final String id = (String)event.getProperty(JobUtil.NOTIFICATION_PROPERTY_JOB_NAME);
+                        final String id = (String)event.getProperty("id");
                         failed.add(id);
                     }
                 });
@@ -506,7 +391,7 @@ public class JobHandlingTest extends Abs
 
                     @Override
                     public void handleEvent(Event event) {
-                        final String id = (String)event.getProperty(JobUtil.NOTIFICATION_PROPERTY_JOB_NAME);
+                        final String id = (String)event.getProperty("id");
                         finished.add(id);
                     }
                 });
@@ -515,18 +400,18 @@ public class JobHandlingTest extends Abs
 
                     @Override
                     public void handleEvent(Event event) {
-                        final String id = (String)event.getProperty(JobUtil.NOTIFICATION_PROPERTY_JOB_NAME);
+                        final String id = (String)event.getProperty("id");
                         started.add(id);
                     }
                 });
 
         final JobManager jobManager = this.getJobManager();
         try {
-            jobManager.addJob(TOPIC, "1", null);
-            jobManager.addJob(TOPIC, "2", null);
-            jobManager.addJob(TOPIC, "3", null);
-            jobManager.addJob(TOPIC, "4", null);
-            jobManager.addJob(TOPIC, "5", null);
+            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"1"));
+            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"2"));
+            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"3"));
+            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"4"));
+            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"5"));
 
             int count = 0;
             final long startTime = System.currentTimeMillis();
@@ -560,43 +445,18 @@ public class JobHandlingTest extends Abs
     @Test(timeout = DEFAULT_TEST_TIMEOUT)
     public void testNoJobProcessor() throws Exception {
         final AtomicInteger count = new AtomicInteger(0);
-        final AtomicInteger unprocessedCount = new AtomicInteger(0);
-
-        final ServiceRegistration eh1 = this.registerEventHandler(TOPIC,
-                new EventHandler() {
 
-                    @Override
-                    public void handleEvent(Event event) {
-                        JobUtil.processJob(event, new JobProcessor() {
+        final ServiceRegistration eh1 = this.registerJobConsumer(TOPIC,
+                new JobConsumer() {
 
-                            @Override
-                            public boolean process(Event job) {
-                                try {
-                                    Thread.sleep(200);
-                                } catch (InterruptedException ie) {
-                                    // ignore
-                                }
-                                return true;
-                            }
-                        });
-                    }
-                });
-        final ServiceRegistration eh2 = this.registerEventHandler("sling/test2",
-                new EventHandler() {
+            @Override
+            public JobResult process(final Job job) {
+                count.incrementAndGet();
 
-                    @Override
-                    public void handleEvent(Event event) {
-                        unprocessedCount.incrementAndGet();
-                    }
-                });
-        final ServiceRegistration eh3 = this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED,
-                new EventHandler() {
+                return JobResult.OK;
+            }
+         });
 
-                    @Override
-                    public void handleEvent(Event event) {
-                        count.incrementAndGet();
-                    }
-                });
         try {
             final JobManager jobManager = this.getJobManager();
 
@@ -604,44 +464,20 @@ public class JobHandlingTest extends Abs
             final int COUNT = 20;
             for(int i = 0; i < COUNT; i++ ) {
                 final String jobTopic = (i % 2 == 0 ? TOPIC : TOPIC + "2");
-                final Dictionary<String, Object> props = new Hashtable<String, Object>();
-                props.put(ResourceHelper.PROPERTY_JOB_TOPIC, jobTopic);
 
-                this.eventAdmin.postEvent(new Event(JobUtil.TOPIC_JOB, props));
+                jobManager.addJob(jobTopic, null);
             }
-            final long startTime = System.currentTimeMillis();
             while ( count.get() < COUNT / 2) {
                 try {
-                    Thread.sleep(500);
-                } catch (InterruptedException ie) {
-                    // ignore
-                }
-            }
-            while ( unprocessedCount.get() < COUNT / 2) {
-                try {
-                    Thread.sleep(500);
-                } catch (InterruptedException ie) {
-                    // ignore
-                }
-            }
-            // clean up waits for one minute, so we should do the same
-            while ( System.currentTimeMillis() - startTime < 72000 ) {
-                try {
                     Thread.sleep(100);
                 } catch (InterruptedException ie) {
                     // ignore
                 }
             }
-            ((Runnable)jobManager).run();
-            while ( unprocessedCount.get() < COUNT ) {
-                try {
-                    Thread.sleep(500);
-                } catch (InterruptedException ie) {
-                    // ignore
-                }
-            }
+
             assertEquals("Finished count", COUNT / 2, count.get());
-            assertEquals("Unprocessed count",COUNT, unprocessedCount.get());
+            // unprocessed count should be 0 as there is no job consumer for this job
+            assertEquals("Unprocessed count", 0, jobManager.getStatistics().getNumberOfJobs());
             assertEquals("Finished count", COUNT / 2, jobManager.getStatistics().getNumberOfFinishedJobs());
 
             // now remove jobs
@@ -650,8 +486,6 @@ public class JobHandlingTest extends Abs
             }
         } finally {
             eh1.unregister();
-            eh2.unregister();
-            eh3.unregister();
         }
     }
 }
\ No newline at end of file