Skip to content

Commit

Permalink
[ Tests ] Use Awaitility to instead of Thread sleep (apache#11164)
Browse files Browse the repository at this point in the history
### Motivation

Use Awaitility to instead of Thread sleep

### Modifications

- Use Awaitility to instead of Thread sleep
  • Loading branch information
mattisonchao authored Jun 30, 2021
1 parent 440f71c commit 8579929
Showing 1 changed file with 17 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static org.testng.Assert.assertEquals;

import java.time.Duration;
import java.util.List;
import java.util.Properties;
import java.util.UUID;
Expand Down Expand Up @@ -85,18 +86,9 @@ public void testInitialization() throws InterruptedException, ExecutionException
future.completeExceptionally(t);
}
});

// Make sure subscription has been created
while (true) {
try {
List<String> subscriptions = admin.topics().getSubscriptions(topicName);
if(subscriptions.size() == 1){
break;
}
} catch (Exception ignored){
}
Thread.sleep(200);
}
Awaitility.await()
.ignoreExceptions()
.until(()->admin.topics().getSubscriptions(topicName).size() == 1);

PulsarClientTool pulsarClientToolProducer = new PulsarClientTool(properties);

Expand Down Expand Up @@ -150,16 +142,10 @@ public void testNonDurableSubscribe() throws Exception {
Assert.assertFalse(future.isCompletedExceptionally());
future.get();

while (true) {
try {
List<String> subscriptions = admin.topics().getSubscriptions(topicName);
if (subscriptions.size() == 0) {
break;
}
} catch (Exception ignored) {
}
Thread.sleep(200);
}
Awaitility.await()
.ignoreExceptions()
.atMost(Duration.ofMillis(20000))
.until(()->admin.topics().getSubscriptions(topicName).size() == 0);
}

@Test(timeOut = 60000)
Expand All @@ -186,18 +172,11 @@ public void testDurableSubscribe() throws Exception {
future.completeExceptionally(t);
}
});

// Make sure subscription has been created
while (true) {
try {
List<String> subscriptions = admin.topics().getSubscriptions(topicName);
if (subscriptions.size() == 1) {
break;
}
} catch (Exception ignored) {
}
Thread.sleep(200);
}
Awaitility.await()
.atMost(Duration.ofMillis(60000))
.ignoreExceptions()
.until(() -> admin.topics().getSubscriptions(topicName).size() == 1);

PulsarClientTool pulsarClientToolProducer = new PulsarClientTool(properties);

Expand All @@ -207,7 +186,7 @@ public void testDurableSubscribe() throws Exception {
Assert.assertFalse(future.isCompletedExceptionally());
future.get();
//wait for close
Thread.sleep(2000);
Awaitility.waitAtMost(Duration.ofDays(2000));
List<String> subscriptions = admin.topics().getSubscriptions(topicName);
Assert.assertNotNull(subscriptions);
Assert.assertEquals(subscriptions.size(), 1);
Expand Down Expand Up @@ -239,15 +218,10 @@ public void testEncryption() throws Exception {
});

// Make sure subscription has been created
Awaitility.await().until(() -> {
boolean isCreated = false;
try {
List<String> subscriptions = admin.topics().getSubscriptions(topicName);
isCreated = (subscriptions.size() == 1);
} catch (Exception ignored) {
}
return isCreated;
});
Awaitility.await()
.atMost(Duration.ofMillis(20000))
.ignoreExceptions()
.until(() -> admin.topics().getSubscriptions(topicName).size() == 1);

PulsarClientTool pulsarClientToolProducer = new PulsarClientTool(properties);
String[] args = {"produce", "-m", "Have a nice day", "-n", Integer.toString(numberOfMessages), "-ekn",
Expand Down

0 comments on commit 8579929

Please sign in to comment.