You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ga...@apache.org on 2008/03/07 20:56:28 UTC
svn commit: r634792 [5/9] - in /geronimo/sandbox/concurrent: ./
concurrent-deployer/ concurrent-deployer/src/ concurrent-deployer/src/main/
concurrent-deployer/src/main/plan/ concurrent/ concurrent/src/
concurrent/src/main/ concurrent/src/main/plan/ ge...
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedRunnable.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedRunnable.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedRunnable.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedRunnable.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,105 @@
+/**
+ * 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.geronimo.concurrent.thread;
+
+import java.util.Map;
+
+import org.apache.geronimo.concurrent.ManagedContext;
+import org.apache.geronimo.concurrent.ManagedTask;
+import org.apache.geronimo.concurrent.ManagedTaskUtils;
+
+public class ManagedRunnable implements Runnable, ManagedTask {
+
+ private Runnable target;
+ private ManagedContext managedContext;
+ private boolean associateTaskWithThread;
+
+ private Thread runner;
+
+ public ManagedRunnable(Runnable target,
+ ManagedContext managedContext) {
+ this(target, managedContext, true);
+ }
+
+ public ManagedRunnable(Runnable target,
+ ManagedContext managedContext,
+ boolean associateTaskWithThread) {
+ this.target = target;
+ this.managedContext = managedContext;
+ this.associateTaskWithThread = associateTaskWithThread;
+ }
+
+ public void run() {
+ if (this.target == null) {
+ return;
+ }
+
+ // set ee context
+ Map<String, Object> threadContext = this.managedContext.set();
+ try {
+ if (this.associateTaskWithThread) {
+ runTasked();
+ } else {
+ runBasic();
+ }
+ } finally {
+ // restore ee context
+ this.managedContext.unset(threadContext);
+ }
+ }
+
+ private void runBasic() {
+ this.target.run();
+ }
+
+ private void runTasked() {
+ Thread thread = Thread.currentThread();
+ if (!(thread instanceof ManagedThread)) {
+ throw new IllegalStateException("Expected ManagedThread thread");
+ }
+
+ // associate task with thread
+ ManagedThread managedThread = (ManagedThread)thread;
+ managedThread.startTask(this);
+ try {
+ this.runner = managedThread;
+ this.target.run();
+ } finally {
+ this.runner = null;
+ // de-associate task with thread
+ managedThread.endTask();
+ }
+ }
+
+ public boolean cancel() {
+ Thread thread = this.runner;
+ if (thread != null) {
+ thread.interrupt();
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ public String getIdentityDescription(String locale) {
+ return ManagedTaskUtils.getTaskDescription(this.target, locale);
+ }
+
+ public String getIdentityName() {
+ return ManagedTaskUtils.getTaskName(this.target);
+ }
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedRunnable.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedRunnable.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedThread.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedThread.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedThread.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedThread.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,216 @@
+/**
+ * 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.geronimo.concurrent.thread;
+
+import java.util.Locale;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.geronimo.concurrent.ManagedTask;
+
+public class ManagedThread extends Thread {
+
+ private final static Log LOG = LogFactory.getLog(ManagedThread.class);
+
+ public enum TaskState {
+ RUNNING, HUNG, CANCELLED, RELEASED, DONE
+ }
+
+ // thread-specific properties
+ protected ThreadStoppedListener threadStoppedListener;
+ protected long hungTaskThreshold;
+
+ // task-specific properties
+ protected String taskIdentityDescription;
+ protected long startTime;
+ protected ManagedTask task;
+ protected TaskState state;
+
+ public ManagedThread(Runnable runnable,
+ ThreadStoppedListener threadStoppedListener) {
+ super(runnable);
+ setThreadStoppedListener(threadStoppedListener);
+ }
+
+ public ManagedThread(ThreadGroup group,
+ Runnable runnable,
+ String name,
+ ThreadStoppedListener threadStoppedListener) {
+ super(group, runnable, name);
+ setThreadStoppedListener(threadStoppedListener);
+ }
+
+ public void setThreadStoppedListener(ThreadStoppedListener threadStoppedListener) {
+ this.threadStoppedListener = threadStoppedListener;
+ }
+
+ /**
+ * This function is assumed to be called on the same thread that is executing
+ * the task and after the right context is associated with the thread.
+ */
+ public void startTask(ManagedTask task) {
+ if (Thread.currentThread() != this) {
+ throw new IllegalStateException("startTask called from invalid thread");
+ }
+ if (this.task != null) {
+ throw new IllegalStateException("Another task is already associated with this thread");
+ }
+
+ this.task = task;
+
+ setStartState();
+
+ this.startTime = System.currentTimeMillis();
+ this.taskIdentityDescription =
+ getTaskIdentityDescription(getThreadLocale().toString());
+
+ LOG.debug("Start task: " + this.taskIdentityDescription);
+ }
+
+ /**
+ * This function is assumed to be called on the same thread as when
+ * {@link #startTask(Object)} was first called.
+ */
+ public void endTask() {
+ if (Thread.currentThread() != this) {
+ throw new IllegalStateException("endTask called from invalid thread");
+ }
+
+ LOG.debug("End task: " + this.taskIdentityDescription);
+
+ setEndState();
+
+ this.task = null;
+ this.startTime = 0;
+ this.taskIdentityDescription = null;
+ }
+
+ public void run() {
+ try {
+ super.run();
+ } finally {
+ this.threadStoppedListener.threadStopped(this);
+ }
+ }
+
+ public long getHungTaskThreshold() {
+ return this.hungTaskThreshold;
+ }
+
+ public void setHungTaskThreshold(long hungTaskThreshold) {
+ if (hungTaskThreshold < 0) {
+ throw new IllegalArgumentException("Threshold must be zero or greater");
+ }
+ this.hungTaskThreshold = hungTaskThreshold;
+ }
+
+ protected Locale getThreadLocale() {
+ return Locale.getDefault();
+ }
+
+ public long getThreadID() {
+ return getId();
+ }
+
+ public String getThreadName() {
+ return getName();
+ }
+
+ public synchronized long getTaskRunTime() {
+ return (isTaskRunning()) ? getRunTime() : 0;
+ }
+
+ protected long getRunTime() {
+ return System.currentTimeMillis() - this.startTime;
+ }
+
+ protected boolean isHung() {
+ if (this.hungTaskThreshold > 0) {
+ return (getRunTime() > this.hungTaskThreshold);
+ } else {
+ return false;
+ }
+ }
+
+ protected synchronized void setState(TaskState newState) {
+ this.state = newState;
+ }
+
+ private synchronized void setStartState() {
+ setState(TaskState.RUNNING);
+ }
+
+ private synchronized void setEndState() {
+ if (this.state == TaskState.RUNNING) {
+ setState(TaskState.DONE);
+ } else if (this.state == TaskState.CANCELLED ||
+ this.state == TaskState.HUNG) {
+ setState(TaskState.RELEASED);
+ }
+ }
+
+ public synchronized boolean isTaskRunning() {
+ return (this.state == TaskState.RUNNING ||
+ this.state == TaskState.HUNG ||
+ this.state == TaskState.CANCELLED);
+ }
+
+ public synchronized boolean isTaskHung() {
+ return (this.state == TaskState.HUNG);
+ }
+
+ public synchronized boolean isTaskCancelled() {
+ return (this.state == TaskState.CANCELLED);
+ }
+
+ /*
+ * Should be called periodically to check if the task is hung
+ */
+ public synchronized void updateState() {
+ if (this.state == TaskState.RUNNING) {
+ if (isHung()) {
+ setState(TaskState.HUNG);
+ }
+ }
+ }
+
+ /**
+ * Cancel hung task.
+ */
+ public synchronized boolean cancelTask() {
+ if (this.state == TaskState.HUNG) {
+ boolean result = this.task.cancel();
+ setState(TaskState.CANCELLED);
+ return result;
+ } else {
+ return false;
+ }
+ }
+
+ public String getTaskIdentityDescription() {
+ return (isTaskRunning()) ? this.taskIdentityDescription : null;
+ }
+
+ public String getTaskIdentityDescription(String localeStr) {
+ return (isTaskRunning()) ? this.task.getIdentityDescription(localeStr) : null;
+ }
+
+ public String getTaskIdentityName() {
+ return (isTaskRunning()) ? this.task.getIdentityName() : null;
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedThread.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedThread.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedThreadFactoryUtils.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedThreadFactoryUtils.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedThreadFactoryUtils.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedThreadFactoryUtils.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,63 @@
+/**
+ * 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.geronimo.concurrent.thread;
+
+import javax.util.concurrent.ManagedThreadFactory;
+
+import org.apache.geronimo.concurrent.ManagedContext;
+import org.apache.geronimo.concurrent.ManagedContextHandler;
+
+public class ManagedThreadFactoryUtils {
+
+ /**
+ * Creates ThreadFactory to be used within component-managed executor service.
+ */
+ public static ManagedThreadFactory createEmbeddedThreadFactory(ManagedThreadFactory factory,
+ ManagedContext managedContext) {
+ // apply context to the thread only
+ return new GenericThreadFactory(factory, managedContext, false);
+ }
+
+ public static ManagedThreadFactory createStandaloneThreadFactory(ManagedThreadFactory factory,
+ ManagedContextHandler contextHandler) {
+ ManagedContext managedContext = ManagedContext.captureContext(contextHandler);
+ // apply context to the thread AND set the right task info on the thread
+ return new GenericThreadFactory(factory, managedContext, true);
+ }
+
+ private static class GenericThreadFactory implements ManagedThreadFactory {
+
+ private ManagedThreadFactory factory;
+ private ManagedContext managedContext;
+ private boolean associateTask;
+
+ public GenericThreadFactory(ManagedThreadFactory factory,
+ ManagedContext managedContext,
+ boolean associateTask) {
+ this.factory = factory;
+ this.managedContext = managedContext;
+ this.associateTask = associateTask;
+ }
+
+ public Thread newThread(Runnable runnable) {
+ runnable = new ManagedRunnable(runnable, this.managedContext, associateTask);
+ return this.factory.newThread(runnable);
+ }
+
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedThreadFactoryUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ManagedThreadFactoryUtils.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ThreadStoppedListener.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ThreadStoppedListener.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ThreadStoppedListener.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ThreadStoppedListener.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,23 @@
+/**
+ * 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.geronimo.concurrent.thread;
+
+public interface ThreadStoppedListener {
+
+ void threadStopped(Thread thread);
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ThreadStoppedListener.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/thread/ThreadStoppedListener.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/ManagedContextHandlerChainTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/ManagedContextHandlerChainTest.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/ManagedContextHandlerChainTest.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/ManagedContextHandlerChainTest.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,132 @@
+/**
+ * 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.geronimo.concurrent;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+public class ManagedContextHandlerChainTest extends TestCase {
+
+ public void testOk() throws Exception {
+ ManagedContextHandlerChain chain = new ManagedContextHandlerChain();
+
+ List calls = new ArrayList();
+
+ chain.addManagedContextHandler(new DummyContextHandler(1, calls));
+ chain.addManagedContextHandler(new DummyContextHandler(2, calls));
+ chain.addManagedContextHandler(new DummyContextHandler(3, calls));
+
+ Map<String, Object> threadContext = new HashMap<String, Object>();
+ chain.saveContext(threadContext);
+ try {
+ chain.setContext(threadContext);
+ } finally {
+ chain.unsetContext(threadContext);
+ }
+
+ assertEquals(Arrays.asList("a1", "a2", "a3", "b1", "b2", "b3", "c3", "c2", "c1"), calls);
+ }
+
+ public void testFailSet() throws Exception {
+ ManagedContextHandlerChain chain = new ManagedContextHandlerChain();
+
+ List calls = new ArrayList();
+
+ chain.addManagedContextHandler(new DummyContextHandler(1, calls));
+ chain.addManagedContextHandler(new DummyContextHandler(2, calls, "b"));
+ chain.addManagedContextHandler(new DummyContextHandler(3, calls));
+
+ Map<String, Object> threadContext = new HashMap<String, Object>();
+ chain.saveContext(threadContext);
+ try {
+ chain.setContext(threadContext);
+ } catch (Exception e) {
+ // ignore exception
+ } finally {
+ chain.unsetContext(threadContext);
+ }
+
+ assertEquals(Arrays.asList("a1", "a2", "a3", "b1", "b2", "c2", "c1"), calls);
+ }
+
+ public void testFailUnset() throws Exception {
+ ManagedContextHandlerChain chain = new ManagedContextHandlerChain();
+
+ List calls = new ArrayList();
+
+ chain.addManagedContextHandler(new DummyContextHandler(1, calls));
+ chain.addManagedContextHandler(new DummyContextHandler(2, calls, "c"));
+ chain.addManagedContextHandler(new DummyContextHandler(3, calls));
+
+ Map<String, Object> threadContext = new HashMap<String, Object>();
+ chain.saveContext(threadContext);
+ try {
+ chain.setContext(threadContext);
+ } finally {
+ chain.unsetContext(threadContext);
+ }
+
+ assertEquals(Arrays.asList("a1", "a2", "a3", "b1", "b2", "b3", "c3", "c2", "c1"), calls);
+ }
+
+ private static class DummyContextHandler implements ManagedContextHandler {
+
+ List calls;
+ int id;
+ String fail;
+
+ public DummyContextHandler(int id, List calls) {
+ this(id, calls, null);
+ }
+
+ public DummyContextHandler(int id, List calls, String fail) {
+ this.id = id;
+ this.calls = calls;
+ this.fail = fail + id;
+ }
+
+ public void saveContext(Map<String, Object> context) {
+ String name = "a" + this.id;
+ this.calls.add(name);
+ if (name.equals(this.fail)) {
+ throw new NullPointerException(name);
+ }
+ }
+
+ public void setContext(Map<String, Object> threadContext) {
+ String name = "b" + this.id;
+ this.calls.add(name);
+ if (name.equals(this.fail)) {
+ throw new NullPointerException(name);
+ }
+ }
+
+ public void unsetContext(Map<String, Object> threadContext) {
+ String name = "c" + this.id;
+ this.calls.add(name);
+ if (name.equals(this.fail)) {
+ throw new NullPointerException(name);
+ }
+ }
+
+ }
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/ManagedContextHandlerChainTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/ManagedContextHandlerChainTest.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/ManagedTaskUtilsTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/ManagedTaskUtilsTest.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/ManagedTaskUtilsTest.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/ManagedTaskUtilsTest.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,54 @@
+/**
+ * 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.geronimo.concurrent;
+
+import java.util.Locale;
+
+import junit.framework.TestCase;
+
+public class ManagedTaskUtilsTest extends TestCase {
+
+ public void testParseLocale() {
+ Locale expected;
+ Locale actual;
+
+ expected = new Locale("en");
+ actual = ManagedTaskUtils.parseLocale("en");
+ assertEquals(expected, actual);
+
+ expected = new Locale("de", "DE");
+ actual = ManagedTaskUtils.parseLocale("de_DE");
+ assertEquals(expected, actual);
+
+ expected = new Locale("", "GB");
+ actual = ManagedTaskUtils.parseLocale("_GB");
+ assertEquals(expected, actual);
+
+ expected = new Locale("en", "US", "WIN");
+ actual = ManagedTaskUtils.parseLocale("en_US_WIN");
+ assertEquals(expected, actual);
+
+ expected = new Locale("de", "", "POSIX");
+ actual = ManagedTaskUtils.parseLocale("de__POSIX");
+ assertEquals(expected, actual);
+
+ expected = new Locale("fr", "", "MAC");
+ actual = ManagedTaskUtils.parseLocale("fr__MAC");
+ assertEquals(expected, actual);
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/ManagedTaskUtilsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/ManagedTaskUtilsTest.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestCallable.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestCallable.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestCallable.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestCallable.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,47 @@
+/**
+ * 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.geronimo.concurrent;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Callable;
+
+public class TestCallable implements Callable<Object> {
+ private List list = new ArrayList();
+ private long delay;
+
+ public TestCallable() {
+ this(0);
+ }
+
+ public TestCallable(long delay) {
+ this.delay = delay;
+ }
+
+ public List getList() {
+ return this.list;
+ }
+
+ public Object call() throws Exception {
+ System.out.println(Thread.currentThread());
+ if (this.delay > 0) {
+ Thread.sleep(this.delay);
+ }
+ list.add(TestContextHandler.getCurrentObject());
+ return this;
+ }
+}
\ No newline at end of file
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestCallable.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestCallable.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestContextHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestContextHandler.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestContextHandler.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestContextHandler.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,63 @@
+/**
+ * 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.geronimo.concurrent;
+
+import java.util.Map;
+
+public class TestContextHandler implements ManagedContextHandler {
+
+ public static final String SKIP =
+ TestContextHandler.class.getName() + ".skip";
+
+ private final static String OLD_OBJECT =
+ TestContextHandler.class.getName() + ".oldObject";
+
+ private final static String NEW_OBJECT =
+ TestContextHandler.class.getName() + ".newObject";
+
+ private static ThreadLocal context = new ThreadLocal();
+
+ public TestContextHandler() {
+ }
+
+ public void saveContext(Map<String, Object> context) {
+ context.put(NEW_OBJECT, getCurrentObject());
+ }
+
+ public void setContext(Map<String, Object> threadContext) {
+ threadContext.put(OLD_OBJECT, getCurrentObject());
+
+ if (Boolean.valueOf((String)threadContext.get(SKIP)).booleanValue()) {
+ setCurrentObject("skipped");
+ } else {
+ setCurrentObject(threadContext.get(NEW_OBJECT));
+ }
+ }
+
+ public void unsetContext(Map<String, Object> threadContext) {
+ setCurrentObject(threadContext.get(OLD_OBJECT));
+ }
+
+ public static Object getCurrentObject() {
+ return context.get();
+ }
+
+ public static void setCurrentObject(Object obj) {
+ context.set(obj);
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestContextHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestContextHandler.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestManagedContextBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestManagedContextBuilder.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestManagedContextBuilder.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestManagedContextBuilder.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,46 @@
+/**
+ * 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.geronimo.concurrent;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.geronimo.concurrent.spi.ManagedContextBuilder;
+
+public class TestManagedContextBuilder extends ManagedContextBuilder {
+
+ public ManagedContext buildManagedContext(ManagedContextHandler contextHandler) {
+ // save context
+ Map<String, Object> capturedContext = new HashMap<String, Object>();
+ contextHandler.saveContext(capturedContext);
+
+ return new TestManagedContext(contextHandler, capturedContext);
+ }
+
+ public static class TestManagedContext extends ManagedContext {
+
+ public TestManagedContext(ManagedContextHandler handler, Map<String, Object> context) {
+ super(handler, context);
+ }
+
+ public boolean isValid() {
+ return true;
+ }
+
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestManagedContextBuilder.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestManagedContextBuilder.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestRunnable.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestRunnable.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestRunnable.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestRunnable.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,35 @@
+/**
+ * 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.geronimo.concurrent;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class TestRunnable implements Runnable {
+
+ private List list = new ArrayList();
+
+ public List getList() {
+ return this.list;
+ }
+
+ public void run() {
+ System.out.println(Thread.currentThread());
+ list.add(TestContextHandler.getCurrentObject());
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestRunnable.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/TestRunnable.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/context/ContextServiceTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/context/ContextServiceTest.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/context/ContextServiceTest.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/context/ContextServiceTest.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,301 @@
+/**
+ * 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.geronimo.concurrent.context;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorCompletionService;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import javax.util.concurrent.ContextService;
+
+import org.apache.geronimo.concurrent.ManagedContextHandler;
+import org.apache.geronimo.concurrent.ManagedContextHandlerChain;
+import org.apache.geronimo.concurrent.TestContextHandler;
+import org.apache.geronimo.concurrent.context.BasicContextService;
+
+import junit.framework.TestCase;
+
+public class ContextServiceTest extends TestCase {
+
+ private ContextService getContextService() {
+ return new TestContextService();
+ }
+
+ public void testProxing() throws Exception {
+ ContextService service = getContextService();
+
+ MyTask task1 = new MyTask();
+
+ Object expected = "foo123bar";
+ TestContextHandler.setCurrentObject(expected);
+
+ Runnable task2 = (Runnable)service.createContextObject(task1, new Class[] {Runnable.class, Callable.class});
+
+ assertEquals(expected, TestContextHandler.getCurrentObject());
+ TestContextHandler.setCurrentObject(null);
+ assertNull(TestContextHandler.getCurrentObject());
+
+ task2.run();
+ task2.equals(task2);
+ task2.hashCode();
+ task2.toString();
+ ((Callable)task2).call();
+
+ assertEquals(Arrays.asList(expected, null, null, null, expected), task1.getList());
+ }
+
+ public void testBadProxyInterfaces() throws Exception {
+ ContextService service = getContextService();
+
+ MyTask task1 = new MyTask();
+
+ try {
+ service.createContextObject(task1, null);
+ fail("Did not throw exception");
+ } catch (IllegalArgumentException e) {
+ // ok
+ }
+
+ try {
+ service.createContextObject(task1, new Class[] {} );
+ fail("Did not throw exception");
+ } catch (IllegalArgumentException e) {
+ // ok
+ }
+
+ try {
+ service.createContextObject(task1, new Class[] {null} );
+ fail("Did not throw exception");
+ } catch (IllegalArgumentException e) {
+ // ok
+ }
+
+ try {
+ service.createContextObject(task1, new Class[] {Serializable.class, null} );
+ fail("Did not throw exception");
+ } catch (IllegalArgumentException e) {
+ // ok
+ }
+
+ try {
+ service.createContextObject(task1, new Class[] {Runnable.class, null} );
+ fail("Did not throw exception");
+ } catch (IllegalArgumentException e) {
+ // ok
+ }
+ }
+
+ /*
+ * Follows the example in the spec.
+ */
+ public void testWithExecutorService() throws Exception {
+ ThreadPoolExecutor threadPoolExecutor =
+ new ThreadPoolExecutor(5, 10, 5, TimeUnit.SECONDS,
+ new ArrayBlockingQueue<Runnable>(10));
+
+ ExecutorCompletionService threadPool =
+ new ExecutorCompletionService(threadPoolExecutor);
+
+ ContextService service = getContextService();
+
+ MyTask task1 = new MyTask();
+
+ String expected = "hello123World";
+ TestContextHandler.setCurrentObject(expected);
+
+ Callable task2 = (Callable)service.createContextObject(task1, new Class[] {Callable.class});
+
+ assertEquals(expected, TestContextHandler.getCurrentObject());
+ TestContextHandler.setCurrentObject(null);
+ assertNull(TestContextHandler.getCurrentObject());
+
+ threadPool.submit(task2);
+
+ assertEquals(expected, threadPool.take().get());
+
+ threadPoolExecutor.shutdown();
+ }
+
+ public void testProxyingProperties1() throws Exception {
+ ContextService service = getContextService();
+
+ MyTask task1 = new MyTask();
+
+ Object expected = "foo123bar";
+ TestContextHandler.setCurrentObject(expected);
+
+ Runnable task2 = (Runnable)service.createContextObject(task1, new Class[] {Runnable.class});
+
+ assertEquals(expected, TestContextHandler.getCurrentObject());
+ TestContextHandler.setCurrentObject(null);
+ assertNull(TestContextHandler.getCurrentObject());
+
+ task2.run();
+
+ assertEquals(Arrays.asList(expected), task1.getList());
+
+ Map<String, String> props = null;
+
+ props = new HashMap<String, String>();
+ props.put(TestContextHandler.SKIP, "true");
+
+ service.setProperties(task2, props);
+
+ task1.getList().clear();
+ task2.run();
+
+ assertEquals(Arrays.asList("skipped"), task1.getList());
+
+ props = new HashMap<String, String>();
+ props.put(TestContextHandler.SKIP, "false");
+
+ service.setProperties(task2, props);
+
+ task1.getList().clear();
+ task2.run();
+
+ assertEquals(Arrays.asList(expected), task1.getList());
+ }
+
+ public void testProxyingProperties2() throws Exception {
+ ContextService service = getContextService();
+
+ Map<String, String> props = null;
+
+ MyTask task1 = new MyTask();
+
+ Object expected = "foo123bar";
+ TestContextHandler.setCurrentObject(expected);
+
+ props = new HashMap<String, String>();
+ props.put(TestContextHandler.SKIP, "true");
+
+ Runnable task2 = (Runnable)service.createContextObject(task1, new Class[] {Runnable.class}, props);
+
+ props = new HashMap<String, String>();
+ props.put(TestContextHandler.SKIP, "false");
+
+ Runnable task3 = (Runnable)service.createContextObject(task1, new Class[] {Runnable.class}, props);
+
+ assertEquals(expected, TestContextHandler.getCurrentObject());
+ TestContextHandler.setCurrentObject(null);
+ assertNull(TestContextHandler.getCurrentObject());
+
+ task1.getList().clear();
+ task2.run();
+ assertEquals(Arrays.asList("skipped"), task1.getList());
+
+ task1.getList().clear();
+ task3.run();
+ assertEquals(Arrays.asList(expected), task1.getList());
+ }
+
+ public void testGetSetProperties() throws Exception {
+ ContextService service = getContextService();
+
+ Map<String, String> props1 = new HashMap<String, String>();
+ props1.put("foo", "bar");
+
+ MyTask task1 = new MyTask();
+ Runnable task2 = (Runnable)service.createContextObject(task1, new Class[] {Runnable.class}, props1);
+
+ assertEquals(props1, service.getProperties(task2));
+
+ Map<String, String> props2 = new HashMap<String, String>();
+ props2.put("hello", "world");
+
+ service.setProperties(task2, props2);
+
+ assertEquals(props2, service.getProperties(task2));
+ }
+
+ public void testBadGetSetProperties() throws Exception {
+ ContextService service = getContextService();
+
+ MyTask task1 = new MyTask();
+
+ // get Properties on non-proxied object
+ try {
+ service.getProperties(task1);
+ fail("did not throw exception");
+ } catch (IllegalArgumentException e) {
+ // ignore
+ }
+
+ // set Properties on non-proxied object
+ try {
+ service.setProperties(task1, new HashMap<String, String>());
+ fail("did not throw exception");
+ } catch (IllegalArgumentException e) {
+ // ignore
+ }
+ }
+
+ private static class TestContextService extends BasicContextService {
+ public TestContextService() {
+ super(createManagedContextHandler());
+ }
+ private static ManagedContextHandler createManagedContextHandler() {
+ ManagedContextHandlerChain chain = new ManagedContextHandlerChain();
+ chain.addManagedContextHandler(new TestContextHandler());
+ return chain;
+ }
+ }
+
+ private static class MyTask implements Runnable, Callable {
+
+ private List list = new ArrayList();
+
+ public List getList() {
+ return this.list;
+ }
+
+ public boolean equals(Object other) {
+ list.add(TestContextHandler.getCurrentObject());
+ return super.equals(other);
+ }
+
+ public String toString() {
+ list.add(TestContextHandler.getCurrentObject());
+ return "MyTask" + super.hashCode();
+ }
+
+ public int hashCode() {
+ list.add(TestContextHandler.getCurrentObject());
+ return super.hashCode();
+ }
+
+ public void run() {
+ list.add(TestContextHandler.getCurrentObject());
+ }
+
+ public Object call() throws Exception {
+ list.add(TestContextHandler.getCurrentObject());
+ return TestContextHandler.getCurrentObject();
+ }
+
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/context/ContextServiceTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/context/ContextServiceTest.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/BasicManagedExecutorServiceTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/BasicManagedExecutorServiceTest.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/BasicManagedExecutorServiceTest.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/BasicManagedExecutorServiceTest.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,411 @@
+/**
+ * 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.geronimo.concurrent.executor;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.Future;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.TimeUnit;
+
+import javax.util.concurrent.ManagedExecutorService;
+
+import junit.framework.TestCase;
+
+import org.apache.geronimo.concurrent.TestCallable;
+import org.apache.geronimo.concurrent.TestContextHandler;
+import org.apache.geronimo.concurrent.TestRunnable;
+
+public abstract class BasicManagedExecutorServiceTest extends TestCase {
+
+ public static final String NOT_EXPECTED = "unexpected data";
+
+ protected ManagedExecutorService service;
+ protected Object expected;
+
+ private static int counter = 0;
+
+ protected static Object setRandomContextData() {
+ Object data = "random data " + counter++;
+ TestContextHandler.setCurrentObject(data);
+ return data;
+ }
+
+ public void tearDown() throws Exception {
+ service.shutdown();
+ }
+
+ public void testExecute() throws Exception {
+ TestRunnable task = new TestRunnable();
+
+ service.execute(task);
+
+ Thread.sleep(1000 * 5);
+
+ assertEquals(Arrays.asList(expected), task.getList());
+ }
+
+ public void testSubmit() throws Exception {
+ // test1
+ TestRunnable task1 = new TestRunnable();
+
+ Future f1 = service.submit(task1);
+
+ assertNull(f1.get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task1.getList());
+
+ // test2
+ TestRunnable task2 = new TestRunnable();
+ Integer value = new Integer(5);
+
+ Future f2 = service.submit(task2, value);
+
+ assertEquals(value, f2.get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task2.getList());
+
+ // test3
+ TestCallable task3 = new TestCallable();
+
+ Future f3 = service.submit(task3);
+
+ assertEquals(task3, f3.get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task3.getList());
+ }
+
+ public void testSubmitWithListener() throws Exception {
+ // test1
+ TestRunnable task1 = new TestRunnable();
+ TestManagedTaskListener listener1 = new TestManagedTaskListener();
+
+ Future f1 = service.submit(task1, listener1);
+
+ assertNull(f1.get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task1.getList());
+ List<TestManagedTaskListener.CallbackInfo> callbacks1 = createCallbackInfo(f1);
+ compareCallbacks(callbacks1, listener1.getCallbacks(f1));
+
+ // test2
+ TestRunnable task2 = new TestRunnable();
+ TestManagedTaskListener listener2 = new TestManagedTaskListener();
+ Integer value = new Integer(5);
+
+ Future f2 = service.submit(task2, value, listener2);
+
+ assertEquals(value, f2.get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task2.getList());
+ List<TestManagedTaskListener.CallbackInfo> callbacks2 = createCallbackInfo(f2);
+ compareCallbacks(callbacks2, listener2.getCallbacks(f2));
+
+ // test3
+ TestCallable task3 = new TestCallable();
+ TestManagedTaskListener listener3 = new TestManagedTaskListener();
+
+ Future f3 = service.submit(task3, listener3);
+
+ assertEquals(task3, f3.get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task3.getList());
+ List<TestManagedTaskListener.CallbackInfo> callbacks3 = createCallbackInfo(f3);
+ compareCallbacks(callbacks3, listener3.getCallbacks(f3));
+ }
+
+ public void testInvokeAll() throws Exception {
+ testInvokeAll(false);
+ }
+
+ public void testInvokeAllWithTimeout() throws Exception {
+ testInvokeAll(true);
+ }
+
+ private void testInvokeAll(boolean withTimeout) throws Exception {
+ TestCallable task1 = new TestCallable();
+ TestCallable task2 = new TestCallable();
+
+ ArrayList<Callable<Object>> tasks = new ArrayList<Callable<Object>>();
+ tasks.add(task1);
+ tasks.add(task2);
+
+ List<Future<Object>> results = null;
+ if (withTimeout) {
+ results = service.invokeAll(tasks, 30, TimeUnit.SECONDS);
+ } else {
+ results = service.invokeAll(tasks);
+ }
+
+ assertEquals(task1, results.get(0).get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task1.getList());
+
+ assertEquals(task2, results.get(1).get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task2.getList());
+ }
+
+ public void testInvokeAllWithListener() throws Exception {
+ testInvokeAllWithListener(false);
+ }
+
+ public void testInvokeAllWithListenerWithTimeout() throws Exception {
+ testInvokeAllWithListener(true);
+ }
+
+ private void testInvokeAllWithListener(boolean withTimeout) throws Exception {
+ TestCallable task1 = new TestCallable();
+ TestCallable task2 = new TestCallable();
+ TestManagedTaskListener listener = new TestManagedTaskListener();
+
+ ArrayList<Callable<Object>> tasks = new ArrayList<Callable<Object>>();
+ tasks.add(task1);
+ tasks.add(task2);
+
+ List<Future<Object>> results = null;
+ if (withTimeout) {
+ results = service.invokeAll(tasks, 30, TimeUnit.SECONDS, listener);
+ } else {
+ results = service.invokeAll(tasks, listener);
+ }
+
+ Future f1 = results.get(0);
+ assertEquals(task1, f1.get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task1.getList());
+ List<TestManagedTaskListener.CallbackInfo> callbacks1 = createCallbackInfo(f1);
+ compareCallbacks(callbacks1, listener.getCallbacks(f1));
+
+ Future f2 = results.get(1);
+ assertEquals(task2, f2.get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task2.getList());
+ List<TestManagedTaskListener.CallbackInfo> callbacks2 = createCallbackInfo(f2);
+ compareCallbacks(callbacks2, listener.getCallbacks(f2));
+ }
+
+ public void testInvokeAny() throws Exception {
+ testInvokeAny(false);
+ }
+
+ public void testInvokeAnyWithTimeout() throws Exception {
+ testInvokeAny(true);
+ }
+
+ private void testInvokeAny(boolean withTimeout) throws Exception {
+ TestCallable task1 = new TestCallable(1000 * 10);
+ TestCallable task2 = new TestCallable(1000 * 5);
+
+ ArrayList<Callable<Object>> tasks = new ArrayList<Callable<Object>>();
+ tasks.add(task1);
+ tasks.add(task2);
+
+ Object result = null;
+ if (withTimeout) {
+ result = service.invokeAny(tasks, 30, TimeUnit.SECONDS);
+ } else {
+ result = service.invokeAny(tasks);
+ }
+
+ // task2 runs faster
+ assertEquals(task2, result);
+ assertEquals(Arrays.asList(expected), task2.getList());
+
+ // task1 should get cancelled so list should be empty
+ assertTrue(task1.getList().isEmpty());
+ }
+
+ public void testInvokeAnyWithListener() throws Exception {
+ // XXX: Not sure how to test it since cannot quite connect listener callbacks
+ // to the Future that represents the given task
+ }
+
+ public abstract void setTestRandomContextData();
+
+ public void testSubmitWithMixedContext() throws Exception {
+ // test1
+ TestRunnable task1 = new TestRunnable();
+ TestManagedTaskListener listener1 = new TestManagedTaskListener();
+
+ setTestRandomContextData();
+ Future f1 = service.submit(task1, listener1);
+
+ assertNull(f1.get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task1.getList());
+ List<TestManagedTaskListener.CallbackInfo> callbacks1 = createCallbackInfo(f1);
+ compareCallbacks(callbacks1, listener1.getCallbacks(f1));
+
+ // test2
+ TestRunnable task2 = new TestRunnable();
+ TestManagedTaskListener listener2 = new TestManagedTaskListener();
+ Integer value = new Integer(5);
+
+ setTestRandomContextData();
+ Future f2 = service.submit(task2, value, listener2);
+
+ assertEquals(value, f2.get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task2.getList());
+ List<TestManagedTaskListener.CallbackInfo> callbacks2 = createCallbackInfo(f2);
+ compareCallbacks(callbacks2, listener2.getCallbacks(f2));
+
+ // test3
+ TestCallable task3 = new TestCallable();
+ TestManagedTaskListener listener3 = new TestManagedTaskListener();
+
+ setTestRandomContextData();
+ Future f3 = service.submit(task3, listener3);
+
+ assertEquals(task3, f3.get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task3.getList());
+ List<TestManagedTaskListener.CallbackInfo> callbacks3 = createCallbackInfo(f3);
+ compareCallbacks(callbacks3, listener3.getCallbacks(f3));
+ }
+
+
+ public void testRejectSubmissionOnShutdown() throws Exception {
+ service.shutdown();
+
+ // test1
+ TestRunnable task1 = new TestRunnable();
+ TestManagedTaskListener listener1 = new TestManagedTaskListener();
+
+ try {
+ service.submit(task1, listener1);
+ fail("Did not throw RejectedExecutionException");
+ } catch (RejectedExecutionException e) {
+ // that's what we expect
+ }
+
+ assertEquals(0, task1.getList().size());
+ checkRejectedListener(listener1.getCallbacks());
+
+ // test2
+ TestRunnable task2 = new TestRunnable();
+ TestManagedTaskListener listener2 = new TestManagedTaskListener();
+ Integer value = new Integer(5);
+
+ try {
+ service.submit(task2, value, listener2);
+ } catch (RejectedExecutionException e) {
+ // that's what we expect
+ }
+
+ assertEquals(0, task2.getList().size());
+ checkRejectedListener(listener2.getCallbacks());
+
+ // test3
+ TestCallable task3 = new TestCallable();
+ TestManagedTaskListener listener3 = new TestManagedTaskListener();
+
+ try {
+ service.submit(task3, listener3);
+ } catch (RejectedExecutionException e) {
+ // that's what we expect
+ }
+
+ assertEquals(0, task3.getList().size());
+ checkRejectedListener(listener3.getCallbacks());
+ }
+
+ private void checkRejectedListener(List<TestManagedTaskListener.CallbackInfo> callbacks) throws Exception {
+ assertEquals(2, callbacks.size());
+
+ TestManagedTaskListener.CallbackInfo callback = null;
+
+ callback = callbacks.get(0);
+ assertEquals(TestManagedTaskListener.Callbacks.SUBMITTED, callback.getCallback());
+ assertEquals(expected, callback.getData());
+ assertEquals(service, callback.getManagedExecutorService());
+
+ callback = callbacks.get(1);
+ assertEquals(TestManagedTaskListener.Callbacks.DONE, callback.getCallback());
+ assertEquals(expected, callback.getData());
+ assertEquals(service, callback.getManagedExecutorService());
+ assertTrue(callback.getException() instanceof RejectedExecutionException);
+ }
+
+ protected void checkListenerBasics(Future expectedFuture,
+ List<TestManagedTaskListener.CallbackInfo> callbacks) throws Exception {
+ for (int i = 0; i < callbacks.size(); i++) {
+ TestManagedTaskListener.CallbackInfo callback = callbacks.get(i);
+
+ assertEquals(expectedFuture, callback.getFuture());
+ assertEquals(service, callback.getManagedExecutorService());
+ assertEquals(expected, callback.getData());
+ }
+ }
+
+ protected void checkData(List list) throws Exception {
+ for (Object data : list) {
+ assertEquals(expected, data);
+ }
+ }
+
+ protected void compareCallbacks(List<TestManagedTaskListener.CallbackInfo> expectedCallbacks,
+ List<TestManagedTaskListener.CallbackInfo> actualCallbacks) throws Exception {
+ System.out.println("Expected");
+ System.out.println(expectedCallbacks);
+ System.out.println("Actual");
+ System.out.println(actualCallbacks);
+
+ assertEquals(expectedCallbacks.size(), actualCallbacks.size());
+ for (int i = 0; i < expectedCallbacks.size(); i++) {
+ TestManagedTaskListener.CallbackInfo expectedCallbackInfo = expectedCallbacks.get(i);
+ TestManagedTaskListener.CallbackInfo actaulCallbackInfo = actualCallbacks.get(i);
+
+ assertEquals(expectedCallbackInfo.getCallback(),
+ actaulCallbackInfo.getCallback());
+ assertEquals(expectedCallbackInfo.getFuture(),
+ actaulCallbackInfo.getFuture());
+ assertEquals(expectedCallbackInfo.getManagedExecutorService(),
+ actaulCallbackInfo.getManagedExecutorService());
+ assertEquals(expectedCallbackInfo.getData(),
+ actaulCallbackInfo.getData());
+ }
+ }
+
+ protected List<TestManagedTaskListener.CallbackInfo> createCallbackInfo(Future future) {
+ return createCallbackInfo(future, 1);
+ }
+
+ protected List<TestManagedTaskListener.CallbackInfo> createCallbackInfo(Future future, int times) {
+ List<TestManagedTaskListener.CallbackInfo> callbacks =
+ new ArrayList<TestManagedTaskListener.CallbackInfo>();
+ for (int i=0;i<times;i++) {
+ addNormalCallbackInfo(future, callbacks);
+ }
+ return callbacks;
+ }
+
+ protected void addNormalCallbackInfo(Future future, List<TestManagedTaskListener.CallbackInfo> callbacks) {
+ callbacks.add(new TestManagedTaskListener.CallbackInfo(TestManagedTaskListener.Callbacks.SUBMITTED, future, service, null, expected));
+ callbacks.add(new TestManagedTaskListener.CallbackInfo(TestManagedTaskListener.Callbacks.STARTING, future, service, null, expected));
+ callbacks.add(new TestManagedTaskListener.CallbackInfo(TestManagedTaskListener.Callbacks.DONE, future, service, null, expected));
+ }
+
+ protected List<TestManagedTaskListener.CallbackInfo> createCancelCallbackInfo(Future future) {
+ List<TestManagedTaskListener.CallbackInfo> callbacks =
+ new ArrayList<TestManagedTaskListener.CallbackInfo>();
+ addCancelCallbackInfo(future, callbacks);
+ return callbacks;
+ }
+
+ protected void addCancelCallbackInfo(Future future, List<TestManagedTaskListener.CallbackInfo> callbacks) {
+ callbacks.add(new TestManagedTaskListener.CallbackInfo(TestManagedTaskListener.Callbacks.SUBMITTED, future, service, null, expected));
+ callbacks.add(new TestManagedTaskListener.CallbackInfo(TestManagedTaskListener.Callbacks.ABORTED, future, service, new CancellationException(), expected));
+ callbacks.add(new TestManagedTaskListener.CallbackInfo(TestManagedTaskListener.Callbacks.DONE, future, service, null, expected));
+ }
+
+ protected void addSkippedCallbackInfo(Future future, List<TestManagedTaskListener.CallbackInfo> callbacks) {
+ callbacks.add(new TestManagedTaskListener.CallbackInfo(TestManagedTaskListener.Callbacks.SUBMITTED, future, service, null, expected));
+ callbacks.add(new TestManagedTaskListener.CallbackInfo(TestManagedTaskListener.Callbacks.DONE, future, service, null, expected));
+ }
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/BasicManagedExecutorServiceTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/BasicManagedExecutorServiceTest.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/BasicManagedScheduledExecutorServiceTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/BasicManagedScheduledExecutorServiceTest.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/BasicManagedScheduledExecutorServiceTest.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/BasicManagedScheduledExecutorServiceTest.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,285 @@
+/**
+ * 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.geronimo.concurrent.executor;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import javax.util.concurrent.ManagedScheduledExecutorService;
+import javax.util.concurrent.SkippedException;
+
+import org.apache.geronimo.concurrent.TestCallable;
+import org.apache.geronimo.concurrent.TestRunnable;
+
+public abstract class BasicManagedScheduledExecutorServiceTest extends BasicManagedExecutorServiceTest {
+
+ protected ManagedScheduledExecutorService scheduledExecutor;
+
+ public void testSchedule() throws Exception {
+ // test schedule Runnable
+ TestRunnable task1 = new TestRunnable();
+
+ Future f1 = scheduledExecutor.schedule(task1, 2, TimeUnit.SECONDS);
+
+ assertNull(f1.get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task1.getList());
+
+ // test schedule Callable
+ TestCallable task2 = new TestCallable();
+
+ Future f2 = scheduledExecutor.schedule(task2, 2, TimeUnit.SECONDS);
+
+ assertEquals(task2, f2.get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task2.getList());
+ }
+
+ public void testScheduleWithListener() throws Exception {
+ // test schedule Runnable
+ TestRunnable task1 = new TestRunnable();
+ TestManagedTaskListener listener1 = new TestManagedTaskListener();
+
+ Future f1 = scheduledExecutor.schedule(task1, 2, TimeUnit.SECONDS, listener1);
+
+ assertNull(f1.get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task1.getList());
+ List<TestManagedTaskListener.CallbackInfo> callbacks1 = createCallbackInfo(f1);
+ compareCallbacks(callbacks1, listener1.getCallbacks(f1));
+
+ // test schedule Callable
+ TestCallable task2 = new TestCallable();
+ TestManagedTaskListener listener2 = new TestManagedTaskListener();
+
+ Future f2 = scheduledExecutor.schedule(task2, 2, TimeUnit.SECONDS, listener2);
+
+ assertEquals(task2, f2.get(5, TimeUnit.SECONDS));
+ assertEquals(Arrays.asList(expected), task2.getList());
+ List<TestManagedTaskListener.CallbackInfo> callbacks2 = createCallbackInfo(f2);
+ compareCallbacks(callbacks2, listener2.getCallbacks(f2));
+ }
+
+ public void testPeriodic() throws Exception {
+ // test scheduleAtFixedRate
+ TestRunnable task1 = new TestRunnable();
+ assertTrue(task1.getList().size() == 0);
+
+ Future f1 = scheduledExecutor.scheduleAtFixedRate(task1, 0, 2, TimeUnit.SECONDS);
+
+ Thread.sleep(1000 * 5);
+ f1.cancel(true);
+
+ assertTrue(task1.getList().size() > 1);
+ checkData(task1.getList());
+
+ // test scheduleWithFixedDelay
+ TestRunnable task2 = new TestRunnable();
+ assertTrue(task2.getList().size() == 0);
+
+ Future f2 = scheduledExecutor.scheduleWithFixedDelay(task2, 0, 2, TimeUnit.SECONDS);
+
+ Thread.sleep(1000 * 5);
+ f2.cancel(true);
+
+ assertTrue(task2.getList().size() > 1);
+ checkData(task2.getList());
+ }
+
+ public void testPeriodicWithListener() throws Exception {
+ // test scheduleAtFixedRate
+ TestRunnable task1 = new TestRunnable();
+ assertTrue(task1.getList().size() == 0);
+ TestManagedTaskListener listener1 = new TestManagedTaskListener();
+
+ Future f1 = scheduledExecutor.scheduleAtFixedRate(task1, 0, 2, TimeUnit.SECONDS, listener1);
+
+ Thread.sleep(1000 * 5);
+ f1.cancel(true);
+
+ assertTrue(task1.getList().size() > 1);
+ checkData(task1.getList());
+ assertTrue(listener1.getCallbacks().size() >= 3);
+ checkListenerBasics(f1, listener1.getCallbacks());
+
+ // test scheduleWithFixedDelay
+ TestRunnable task2 = new TestRunnable();
+ assertTrue(task2.getList().size() == 0);
+ TestManagedTaskListener listener2 = new TestManagedTaskListener();
+
+ Future f2 = scheduledExecutor.scheduleWithFixedDelay(task2, 0, 2, TimeUnit.SECONDS, listener2);
+
+ Thread.sleep(1000 * 5);
+ f2.cancel(true);
+
+ assertTrue(task2.getList().size() > 1);
+ checkData(task2.getList());
+ assertTrue(listener2.getCallbacks().size() >= 3);
+ checkListenerBasics(f2, listener2.getCallbacks());
+ }
+
+ public void testScheduleWithTrigger() throws Exception {
+ // test schedule Runnable
+ TestRunnable task1 = new TestRunnable();
+ TestTrigger trigger1 = new TestTrigger();
+ TestManagedTaskListener listener1 = new TestManagedTaskListener();
+
+ Future f1 = scheduledExecutor.schedule(task1, trigger1, listener1);
+
+ Thread.sleep(1000 * 8);
+ f1.cancel(true);
+
+ assertTrue(task1.getList().size() >= 1);
+ checkData(task1.getList());
+ assertTrue(trigger1.getCallbacks().size() >= 3);
+ checkTriggerBasics(f1, trigger1.getCallbacks());
+ assertTrue(listener1.getCallbacks().size() >= 3);
+ checkListenerBasics(f1, listener1.getCallbacks());
+
+ // test schedule Callable
+ TestCallable task2 = new TestCallable();
+ TestTrigger trigger2 = new TestTrigger();
+ TestManagedTaskListener listener2 = new TestManagedTaskListener();
+
+ Future f2 = scheduledExecutor.schedule(task2, trigger2, listener2);
+
+ Thread.sleep(1000 * 8);
+ f2.cancel(true);
+
+ assertTrue(task2.getList().size() >= 1);
+ checkData(task2.getList());
+ assertTrue(trigger2.getCallbacks().size() >= 3);
+ checkTriggerBasics(f2, trigger2.getCallbacks());
+ assertTrue(listener2.getCallbacks().size() >= 3);
+ checkListenerBasics(f2, listener2.getCallbacks());
+ }
+
+ public void testCancelPeriodicBeforeRun() throws Exception {
+ TestRunnable task = new TestRunnable();
+ TestManagedTaskListener listener = new TestManagedTaskListener();
+
+ Future f1 = scheduledExecutor.scheduleWithFixedDelay(task, 5, 5, TimeUnit.SECONDS, listener);
+
+ Thread.sleep(1000 * 2);
+
+ f1.cancel(true);
+
+ Thread.sleep(1000 * 5);
+
+ List<TestManagedTaskListener.CallbackInfo> callbacks = createCancelCallbackInfo(f1);
+ compareCallbacks(callbacks, listener.getCallbacks());
+ }
+
+ public void testCancelTriggerBeforeRun() throws Exception {
+ TestRunnable task = new TestRunnable();
+ TestTrigger trigger = new TestTrigger();
+ TestManagedTaskListener listener = new TestManagedTaskListener();
+
+ Future f1 = scheduledExecutor.schedule(task, trigger, listener);
+
+ Thread.sleep(1000 * 2);
+
+ f1.cancel(true);
+
+ Thread.sleep(1000 * 5);
+
+ List<TestManagedTaskListener.CallbackInfo> callbacks = createCancelCallbackInfo(f1);
+ compareCallbacks(callbacks, listener.getCallbacks());
+
+ List<TestTrigger.CallbackInfo> triggerCallbacks = trigger.getCallbacks();
+ assertEquals(1, triggerCallbacks.size());
+ TestTrigger.CallbackInfo triggerCallback = triggerCallbacks.get(0);
+ assertEquals(TestTrigger.Callbacks.GET_NEXT_RUN_TIME, triggerCallback.getCallback());
+ assertEquals(f1, triggerCallback.getFuture());
+ assertEquals(expected, triggerCallback.getData());
+ }
+
+ public void testTriggerSkipAndCancel() throws Exception {
+ TestRunnable task = new TestRunnable();
+ TestTrigger trigger = new TestTrigger(3, -1);
+ TestManagedTaskListener listener = new TestManagedTaskListener();
+
+ Future f1 = scheduledExecutor.schedule(task, trigger, listener);
+
+ try {
+ f1.get(30, TimeUnit.SECONDS);
+ fail("Did not throw SkippedException");
+ } catch (SkippedException e) {
+ // that's what we expect
+ }
+
+ assertFalse(f1.isDone());
+ assertFalse(f1.isCancelled());
+
+ f1.cancel(true);
+
+ try {
+ f1.get(30, TimeUnit.SECONDS);
+ } catch (CancellationException e) {
+ // that's what we expect
+ }
+
+ assertTrue(f1.isDone());
+ assertTrue(f1.isCancelled());
+
+ Thread.sleep(1000 * 10);
+
+ List<TestManagedTaskListener.CallbackInfo> callbacks = createCallbackInfo(f1, 2);
+ addSkippedCallbackInfo(f1, callbacks);
+ addCancelCallbackInfo(f1, callbacks);
+ compareCallbacks(callbacks, listener.getCallbacks());
+ }
+
+ /*
+ * The behavior on Future.get() when getNextRunTime() returns null
+ * is not quite defined yet.
+ */
+ /*
+ public void testTriggerSkipAndDone() throws Exception {
+ TestRunnable task = new TestRunnable();
+ TestTrigger trigger = new TestTrigger(3, 5);
+ TestManagedTaskListener listener = new TestManagedTaskListener();
+
+ Future f1 = scheduledExecutor.schedule(task, trigger, listener);
+
+ try {
+ f1.get(30, TimeUnit.SECONDS);
+ fail("Did not throw SkippedException");
+ } catch (SkippedException e) {
+ // that's what we expect
+ }
+
+ assertFalse(f1.isDone());
+ assertFalse(f1.isCancelled());
+
+ Thread.sleep(1000 * 8);
+
+ f1.get(30, TimeUnit.SECONDS);
+ }
+ */
+
+ protected void checkTriggerBasics(Future expectedFuture,
+ List<TestTrigger.CallbackInfo> callbacks) throws Exception {
+ for (int i = 0; i < callbacks.size(); i++) {
+ TestTrigger.CallbackInfo callback = callbacks.get(i);
+
+ assertEquals(expectedFuture, callback.getFuture());
+ assertEquals(expected, callback.getData());
+ }
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/BasicManagedScheduledExecutorServiceTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/BasicManagedScheduledExecutorServiceTest.java
------------------------------------------------------------------------------
svn:executable =
Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/ComponentManagedExecutorServiceTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/ComponentManagedExecutorServiceTest.java?rev=634792&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/ComponentManagedExecutorServiceTest.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/ComponentManagedExecutorServiceTest.java Fri Mar 7 11:56:14 2008
@@ -0,0 +1,62 @@
+/**
+ * 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.geronimo.concurrent.executor;
+
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.TimeUnit;
+
+import javax.util.concurrent.ManagedThreadFactory;
+
+import org.apache.geronimo.concurrent.ManagedContextHandler;
+import org.apache.geronimo.concurrent.ManagedContextHandlerChain;
+import org.apache.geronimo.concurrent.TestContextHandler;
+import org.apache.geronimo.concurrent.executor.ComponentManagedExecutorService;
+import org.apache.geronimo.concurrent.thread.AbstractManagedThreadFactory;
+
+public class ComponentManagedExecutorServiceTest extends BasicManagedExecutorServiceTest {
+
+ public void setUp() throws Exception {
+ expected = setRandomContextData();
+ service = new TestManagedExecutorService();
+
+ Object unexpected = setRandomContextData();
+ assertTrue(expected != unexpected);
+ }
+
+ public void setTestRandomContextData() {
+ setRandomContextData();
+ }
+
+ private static class TestManagedExecutorService extends ComponentManagedExecutorService {
+ public TestManagedExecutorService() {
+ super(2, 2, 60, TimeUnit.SECONDS,
+ new ArrayBlockingQueue<Runnable>(10),
+ getManagedThreadFactory(),
+ getManagedContextHandler());
+ }
+ private static ManagedThreadFactory getManagedThreadFactory() {
+ AbstractManagedThreadFactory factory = new AbstractManagedThreadFactory();
+ return factory;
+ }
+ private static ManagedContextHandler getManagedContextHandler() {
+ ManagedContextHandlerChain chain = new ManagedContextHandlerChain();
+ chain.addManagedContextHandler(new TestContextHandler());
+ return chain;
+ }
+ }
+
+}
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/ComponentManagedExecutorServiceTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/test/java/org/apache/geronimo/concurrent/executor/ComponentManagedExecutorServiceTest.java
------------------------------------------------------------------------------
svn:executable =