You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2012/05/16 20:50:31 UTC
[3/44] git commit: Convert TestNG to Spock
Convert TestNG to Spock
Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/ecbc9ed9
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/ecbc9ed9
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/ecbc9ed9
Branch: refs/heads/master
Commit: ecbc9ed9b1955bb8b098edb61a77e4e106a18963
Parents: 45bda7b
Author: Howard M. Lewis Ship <hl...@gmail.com>
Authored: Thu May 10 10:42:51 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Wed May 16 11:50:14 2012 -0700
----------------------------------------------------------------------
.../internal/services/ParallelExecutorSpec.groovy | 104 +++++++++++++++
.../internal/services/ParallelExecutorTest.java | 102 --------------
2 files changed, 104 insertions(+), 102 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/ecbc9ed9/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/services/ParallelExecutorSpec.groovy
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/services/ParallelExecutorSpec.groovy b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/services/ParallelExecutorSpec.groovy
new file mode 100644
index 0000000..4ba10a6
--- /dev/null
+++ b/tapestry-ioc/src/test/groovy/org/apache/tapestry5/ioc/internal/services/ParallelExecutorSpec.groovy
@@ -0,0 +1,104 @@
+package org.apache.tapestry5.ioc.internal.services
+
+import org.apache.tapestry5.ioc.AbstractSharedRegistrySpecification
+import org.apache.tapestry5.ioc.Invokable
+import org.apache.tapestry5.ioc.StringHolder
+import org.apache.tapestry5.ioc.StringHolderImpl
+import org.apache.tapestry5.ioc.services.ParallelExecutor
+import spock.lang.Shared
+
+class ParallelExecutorSpec extends AbstractSharedRegistrySpecification {
+
+ @Shared ParallelExecutor executor
+
+ def setupSpec() {
+ executor = getService ParallelExecutor
+ }
+
+ def "thunks execute in parallel and results are cached"() {
+
+ def thunks = []
+
+ when:
+
+ 100.times { i ->
+
+ def value = "Value[$i]"
+
+ def first = true
+
+ def inv = new Invokable() {
+
+ Object invoke() {
+
+ if (!first) { throw new IllegalStateException("Result of Invokable should be cached.") }
+
+ def holder = new StringHolderImpl()
+
+ holder.value = value
+
+ Thread.sleep 10
+
+ first = false
+
+ return holder
+ }
+ }
+
+ thunks.add executor.invoke(StringHolder, inv)
+ }
+
+ then:
+
+ // Not sure how to truly prove that the results are happening in parallel.
+ // I think it's basically that by the time we work our way though the list, some values
+ // will have been computed ahead.
+
+ thunks.size().times { i ->
+
+ assert thunks[i].value == "Value[$i]"
+ }
+
+ then: "a second pass to proove that the thunk caches the result"
+
+ thunks.size().times { i ->
+
+ assert thunks[i].value == "Value[$i]"
+ }
+ }
+
+ def "toString() of a thunk indicates the interface type"() {
+
+ Invokable inv = Mock()
+
+ when:
+
+ StringHolder thunk = executor.invoke StringHolder, inv
+
+ then:
+
+ thunk.toString() == "FutureThunk[org.apache.tapestry5.ioc.StringHolder]"
+ }
+
+ def "exception inside the Invokable is rethrown by the thunk"() {
+
+ def inv = new Invokable() {
+ Object invoke() { throw new RuntimeException("Future failure!")}
+ }
+
+ StringHolder thunk = executor.invoke StringHolder, inv
+
+
+ when:
+
+ thunk.getValue()
+
+ then:
+
+ RuntimeException e = thrown()
+
+ e.message.contains "Future failure!"
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/ecbc9ed9/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ParallelExecutorTest.java
----------------------------------------------------------------------
diff --git a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ParallelExecutorTest.java b/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ParallelExecutorTest.java
deleted file mode 100644
index 053a7be..0000000
--- a/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/services/ParallelExecutorTest.java
+++ /dev/null
@@ -1,102 +0,0 @@
-// Copyright 2009, 2010 The Apache Software Foundation
-//
-// 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 org.apache.tapestry5.ioc.internal.services;
-
-import org.apache.tapestry5.ioc.Invokable;
-import org.apache.tapestry5.ioc.StringHolder;
-import org.apache.tapestry5.ioc.StringHolderImpl;
-import org.apache.tapestry5.ioc.internal.IOCInternalTestCase;
-import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
-import org.apache.tapestry5.ioc.services.ParallelExecutor;
-import org.testng.annotations.Test;
-
-import java.util.List;
-
-public class ParallelExecutorTest extends IOCInternalTestCase
-{
- @Test
- public void thunk_creation()
- {
- int count = 100;
-
- List<StringHolder> thunks = CollectionFactory.newList();
-
- ParallelExecutor parallelExecutor = getService(ParallelExecutor.class);
-
- for (int i = 0; i < count; i++)
- {
- final String value = String.format("Value[%d]", i);
-
- Invokable<StringHolder> inv = new Invokable<StringHolder>()
- {
- public StringHolder invoke()
- {
- StringHolder holder = new StringHolderImpl();
- holder.setValue(value);
-
- try
- {
- Thread.sleep(100l);
- }
- catch (InterruptedException ie)
- {
- // Swallow
- }
-
- return holder;
- }
- };
-
- thunks.add(parallelExecutor.invoke(StringHolder.class, inv));
- }
-
- for (int j = 0; j < 2; j++)
- {
- for (int i = 0; i < count; i++)
- {
- assertEquals(thunks.get(i).getValue(), String.format("Value[%d]", i));
- }
- }
- }
-
- @Test
- public void exception_thrown_by_invocation()
- {
- ParallelExecutor parallelExecutor = getService(ParallelExecutor.class);
-
- Invokable<StringHolder> inv = new Invokable<StringHolder>()
- {
- public StringHolder invoke()
- {
- throw new RuntimeException("Future failure!");
- }
- };
-
- StringHolder holder = parallelExecutor.invoke(StringHolder.class, inv);
-
- assertEquals(holder.toString(), "FutureThunk[org.apache.tapestry5.ioc.StringHolder]");
-
- try
- {
- holder.getValue();
- unreachable();
- }
- catch (RuntimeException ex)
- {
- assertMessageContains(ex, "Future failure!");
- }
- }
-
-}