Skip to content

Commit

Permalink
BAEL-1108 Added awailability (eugenp#3788)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Sgitario authored and pedja4 committed Mar 8, 2018
1 parent 20ac5ea commit 71ec77b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 80 deletions.
8 changes: 8 additions & 0 deletions apache-curator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<avaitility.version>1.7.0</avaitility.version>

</properties>

Expand Down Expand Up @@ -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>
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,73 +27,63 @@ public void givenPath_whenCreateKey_thenValueIsStored() throws Exception {
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);
String key = getKey();
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());

Thread.sleep(1000);
async.setData()
.forPath(key, expected.getBytes());

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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,56 +15,65 @@
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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,33 @@
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);
}

});
}

}
Expand Down

0 comments on commit 71ec77b

Please sign in to comment.