Return-Path: X-Original-To: apmail-cassandra-commits-archive@www.apache.org Delivered-To: apmail-cassandra-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2CC8C18EA4 for ; Thu, 6 Aug 2015 12:37:39 +0000 (UTC) Received: (qmail 31291 invoked by uid 500); 6 Aug 2015 12:37:38 -0000 Delivered-To: apmail-cassandra-commits-archive@cassandra.apache.org Received: (qmail 31174 invoked by uid 500); 6 Aug 2015 12:37:37 -0000 Mailing-List: contact commits-help@cassandra.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cassandra.apache.org Delivered-To: mailing list commits@cassandra.apache.org Received: (qmail 30743 invoked by uid 99); 6 Aug 2015 12:37:37 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 06 Aug 2015 12:37:37 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 9A904E6B7E; Thu, 6 Aug 2015 12:37:37 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: benedict@apache.org To: commits@cassandra.apache.org Date: Thu, 06 Aug 2015 12:37:42 -0000 Message-Id: <9d67e7d7e91240ec8061a40901a8812e@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [6/9] cassandra git commit: Fix WaitQueueTest flakiness Fix WaitQueueTest flakiness Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/ecb2b4b0 Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/ecb2b4b0 Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/ecb2b4b0 Branch: refs/heads/trunk Commit: ecb2b4b0473c019d1132c89887734792c75e0895 Parents: 765ab3f Author: Benedict Elliott Smith Authored: Mon Aug 3 16:34:29 2015 +0100 Committer: Benedict Elliott Smith Committed: Thu Aug 6 14:36:07 2015 +0200 ---------------------------------------------------------------------- test/unit/org/apache/cassandra/Util.java | 5 ++ .../cassandra/concurrent/WaitQueueTest.java | 91 ++++++-------------- 2 files changed, 32 insertions(+), 64 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/ecb2b4b0/test/unit/org/apache/cassandra/Util.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/Util.java b/test/unit/org/apache/cassandra/Util.java index 254c21c..7efe6f4 100644 --- a/test/unit/org/apache/cassandra/Util.java +++ b/test/unit/org/apache/cassandra/Util.java @@ -526,4 +526,9 @@ public class Util assert p == newP; } } + + public static void joinThread(Thread thread) throws InterruptedException + { + thread.join(10000); + } } http://git-wip-us.apache.org/repos/asf/cassandra/blob/ecb2b4b0/test/unit/org/apache/cassandra/concurrent/WaitQueueTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/concurrent/WaitQueueTest.java b/test/unit/org/apache/cassandra/concurrent/WaitQueueTest.java index 3e7cb7b..8e092c5 100644 --- a/test/unit/org/apache/cassandra/concurrent/WaitQueueTest.java +++ b/test/unit/org/apache/cassandra/concurrent/WaitQueueTest.java @@ -21,10 +21,13 @@ package org.apache.cassandra.concurrent; */ +import org.apache.cassandra.Util; import org.apache.cassandra.utils.concurrent.WaitQueue; import org.junit.*; +import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import static org.junit.Assert.*; @@ -38,6 +41,7 @@ public class WaitQueueTest } public void testSerial(final WaitQueue queue) throws InterruptedException { + AtomicInteger ready = new AtomicInteger(); Thread[] ts = new Thread[4]; for (int i = 0 ; i < ts.length ; i++) ts[i] = new Thread(new Runnable() @@ -46,6 +50,7 @@ public class WaitQueueTest public void run() { WaitQueue.Signal wait = queue.register(); + ready.incrementAndGet(); try { wait.await(); @@ -55,68 +60,28 @@ public class WaitQueueTest } } }); - for (int i = 0 ; i < ts.length ; i++) - ts[i].start(); - Thread.sleep(100); - queue.signal(); - queue.signal(); - queue.signal(); - queue.signal(); - for (int i = 0 ; i < ts.length ; i++) + for (Thread t : ts) + t.start(); + final ThreadLocalRandom random = ThreadLocalRandom.current(); + while (ready.get() < ts.length) + random.nextLong(); + for (Thread t : ts) + queue.signal(); + for (Thread t : ts) { - ts[i].join(100); - assertFalse(queue.getClass().getName(), ts[i].isAlive()); + Util.joinThread(t); + assertFalse(queue.getClass().getName(), t.isAlive()); } } - - @Test - public void testCondition1() throws InterruptedException - { - testCondition1(new WaitQueue()); - } - - public void testCondition1(final WaitQueue queue) throws InterruptedException - { - final AtomicBoolean cond1 = new AtomicBoolean(false); - final AtomicBoolean fail = new AtomicBoolean(false); - Thread t1 = new Thread(new Runnable() - { - @Override - public void run() - { - try - { - Thread.sleep(200); - } catch (InterruptedException e) - { - e.printStackTrace(); - } - WaitQueue.Signal wait = queue.register(); - if (!cond1.get()) - { - System.err.println("Condition should have already been met"); - fail.set(true); - } - } - }); - t1.start(); - Thread.sleep(50); - cond1.set(true); - Thread.sleep(300); - queue.signal(); - t1.join(300); - assertFalse(queue.getClass().getName(), t1.isAlive()); - assertFalse(fail.get()); - } - @Test - public void testCondition2() throws InterruptedException + public void testCondition() throws InterruptedException { - testCondition2(new WaitQueue()); + testCondition(new WaitQueue()); } - public void testCondition2(final WaitQueue queue) throws InterruptedException + public void testCondition(final WaitQueue queue) throws InterruptedException { + final AtomicBoolean ready = new AtomicBoolean(false); final AtomicBoolean condition = new AtomicBoolean(false); final AtomicBoolean fail = new AtomicBoolean(false); Thread t = new Thread(new Runnable() @@ -129,16 +94,12 @@ public class WaitQueueTest { System.err.println(""); fail.set(true); + ready.set(true); + return; } - try - { - Thread.sleep(200); - wait.await(); - } catch (InterruptedException e) - { - e.printStackTrace(); - } + ready.set(true); + wait.awaitUninterruptibly(); if (!condition.get()) { System.err.println("Woke up when condition not met"); @@ -147,10 +108,12 @@ public class WaitQueueTest } }); t.start(); - Thread.sleep(50); + final ThreadLocalRandom random = ThreadLocalRandom.current(); + while (!ready.get()) + random.nextLong(); condition.set(true); queue.signal(); - t.join(300); + Util.joinThread(t); assertFalse(queue.getClass().getName(), t.isAlive()); assertFalse(fail.get()); }