BAEL-1108 Added awailability (#3788)

* BAEL-399: A Guide to Multitenancy in Hibernate 5

* Removed unused properties in profile 2

* Changes after code review

* BAEL-1108

* Fixed tests and renamed test names

* BAEL-1108 Formatting

* Added awailability
This commit is contained in:
Jose Carvajal 2018-03-09 00:31:49 +01:00 committed by Predrag Maric
parent 20ac5ea8ee
commit 71ec77b6d5
4 changed files with 90 additions and 79 deletions

View File

@ -18,6 +18,7 @@
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<avaitility.version>1.7.0</avaitility.version>
</properties>
@ -64,5 +65,12 @@
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${avaitility.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -1,5 +1,6 @@
package com.baeldung.apache.curator.configuration;
import static com.jayway.awaitility.Awaitility.await;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.ArrayList;
@ -26,30 +27,27 @@ public class ConfigurationManagementManualTest extends BaseTest {
String expected = "my_value";
// Create key nodes structure
client
.create()
client.create()
.forPath(key);
// Set data value for our key
async
.setData()
async.setData()
.forPath(key, expected.getBytes());
// Get data value
AtomicBoolean isEquals = new AtomicBoolean();
async
.getData()
async.getData()
.forPath(key)
.thenAccept(data -> isEquals.set(new String(data).equals(expected)));
.thenAccept(
data -> isEquals.set(new String(data).equals(expected)));
Thread.sleep(1000);
assertThat(isEquals.get()).isTrue();
await().until(() -> assertThat(isEquals.get()).isTrue());
}
}
@Test
public void givenPath_whenWatchAKeyAndStoreAValue_thenWatcherIsTriggered() throws Exception {
public void givenPath_whenWatchAKeyAndStoreAValue_thenWatcherIsTriggered()
throws Exception {
try (CuratorFramework client = newClient()) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
@ -57,22 +55,19 @@ public class ConfigurationManagementManualTest extends BaseTest {
String expected = "my_value";
// Create key structure
async
.create()
async.create()
.forPath(key);
List<String> changes = new ArrayList<>();
// Watch data value
async
.watched()
async.watched()
.getData()
.forPath(key)
.event()
.thenAccept(watchedEvent -> {
try {
changes.add(new String(client
.getData()
changes.add(new String(client.getData()
.forPath(watchedEvent.getPath())));
} catch (Exception e) {
// fail ...
@ -80,19 +75,15 @@ public class ConfigurationManagementManualTest extends BaseTest {
});
// Set data value for our key
async
.setData()
async.setData()
.forPath(key, expected.getBytes());
Thread.sleep(1000);
assertThat(changes.size() > 0).isTrue();
await().until(() -> assertThat(changes.size() > 0).isTrue());
}
}
private String getKey() {
return String.format(KEY_FORMAT, UUID
.randomUUID()
return String.format(KEY_FORMAT, UUID.randomUUID()
.toString());
}
}

View File

@ -1,5 +1,6 @@
package com.baeldung.apache.curator.connection;
import static com.jayway.awaitility.Awaitility.await;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.concurrent.atomic.AtomicBoolean;
@ -14,56 +15,65 @@ import org.junit.Test;
public class ConnectionManagementManualTest {
@Test
public void givenRunningZookeeper_whenOpenConnection_thenClientIsOpened() throws Exception {
public void givenRunningZookeeper_whenOpenConnection_thenClientIsOpened()
throws Exception {
int sleepMsBetweenRetries = 100;
int maxRetries = 3;
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
RetryPolicy retryPolicy = new RetryNTimes(maxRetries,
sleepMsBetweenRetries);
try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
try (CuratorFramework client = CuratorFrameworkFactory
.newClient("127.0.0.1:2181", retryPolicy)) {
client.start();
assertThat(client
.checkExists()
assertThat(client.checkExists()
.forPath("/")).isNotNull();
}
}
@Test
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncNotBlocking_thenClientIsOpened() throws InterruptedException {
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncNotBlocking_thenClientIsOpened()
throws InterruptedException {
int sleepMsBetweenRetries = 100;
int maxRetries = 3;
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
RetryPolicy retryPolicy = new RetryNTimes(maxRetries,
sleepMsBetweenRetries);
try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
try (CuratorFramework client = CuratorFrameworkFactory
.newClient("127.0.0.1:2181", retryPolicy)) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
AtomicBoolean exists = new AtomicBoolean(false);
async
.checkExists()
async.checkExists()
.forPath("/")
.thenAcceptAsync(s -> exists.set(s != null));
Thread.sleep(100);
assertThat(exists.get()).isTrue();
await().until(() -> assertThat(exists.get()).isTrue());
}
}
@Test
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncBlocking_thenClientIsOpened() throws InterruptedException {
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncBlocking_thenClientIsOpened()
throws InterruptedException {
int sleepMsBetweenRetries = 100;
int maxRetries = 3;
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
RetryPolicy retryPolicy = new RetryNTimes(maxRetries,
sleepMsBetweenRetries);
try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
try (CuratorFramework client = CuratorFrameworkFactory
.newClient("127.0.0.1:2181", retryPolicy)) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
AtomicBoolean exists = new AtomicBoolean(false);
async
.checkExists()
async.checkExists()
.forPath("/")
.thenAccept(s -> exists.set(s != null));
Thread.sleep(100);
assertThat(exists.get()).isTrue();
await().until(() -> assertThat(exists.get()).isTrue());
}
}
}

View File

@ -16,21 +16,23 @@ import com.baeldung.apache.curator.BaseTest;
public class ModelTypedExamplesManualTest extends BaseTest {
@Test
public void givenPath_whenStoreAModel_thenNodesAreCreated() throws InterruptedException {
public void givenPath_whenStoreAModel_thenNodesAreCreated()
throws InterruptedException {
ModelSpec<HostConfig> mySpec = ModelSpec
.builder(ZPath.parseWithIds("/config/dev"), JacksonModelSerializer.build(HostConfig.class))
.builder(ZPath.parseWithIds("/config/dev"),
JacksonModelSerializer.build(HostConfig.class))
.build();
try (CuratorFramework client = newClient()) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
ModeledFramework<HostConfig> modeledClient = ModeledFramework.wrap(async, mySpec);
ModeledFramework<HostConfig> modeledClient = ModeledFramework
.wrap(async, mySpec);
modeledClient.set(new HostConfig("host-name", 8080));
modeledClient
.read()
modeledClient.read()
.whenComplete((value, e) -> {
if (e != null) {
fail("Cannot read host config", e);