directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seelm...@apache.org
Subject svn commit: r1068307 - in /directory/buildtools/trunk/junit-addons/src/main/java/com: ./ mycila/ mycila/junit/ mycila/junit/concurrent/
Date Tue, 08 Feb 2011 09:51:53 GMT
Author: seelmann
Date: Tue Feb  8 09:51:52 2011
New Revision: 1068307

URL: http://svn.apache.org/viewvc?rev=1068307&view=rev
Log:
o Added JUnit concurrent classes from http://mycila.googlecode.com/svn/mycila-junit/tags/mycila-junit-1.0.ga/src/main/java/com
o Removed @Override annotations for Java5 compatibility


Added:
    directory/buildtools/trunk/junit-addons/src/main/java/com/
    directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/
    directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/
    directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/
    directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/Concurrency.java
    directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentException.java
    directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentJunitRunner.java
    directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentRule.java
    directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentRunnerScheduler.java
    directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentSuite.java
    directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/NamedThreadFactory.java

Added: directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/Concurrency.java
URL: http://svn.apache.org/viewvc/directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/Concurrency.java?rev=1068307&view=auto
==============================================================================
--- directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/Concurrency.java
(added)
+++ directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/Concurrency.java
Tue Feb  8 09:51:52 2011
@@ -0,0 +1,37 @@
+/**
+ * Copyright (C) 2010 Mycila <mathieu.carbou@gmail.com>
+ *
+ * Licensed 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 com.mycila.junit.concurrent;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Set the number of threads.
+ * <br>
+ * If 0 or less, the number of thread will be the number of test methdds ot test classes
in the suite.
+ * <br>
+ * If the computation cannot be done, the default thread number will be the number of available
cores.
+ *
+ * @author Mathieu Carbou (mathieu.carbou@gmail.com)
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.TYPE, ElementType.METHOD})
+public @interface Concurrency {
+    int value() default 0;
+}

Added: directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentException.java
URL: http://svn.apache.org/viewvc/directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentException.java?rev=1068307&view=auto
==============================================================================
--- directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentException.java
(added)
+++ directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentException.java
Tue Feb  8 09:51:52 2011
@@ -0,0 +1,42 @@
+/**
+ * Copyright (C) 2010 Mycila <mathieu.carbou@gmail.com>
+ *
+ * Licensed 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 com.mycila.junit.concurrent;
+
+/**
+ * @author Mathieu Carbou (mathieu.carbou@gmail.com)
+ */
+public final class ConcurrentException extends RuntimeException {
+    private ConcurrentException(Throwable cause) {
+        super(cause.getMessage(), cause);
+    }
+
+    public Throwable unwrap() {
+        Throwable t = getCause();
+        while (t instanceof ConcurrentException)
+            t = t.getCause();
+        return t;
+    }
+
+    public static ConcurrentException wrap(Throwable t) {
+        if (t instanceof ConcurrentException)
+            t = ((ConcurrentException) t).unwrap();
+        ConcurrentException concurrentException = new ConcurrentException(t);
+        concurrentException.setStackTrace(t.getStackTrace());
+        return concurrentException;
+    }
+
+}

Added: directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentJunitRunner.java
URL: http://svn.apache.org/viewvc/directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentJunitRunner.java?rev=1068307&view=auto
==============================================================================
--- directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentJunitRunner.java
(added)
+++ directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentJunitRunner.java
Tue Feb  8 09:51:52 2011
@@ -0,0 +1,42 @@
+/**
+ * Copyright (C) 2010 Mycila <mathieu.carbou@gmail.com>
+ *
+ * Licensed 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 com.mycila.junit.concurrent;
+
+import org.junit.Test;
+import org.junit.runners.BlockJUnit4ClassRunner;
+import org.junit.runners.model.InitializationError;
+import org.junit.runners.model.TestClass;
+
+/**
+ * @author Mathieu Carbou (mathieu.carbou@gmail.com)
+ */
+public final class ConcurrentJunitRunner extends BlockJUnit4ClassRunner {
+    public ConcurrentJunitRunner(final Class<?> klass) throws InitializationError {
+        super(klass);
+        int nThreads = 0;
+        if (klass.isAnnotationPresent(Concurrency.class))
+            nThreads = Math.max(0, klass.getAnnotation(Concurrency.class).value());
+        if (nThreads == 0)
+            nThreads = new TestClass(klass).getAnnotatedMethods(Test.class).size();
+        if (nThreads == 0)
+            nThreads = Runtime.getRuntime().availableProcessors();
+        setScheduler(new ConcurrentRunnerScheduler(
+                klass.getSimpleName(),
+                Math.min(Runtime.getRuntime().availableProcessors(), nThreads),
+                nThreads));
+    }
+}
\ No newline at end of file

