-
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 4 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().atMost(20, SECONDS).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. better to keep the same timeout (10 seconds) 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. Thanks,updated |
||
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().atMost(20, SECONDS).until(()->!main1.auditorElector.isRunning() && !main1.replicationWorker.isRunning() | ||
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. better to keep the same timeout (10 seconds) |
||
&& !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,6 +21,7 @@ | |
|
||
package org.apache.bookkeeper.test; | ||
|
||
import static org.awaitility.Awaitility.await; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
@@ -29,6 +30,7 @@ | |
|
||
import java.io.File; | ||
import java.util.HashSet; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.apache.bookkeeper.bookie.MockUncleanShutdownDetection; | ||
import org.apache.bookkeeper.bookie.TestBookieImpl; | ||
|
@@ -76,7 +78,7 @@ conf, new TestBookieImpl(conf), | |
|
||
int secondsToWait = 5; | ||
while (!server.isRunning()) { | ||
Thread.sleep(1000); | ||
await().atMost(1000, TimeUnit.MILLISECONDS); | ||
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. this loop should become
|
||
if (secondsToWait-- <= 0) { | ||
fail("Bookie never started"); | ||
} | ||
|
@@ -95,11 +97,11 @@ conf, new TestBookieImpl(conf), | |
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. |
||
await().atMost(2L * conf.getZkTimeout(), TimeUnit.MILLISECONDS).await(); | ||
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. we can leave the sleep here, we are not waiting for a particular condition |
||
sendthread.resume(); | ||
|
||
// allow watcher thread to run | ||
Thread.sleep(3000); | ||
await().atMost(3, TimeUnit.SECONDS).await(); | ||
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.
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 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. Thx,done |
||
assertTrue("Bookie should not shutdown on losing zk session", server.isBookieRunning()); | ||
assertTrue("Bookie Server should not shutdown on losing zk session", server.isRunning()); | ||
} finally { | ||
|
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