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:
parent
20ac5ea8ee
commit
71ec77b6d5
@ -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>
|
||||
|
@ -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()
|
||||
.forPath(key);
|
||||
client.create()
|
||||
.forPath(key);
|
||||
|
||||
// Set data value for our key
|
||||
async
|
||||
.setData()
|
||||
.forPath(key, expected.getBytes());
|
||||
async.setData()
|
||||
.forPath(key, expected.getBytes());
|
||||
|
||||
// Get data value
|
||||
AtomicBoolean isEquals = new AtomicBoolean();
|
||||
async
|
||||
.getData()
|
||||
.forPath(key)
|
||||
.thenAccept(data -> isEquals.set(new String(data).equals(expected)));
|
||||
async.getData()
|
||||
.forPath(key)
|
||||
.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,42 +55,35 @@ public class ConfigurationManagementManualTest extends BaseTest {
|
||||
String expected = "my_value";
|
||||
|
||||
// Create key structure
|
||||
async
|
||||
.create()
|
||||
.forPath(key);
|
||||
async.create()
|
||||
.forPath(key);
|
||||
|
||||
List<String> changes = new ArrayList<>();
|
||||
|
||||
// Watch data value
|
||||
async
|
||||
.watched()
|
||||
.getData()
|
||||
.forPath(key)
|
||||
.event()
|
||||
.thenAccept(watchedEvent -> {
|
||||
try {
|
||||
changes.add(new String(client
|
||||
.getData()
|
||||
.forPath(watchedEvent.getPath())));
|
||||
} catch (Exception e) {
|
||||
// fail ...
|
||||
}
|
||||
});
|
||||
async.watched()
|
||||
.getData()
|
||||
.forPath(key)
|
||||
.event()
|
||||
.thenAccept(watchedEvent -> {
|
||||
try {
|
||||
changes.add(new String(client.getData()
|
||||
.forPath(watchedEvent.getPath())));
|
||||
} catch (Exception e) {
|
||||
// fail ...
|
||||
}
|
||||
});
|
||||
|
||||
// Set data value for our key
|
||||
async
|
||||
.setData()
|
||||
.forPath(key, expected.getBytes());
|
||||
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()
|
||||
.toString());
|
||||
return String.format(KEY_FORMAT, UUID.randomUUID()
|
||||
.toString());
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
.forPath("/")).isNotNull();
|
||||
|
||||
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()
|
||||
.forPath("/")
|
||||
.thenAcceptAsync(s -> exists.set(s != null));
|
||||
Thread.sleep(100);
|
||||
assertThat(exists.get()).isTrue();
|
||||
|
||||
async.checkExists()
|
||||
.forPath("/")
|
||||
.thenAcceptAsync(s -> exists.set(s != null));
|
||||
|
||||
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()
|
||||
.forPath("/")
|
||||
.thenAccept(s -> exists.set(s != null));
|
||||
Thread.sleep(100);
|
||||
assertThat(exists.get()).isTrue();
|
||||
|
||||
async.checkExists()
|
||||
.forPath("/")
|
||||
.thenAccept(s -> exists.set(s != null));
|
||||
|
||||
await().until(() -> assertThat(exists.get()).isTrue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,31 +16,33 @@ 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))
|
||||
.build();
|
||||
.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()
|
||||
.whenComplete((value, e) -> {
|
||||
if (e != null) {
|
||||
fail("Cannot read host config", e);
|
||||
} else {
|
||||
assertThat(value).isNotNull();
|
||||
assertThat(value.getHostname()).isEqualTo("host-name");
|
||||
assertThat(value.getPort()).isEqualTo(8080);
|
||||
}
|
||||
modeledClient.read()
|
||||
.whenComplete((value, e) -> {
|
||||
if (e != null) {
|
||||
fail("Cannot read host config", e);
|
||||
} else {
|
||||
assertThat(value).isNotNull();
|
||||
assertThat(value.getHostname()).isEqualTo("host-name");
|
||||
assertThat(value.getPort()).isEqualTo(8080);
|
||||
}
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user