Remove sleep loop to improve test reliability and timeliness
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/f7be98a0 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/f7be98a0 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/f7be98a0 Branch: refs/heads/master Commit: f7be98a084cf3d6b8a82cae05fa1ec32f7ed7029 Parents: efee13b Author: John Wagenleitner <[email protected]> Authored: Sun Jan 31 07:57:41 2016 -0800 Committer: John Wagenleitner <[email protected]> Committed: Sun Jan 31 07:57:41 2016 -0800 ---------------------------------------------------------------------- src/test/groovy/GroovyClosureMethodsTest.groovy | 15 ++++++++------- src/test/groovy/ThreadMethodsTest.groovy | 19 +++++++++++-------- 2 files changed, 19 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/f7be98a0/src/test/groovy/GroovyClosureMethodsTest.groovy ---------------------------------------------------------------------- diff --git a/src/test/groovy/GroovyClosureMethodsTest.groovy b/src/test/groovy/GroovyClosureMethodsTest.groovy index 43afac4..5b83305 100644 --- a/src/test/groovy/GroovyClosureMethodsTest.groovy +++ b/src/test/groovy/GroovyClosureMethodsTest.groovy @@ -18,6 +18,9 @@ */ package groovy +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit + /** * Test case for the eachObject method on a file containing * zero, one or more objects (object stream). Also test cases @@ -161,15 +164,13 @@ class GroovyClosureMethodsTest extends GroovyTestCase { } void testRunAfter() { - boolean modifiedByRunAfter = false + CountDownLatch latch = new CountDownLatch(1) new Timer().runAfter(50) { - modifiedByRunAfter = true - } - assert modifiedByRunAfter == false - for(int i = 0; !modifiedByRunAfter && i < 10; i++) { - Thread.sleep 100 + latch.countDown() } - assert modifiedByRunAfter + assert latch.getCount() == 1 + latch.await(100L, TimeUnit.MILLISECONDS) + assert latch.getCount() == 0 } void testSplitEachLine() { http://git-wip-us.apache.org/repos/asf/groovy/blob/f7be98a0/src/test/groovy/ThreadMethodsTest.groovy ---------------------------------------------------------------------- diff --git a/src/test/groovy/ThreadMethodsTest.groovy b/src/test/groovy/ThreadMethodsTest.groovy index e433ccd..a19a6c5 100644 --- a/src/test/groovy/ThreadMethodsTest.groovy +++ b/src/test/groovy/ThreadMethodsTest.groovy @@ -18,18 +18,21 @@ */ package groovy +import java.util.concurrent.CyclicBarrier +import java.util.concurrent.TimeUnit + class ThreadMethodsTest extends GroovyTestCase { + final CyclicBarrier barrier = new CyclicBarrier(2) void testThreadNaming() { def t = Thread.start("MyNamedThread") { - sleep 3000 // give ourselves time to find the thread - } - def threadFoundByName = false - 30.times { - if (!threadFoundByName) { - sleep 100 // a little bit of time for t to start - threadFoundByName = Thread.allStackTraces.keySet().any { thread -> thread.name == 'MyNamedThread' } - } + barrier.await(3L, TimeUnit.SECONDS) // Signal Thread Start + barrier.await(3L, TimeUnit.SECONDS) // Wait for thread stop signal } + + barrier.await(3L, TimeUnit.SECONDS) // Wait for thread start signal + boolean threadFoundByName = Thread.allStackTraces.keySet().any { thread -> thread.name == 'MyNamedThread' } + barrier.await(3L, TimeUnit.SECONDS) // Send thread stop signal + t.join(100L) assert threadFoundByName } } \ No newline at end of file