Added: directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentRule.java
URL: http://svn.apache.org/viewvc/directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentRule.java?rev=1068307&view=auto
==============================================================================
--- directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentRule.java
(added)
+++ directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentRule.java
Tue Feb  8 09:51:52 2011
@@ -0,0 +1,67 @@
+/**
+ * Copyright (C) 2010 Mycila <mathieu.carbou@gmail.com>
+ *
+ * Licensed 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 com.mycila.junit.concurrent;
+
+import org.junit.rules.MethodRule;
+import org.junit.runners.model.FrameworkMethod;
+import org.junit.runners.model.Statement;
+
+import java.util.concurrent.CountDownLatch;
+
+/**
+ * @author Mathieu Carbou (mathieu.carbou@gmail.com)
+ */
+public final class ConcurrentRule implements MethodRule {
+    public Statement apply(final Statement statement, final FrameworkMethod frameworkMethod,
final Object o) {
+        return new Statement() {
+            public void evaluate() throws Throwable {
+                Concurrency concurrency = frameworkMethod.getAnnotation(Concurrency.class);
+                if (concurrency == null)
+                    statement.evaluate();
+                else {
+                    int nThreads = Math.max(0, concurrency.value());
+                    if (nThreads == 0)
+                        nThreads = Runtime.getRuntime().availableProcessors();
+                    ConcurrentRunnerScheduler scheduler = new ConcurrentRunnerScheduler(
+                            o.getClass().getSimpleName() + "." + frameworkMethod.getName(),
+                            nThreads, nThreads);
+                    final CountDownLatch go = new CountDownLatch(1);
+                    Runnable runnable = new Runnable() {
+                        public void run() {
+                            try {
+                                go.await();
+                                statement.evaluate();
+                            } catch (InterruptedException e) {
+                                Thread.currentThread().interrupt();
+                            } catch (Throwable throwable) {
+                                throw ConcurrentException.wrap(throwable);
+                            }
+                        }
+                    };
+                    for (int i = 0; i < nThreads; i++)
+                        scheduler.schedule(runnable);
+                    go.countDown();
+                    try {
+                        scheduler.finished();
+                    } catch (ConcurrentException e) {
+                        throw e.unwrap();
+                    }
+                }
+            }
+        };
+    }
+}

Added: directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentRunnerScheduler.java
URL: http://svn.apache.org/viewvc/directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentRunnerScheduler.java?rev=1068307&view=auto
==============================================================================
--- directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentRunnerScheduler.java
(added)
+++ directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentRunnerScheduler.java
Tue Feb  8 09:51:52 2011
@@ -0,0 +1,73 @@
+/**
+ * Copyright (C) 2010 Mycila <mathieu.carbou@gmail.com>
+ *
+ * Licensed 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 com.mycila.junit.concurrent;
+
+import org.junit.runners.model.RunnerScheduler;
+
+import java.util.LinkedList;
+import java.util.Queue;
+import java.util.concurrent.CompletionService;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorCompletionService;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author Mathieu Carbou (mathieu.carbou@gmail.com)
+ */
+final class ConcurrentRunnerScheduler implements RunnerScheduler {
+
+    private final ExecutorService executorService;
+    private final Queue<Future<Void>> tasks = new LinkedList<Future<Void>>();
+    private final CompletionService<Void> completionService;
+
+    public ConcurrentRunnerScheduler(String name, int nThreadsMin, int nThreadsMax) {
+        executorService = new ThreadPoolExecutor(
+                nThreadsMin, nThreadsMax,
+                10L, TimeUnit.SECONDS,
+                new SynchronousQueue<Runnable>(),
+                new NamedThreadFactory(name),
+                new ThreadPoolExecutor.CallerRunsPolicy());
+        completionService = new ExecutorCompletionService<Void>(executorService);
+    }
+
+    public void schedule(Runnable childStatement) {
+        tasks.offer(completionService.submit(childStatement, null));
+    }
+
+    public void finished() throws ConcurrentException {
+        try {
+            while (!tasks.isEmpty()) {
+                Future<Void> f = completionService.take();
+                tasks.remove(f);
+                f.get();
+            }
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+        } catch (ExecutionException e) {
+            throw ConcurrentException.wrap(e.getCause());
+        } finally {
+            while (!tasks.isEmpty())
+                tasks.poll().cancel(true);
+            executorService.shutdownNow();
+        }
+    }
+
+}

