Skip to content

Commit 71ec77b

Browse files
Sgitariopedja4
authored andcommitted
BAEL-1108 Added awailability (eugenp#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
1 parent 20ac5ea commit 71ec77b

4 files changed

Lines changed: 91 additions & 80 deletions

File tree

‎apache-curator/pom.xml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
<!-- testing -->
2020
<assertj.version>3.6.1</assertj.version>
21+
<avaitility.version>1.7.0</avaitility.version>
2122

2223
</properties>
2324

@@ -64,5 +65,12 @@
6465
<version>${assertj.version}</version>
6566
<scope>test</scope>
6667
</dependency>
68+
69+
<dependency>
70+
<groupId>com.jayway.awaitility</groupId>
71+
<artifactId>awaitility</artifactId>
72+
<version>${avaitility.version}</version>
73+
<scope>test</scope>
74+
</dependency>
6775
</dependencies>
6876
</project>
Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.baeldung.apache.curator.configuration;
22

3+
import static com.jayway.awaitility.Awaitility.await;
34
import static org.assertj.core.api.Assertions.assertThat;
45

56
import java.util.ArrayList;
@@ -26,73 +27,63 @@ public void givenPath_whenCreateKey_thenValueIsStored() throws Exception {
2627
String expected = "my_value";
2728

2829
// Create key nodes structure
29-
client
30-
.create()
31-
.forPath(key);
30+
client.create()
31+
.forPath(key);
3232

3333
// Set data value for our key
34-
async
35-
.setData()
36-
.forPath(key, expected.getBytes());
34+
async.setData()
35+
.forPath(key, expected.getBytes());
3736

3837
// Get data value
3938
AtomicBoolean isEquals = new AtomicBoolean();
40-
async
41-
.getData()
42-
.forPath(key)
43-
.thenAccept(data -> isEquals.set(new String(data).equals(expected)));
39+
async.getData()
40+
.forPath(key)
41+
.thenAccept(
42+
data -> isEquals.set(new String(data).equals(expected)));
4443

45-
Thread.sleep(1000);
46-
47-
assertThat(isEquals.get()).isTrue();
44+
await().until(() -> assertThat(isEquals.get()).isTrue());
4845
}
4946
}
5047

5148
@Test
52-
public void givenPath_whenWatchAKeyAndStoreAValue_thenWatcherIsTriggered() throws Exception {
49+
public void givenPath_whenWatchAKeyAndStoreAValue_thenWatcherIsTriggered()
50+
throws Exception {
5351
try (CuratorFramework client = newClient()) {
5452
client.start();
5553
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
5654
String key = getKey();
5755
String expected = "my_value";
5856

5957
// Create key structure
60-
async
61-
.create()
62-
.forPath(key);
58+
async.create()
59+
.forPath(key);
6360

6461
List<String> changes = new ArrayList<>();
6562

6663
// Watch data value
67-
async
68-
.watched()
69-
.getData()
70-
.forPath(key)
71-
.event()
72-
.thenAccept(watchedEvent -> {
73-
try {
74-
changes.add(new String(client
75-
.getData()
76-
.forPath(watchedEvent.getPath())));
77-
} catch (Exception e) {
78-
// fail ...
79-
}
80-
});
64+
async.watched()
65+
.getData()
66+
.forPath(key)
67+
.event()
68+
.thenAccept(watchedEvent -> {
69+
try {
70+
changes.add(new String(client.getData()
71+
.forPath(watchedEvent.getPath())));
72+
} catch (Exception e) {
73+
// fail ...
74+
}
75+
});
8176

8277
// Set data value for our key
83-
async
84-
.setData()
85-
.forPath(key, expected.getBytes());
86-
87-
Thread.sleep(1000);
78+
async.setData()
79+
.forPath(key, expected.getBytes());
8880

89-
assertThat(changes.size() > 0).isTrue();
81+
await().until(() -> assertThat(changes.size() > 0).isTrue());
9082
}
9183
}
9284

9385
private String getKey() {
94-
return String.format(KEY_FORMAT, UUID
95-
.randomUUID()
96-
.toString());
86+
return String.format(KEY_FORMAT, UUID.randomUUID()
87+
.toString());
9788
}
9889
}
Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.baeldung.apache.curator.connection;
22

3+
import static com.jayway.awaitility.Awaitility.await;
34
import static org.assertj.core.api.Assertions.assertThat;
45

