diff --git a/bookkeeper-server/pom.xml b/bookkeeper-server/pom.xml index b4d1cfc474c..df14bd70698 100644 --- a/bookkeeper-server/pom.xml +++ b/bookkeeper-server/pom.xml @@ -209,6 +209,10 @@ ${project.parent.version} test + + org.awaitility + awaitility + diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/datainteg/WriteSetsTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/datainteg/WriteSetsTest.java index 1a82b0bde03..16350410949 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/datainteg/WriteSetsTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/datainteg/WriteSetsTest.java @@ -19,11 +19,12 @@ package org.apache.bookkeeper.bookie.datainteg; +import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.isIn; +import static org.hamcrest.Matchers.in; import com.google.common.collect.ImmutableList; @@ -168,7 +169,7 @@ private static void assertContentsMatch(ImmutableList writeSet, } for (int i = 0; i < distWriteSet.size(); i++) { - assertThat(distWriteSet.get(i), isIn(writeSet)); + assertThat(distWriteSet.get(i), is(in(writeSet))); } } } diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AutoRecoveryMainTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AutoRecoveryMainTest.java index f013399c3e4..b05cc269273 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AutoRecoveryMainTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AutoRecoveryMainTest.java @@ -20,10 +20,13 @@ */ package org.apache.bookkeeper.replication; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.awaitility.Awaitility.await; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import java.io.IOException; + import org.apache.bookkeeper.bookie.BookieImpl; import org.apache.bookkeeper.meta.zk.ZKMetadataClientDriver; import org.apache.bookkeeper.net.BookieId; @@ -49,11 +52,9 @@ public void testStartup() throws Exception { AutoRecoveryMain main = new AutoRecoveryMain(confByIndex(0)); try { main.start(); - Thread.sleep(500); - assertTrue("AuditorElector should be running", - main.auditorElector.isRunning()); - assertTrue("Replication worker should be running", - main.replicationWorker.isRunning()); + await().atMost(1, SECONDS).untilAsserted(() -> + assertTrue("AuditorElector and Replication Worker should be running", + main.auditorElector.isRunning() && main.replicationWorker.isRunning())); } finally { main.shutdown(); } @@ -66,12 +67,9 @@ public void testStartup() throws Exception { public void testShutdown() throws Exception { AutoRecoveryMain main = new AutoRecoveryMain(confByIndex(0)); main.start(); - Thread.sleep(500); - assertTrue("AuditorElector should be running", - main.auditorElector.isRunning()); - assertTrue("Replication worker should be running", - main.replicationWorker.isRunning()); - + await().atMost(1, SECONDS).untilAsserted(()-> + assertTrue("AuditorElector and ReplicationWorker should be running", + main.auditorElector.isRunning() && main.replicationWorker.isRunning())); main.shutdown(); assertFalse("AuditorElector should not be running", main.auditorElector.isRunning()); @@ -98,19 +96,7 @@ public void testAutoRecoverySessionLoss() throws Exception { */ ZKMetadataClientDriver zkMetadataClientDriver1 = startAutoRecoveryMain(main1); ZooKeeper zk1 = zkMetadataClientDriver1.getZk(); - - // Wait until auditor gets elected - for (int i = 0; i < 10; i++) { - try { - if (main1.auditorElector.getCurrentAuditor() != null) { - break; - } else { - Thread.sleep(1000); - } - } catch (IOException e) { - Thread.sleep(1000); - } - } + await().until(() -> main1.auditorElector.getAuditor() != null); BookieId currentAuditor = main1.auditorElector.getCurrentAuditor(); assertNotNull(currentAuditor); Auditor auditor1 = main1.auditorElector.getAuditor(); @@ -146,20 +132,15 @@ public void testAutoRecoverySessionLoss() throws Exception { * wait for some time for all the components of AR1 and AR2 are * shutdown. */ - for (int i = 0; i < 10; i++) { - if (!main1.auditorElector.isRunning() && !main1.replicationWorker.isRunning() - && !main1.isAutoRecoveryRunning() && !main2.auditorElector.isRunning() - && !main2.replicationWorker.isRunning() && !main2.isAutoRecoveryRunning()) { - break; - } - Thread.sleep(1000); - } + await().until(()->!main1.auditorElector.isRunning() && !main1.replicationWorker.isRunning() + && !main1.isAutoRecoveryRunning() && !main2.auditorElector.isRunning() + && !main2.replicationWorker.isRunning() && !main2.isAutoRecoveryRunning()); /* * the AR3 should be current auditor. */ currentAuditor = main3.auditorElector.getCurrentAuditor(); - assertTrue("Current Auditor should be AR3", currentAuditor.equals(BookieImpl.getBookieId(confByIndex(2)))); + assertEquals("Current Auditor should be AR3", currentAuditor, BookieImpl.getBookieId(confByIndex(2))); auditor3 = main3.auditorElector.getAuditor(); assertTrue("Auditor of AR3 should be running", auditor3.isRunning()); diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieZKExpireTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieZKExpireTest.java index 006c485eeee..8ad0319ec89 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieZKExpireTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieZKExpireTest.java @@ -21,9 +21,10 @@ package org.apache.bookkeeper.test; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.awaitility.Awaitility.await; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import io.netty.buffer.UnpooledByteBufAllocator; @@ -73,35 +74,26 @@ conf, new TestBookieImpl(conf), NullStatsLogger.INSTANCE, UnpooledByteBufAllocator.DEFAULT, new MockUncleanShutdownDetection()); server.start(); - - int secondsToWait = 5; - while (!server.isRunning()) { - Thread.sleep(1000); - if (secondsToWait-- <= 0) { - fail("Bookie never started"); - } - } + BookieServer finalServer = server; + await().atMost(5, SECONDS).until(finalServer::isRunning); Thread sendthread = null; threadCount = Thread.activeCount(); threads = new Thread[threadCount * 2]; threadCount = Thread.enumerate(threads); for (int i = 0; i < threadCount; i++) { - if (threads[i].getName().indexOf("SendThread") != -1 + if (threads[i].getName().contains("SendThread") && !threadset.contains(threads[i])) { sendthread = threads[i]; break; } } assertNotNull("Send thread not found", sendthread); - sendthread.suspend(); - Thread.sleep(2 * conf.getZkTimeout()); sendthread.resume(); - // allow watcher thread to run - Thread.sleep(3000); - assertTrue("Bookie should not shutdown on losing zk session", server.isBookieRunning()); - assertTrue("Bookie Server should not shutdown on losing zk session", server.isRunning()); + await().atMost(5, SECONDS).untilAsserted(() -> + assertTrue("Bookie and Bookie Server should not shutdown on losing zk session", + finalServer.isBookieRunning() && finalServer.isRunning())); } finally { server.shutdown(); } diff --git a/pom.xml b/pom.xml index dff71fdda7b..ce7e69ba168 100644 --- a/pom.xml +++ b/pom.xml @@ -209,6 +209,7 @@ 1 4.0.0 3.0.1 + 4.2.0 3.0.0-M6 @@ -779,6 +780,12 @@ rxjava ${rxjava.version} + + org.awaitility + awaitility + ${awaitility.version} + test + @@ -839,6 +846,12 @@ powermock-module-junit4 test + + org.awaitility + awaitility + ${awaitility.version} + test +