-
Notifications
You must be signed in to change notification settings - Fork 902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Useing Awaitility for asynchroneous testing. #3392
base: master
Are you sure you want to change the base?
Changes from all commits
3cac8c6
dadd0ab
42651a4
248eb3b
b4b7bf1
51f94d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(()-> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why it's different than before? looks like the |
||
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()); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this is part of the testing. It wants to resume the send thread after 2* zkTimeout to test the bookie should still working even if the zk session is timeout. |
||
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(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the atMost logic and use the default most await time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default await time is 10s