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

View File

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

View File

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

View File

@ -16,31 +16,33 @@ import com.baeldung.apache.curator.BaseTest;
public class ModelTypedExamplesManualTest extends BaseTest { public class ModelTypedExamplesManualTest extends BaseTest {
@Test @Test
public void givenPath_whenStoreAModel_thenNodesAreCreated() throws InterruptedException { public void givenPath_whenStoreAModel_thenNodesAreCreated()
throws InterruptedException {
ModelSpec<HostConfig> mySpec = ModelSpec ModelSpec<HostConfig> mySpec = ModelSpec
.builder(ZPath.parseWithIds("/config/dev"), JacksonModelSerializer.build(HostConfig.class)) .builder(ZPath.parseWithIds("/config/dev"),
.build(); JacksonModelSerializer.build(HostConfig.class))
.build();
try (CuratorFramework client = newClient()) { try (CuratorFramework client = newClient()) {
client.start(); client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client); 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.set(new HostConfig("host-name", 8080));
modeledClient modeledClient.read()
.read() .whenComplete((value, e) -> {
.whenComplete((value, e) -> { if (e != null) {
if (e != null) { fail("Cannot read host config", e);
fail("Cannot read host config", e); } else {
} else { assertThat(value).isNotNull();
assertThat(value).isNotNull(); assertThat(value.getHostname()).isEqualTo("host-name");
assertThat(value.getHostname()).isEqualTo("host-name"); assertThat(value.getPort()).isEqualTo(8080);
assertThat(value.getPort()).isEqualTo(8080); }
}
}); });
} }
} }