Added: directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentSuite.java
URL: http://svn.apache.org/viewvc/directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentSuite.java?rev=1068307&view=auto
==============================================================================
--- directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentSuite.java
(added)
+++ directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/ConcurrentSuite.java
Tue Feb  8 09:51:52 2011
@@ -0,0 +1,69 @@
+/**
+ * Copyright (C) 2010 Mycila <mathieu.carbou@gmail.com>
+ *
+ * Licensed 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 com.mycila.junit.concurrent;
+
+import org.junit.internal.builders.AllDefaultPossibilitiesBuilder;
+import org.junit.runner.Runner;
+import org.junit.runners.Suite;
+import org.junit.runners.model.InitializationError;
+import org.junit.runners.model.RunnerBuilder;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * @author Mathieu Carbou (mathieu.carbou@gmail.com)
+ */
+public final class ConcurrentSuite extends Suite {
+    public ConcurrentSuite(Class<?> klass) throws InitializationError {
+        super(klass, new AllDefaultPossibilitiesBuilder(true) {
+            @Override
+            public Runner runnerForClass(Class<?> testClass) throws Throwable {
+                List<RunnerBuilder> builders = Arrays.asList(
+                        new RunnerBuilder() {
+                            @Override
+                            public Runner runnerForClass(Class<?> testClass) throws
Throwable {
+                                Concurrency annotation = testClass.getAnnotation(Concurrency.class);
+                                return annotation != null ? new ConcurrentJunitRunner(testClass)
: null;
+                            }
+                        },
+                        ignoredBuilder(),
+                        annotatedBuilder(),
+                        suiteMethodBuilder(),
+                        junit3Builder(),
+                        junit4Builder());
+                for (RunnerBuilder each : builders) {
+                    Runner runner = each.safeRunnerForClass(testClass);
+                    if (runner != null)
+                        return runner;
+                }
+                return null;
+            }
+        });
+        int nThreads = 0;
+        if (klass.isAnnotationPresent(Concurrency.class))
+            nThreads = Math.max(0, klass.getAnnotation(Concurrency.class).value());
+        if (nThreads == 0) {
+            SuiteClasses suiteClasses = klass.getAnnotation(SuiteClasses.class);
+            nThreads = suiteClasses != null ? suiteClasses.value().length : Runtime.getRuntime().availableProcessors();
+        }
+        setScheduler(new ConcurrentRunnerScheduler(
+                klass.getSimpleName(),
+                Math.min(Runtime.getRuntime().availableProcessors(), nThreads),
+                nThreads));
+    }
+}

Added: directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/NamedThreadFactory.java
URL: http://svn.apache.org/viewvc/directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/NamedThreadFactory.java?rev=1068307&view=auto
==============================================================================
--- directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/NamedThreadFactory.java
(added)
+++ directory/buildtools/trunk/junit-addons/src/main/java/com/mycila/junit/concurrent/NamedThreadFactory.java
Tue Feb  8 09:51:52 2011
@@ -0,0 +1,37 @@
+/**
+ * Copyright (C) 2010 Mycila <mathieu.carbou@gmail.com>
+ *
+ * Licensed 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 com.mycila.junit.concurrent;
+
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+* @author Mathieu Carbou (mathieu.carbou@gmail.com)
+*/
+final class NamedThreadFactory implements ThreadFactory {
+    static final AtomicInteger poolNumber = new AtomicInteger(1);
+    final AtomicInteger threadNumber = new AtomicInteger(1);
+    final ThreadGroup group;
+
+    NamedThreadFactory(String poolName) {
+        group = new ThreadGroup(poolName + "-" + poolNumber.getAndIncrement());
+    }
+
+    public Thread newThread(Runnable r) {
+        return new Thread(group, r, group.getName() + "-thread-" + threadNumber.getAndIncrement(),
0);
+    }
+}



Mime
View raw message