Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bookkeeper-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -168,7 +169,7 @@ private static void assertContentsMatch(ImmutableList<Integer> writeSet,
}

for (int i = 0; i < distWriteSet.size(); i++) {
assertThat(distWriteSet.get(i), isIn(writeSet));
assertThat(distWriteSet.get(i), is(in(writeSet)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(() ->
Copy link
Contributor

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?

Copy link
Member Author

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

assertTrue("AuditorElector and Replication Worker should be running",
main.auditorElector.isRunning() && main.replicationWorker.isRunning()));
} finally {
main.shutdown();
}
Expand All @@ -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(()->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same above

Copy link
Contributor

Choose a reason for hiding this comment

The 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());
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it's different than before? looks like the getCurrentAuditor() is different than getAuditor()

BookieId currentAuditor = main1.auditorElector.getCurrentAuditor();
assertNotNull(currentAuditor);
Auditor auditor1 = main1.auditorElector.getAuditor();
Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
Copy link
Member

Choose a reason for hiding this comment

The 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();
}
Expand Down
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
<forkCount.variable>1</forkCount.variable>
<servlet-api.version>4.0.0</servlet-api.version>
<rxjava.version>3.0.1</rxjava.version>
<awaitility.version>4.2.0</awaitility.version>
<maven-failsafe-plugin.version>3.0.0-M6</maven-failsafe-plugin.version>
</properties>

Expand Down Expand Up @@ -779,6 +780,12 @@
<artifactId>rxjava</artifactId>
<version>${rxjava.version}</version>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -839,6 +846,12 @@
<artifactId>powermock-module-junit4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down