56
import java.util.concurrent.atomic.AtomicBoolean;
@@ -14,56 +15,65 @@
1415
public class ConnectionManagementManualTest {
1516

1617
@Test
17-
public void givenRunningZookeeper_whenOpenConnection_thenClientIsOpened() throws Exception {
18+
public void givenRunningZookeeper_whenOpenConnection_thenClientIsOpened()
19+
throws Exception {
1820
int sleepMsBetweenRetries = 100;
1921
int maxRetries = 3;
20-
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
22+
RetryPolicy retryPolicy = new RetryNTimes(maxRetries,
23+
sleepMsBetweenRetries);
2124

22-
try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
25+
try (CuratorFramework client = CuratorFrameworkFactory
26+
.newClient("127.0.0.1:2181", retryPolicy)) {
2327
client.start();
24-
assertThat(client
25-
.checkExists()
26-
.forPath("/")).isNotNull();
28+
29+
assertThat(client.checkExists()
30+
.forPath("/")).isNotNull();
2731
}
2832
}
2933

3034
@Test
31-
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncNotBlocking_thenClientIsOpened() throws InterruptedException {
35+
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncNotBlocking_thenClientIsOpened()
36+
throws InterruptedException {
3237
int sleepMsBetweenRetries = 100;
3338
int maxRetries = 3;
34-
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
39+
RetryPolicy retryPolicy = new RetryNTimes(maxRetries,
40+
sleepMsBetweenRetries);
3541

36-
try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
42+
try (CuratorFramework client = CuratorFrameworkFactory
43+
.newClient("127.0.0.1:2181", retryPolicy)) {
3744
client.start();
3845
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
3946

4047
AtomicBoolean exists = new AtomicBoolean(false);
41-
async
42-
.checkExists()
43-
.forPath("/")
44-
.thenAcceptAsync(s -> exists.set(s != null));
45-
Thread.sleep(100);
46-
assertThat(exists.get()).isTrue();
48+
49+
async.checkExists()
50+
.forPath("/")
51+
.thenAcceptAsync(s -> exists.set(s != null));
52+
53+
await().until(() -> assertThat(exists.get()).isTrue());
4754
}
4855
}
4956

5057
@Test
51-
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncBlocking_thenClientIsOpened() throws InterruptedException {
58+
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncBlocking_thenClientIsOpened()
59+
throws InterruptedException {
5260
int sleepMsBetweenRetries = 100;
5361
int maxRetries = 3;
54-
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
62+
RetryPolicy retryPolicy = new RetryNTimes(maxRetries,
63+
sleepMsBetweenRetries);
5564

56-
try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
65+
try (CuratorFramework client = CuratorFrameworkFactory
66+
.newClient("127.0.0.1:2181", retryPolicy)) {
5767
client.start();
5868
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
5969

6070
AtomicBoolean exists = new AtomicBoolean(false);
61-
async
62-
.checkExists()
63-
.forPath("/")
64-
.thenAccept(s -> exists.set(s != null));
65-
Thread.sleep(100);
66-
assertThat(exists.get()).isTrue();
71+
72+
async.checkExists()
73+
.forPath("/")
74+
.thenAccept(s -> exists.set(s != null));
75+
76+
await().until(() -> assertThat(exists.get()).isTrue());
6777
}
6878
}
6979
}

‎apache-curator/src/test/java/com/baeldung/apache/curator/modeled/ModelTypedExamplesManualTest.java‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,33 @@
1616
public class ModelTypedExamplesManualTest extends BaseTest {
1717

1818
@Test
19-
public void givenPath_whenStoreAModel_thenNodesAreCreated() throws InterruptedException {
19+
public void givenPath_whenStoreAModel_thenNodesAreCreated()
20+
throws InterruptedException {
2021

2122
ModelSpec<HostConfig> mySpec = ModelSpec
22-
.builder(ZPath.parseWithIds("/config/dev"), JacksonModelSerializer.build(HostConfig.class))
23-
.build();
23+
.builder(ZPath.parseWithIds("/config/dev"),
24+
JacksonModelSerializer.build(HostConfig.class))
25+
.build();
2426

2527
try (CuratorFramework client = newClient()) {
2628
client.start();
2729
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
28-
ModeledFramework<HostConfig> modeledClient = ModeledFramework.wrap(async, mySpec);
30+
ModeledFramework<HostConfig> modeledClient = ModeledFramework
31+
.wrap(async, mySpec);
2932

3033
modeledClient.set(new HostConfig("host-name", 8080));
3134

32-
modeledClient
33-
.read()
34-
.whenComplete((value, e) -> {
35-
if (e != null) {
36-
fail("Cannot read host config", e);
37-
} else {
38-
assertThat(value).isNotNull();
39-
assertThat(value.getHostname()).isEqualTo("host-name");
40-
assertThat(value.getPort()).isEqualTo(8080);
41-
}
42-
43-
});
35+
modeledClient.read()
36+
.whenComplete((value, e) -> {
37+
if (e != null) {
38+
fail("Cannot read host config", e);
39+
} else {
40+
assertThat(value).isNotNull();
41+
assertThat(value.getHostname()).isEqualTo("host-name");
42+
assertThat(value.getPort()).isEqualTo(8080);
43+
}
44+
45+
});
4446
}
4547

4648
}

0 commit comments

Comments
 (0)