|
1 | 1 | package com.baeldung.apache.curator.connection; |
2 | 2 |
|
| 3 | +import static com.jayway.awaitility.Awaitility.await; |
3 | 4 | import static org.assertj.core.api.Assertions.assertThat; |
4 | 5 |
|
5 | 6 | import java.util.concurrent.atomic.AtomicBoolean; |
|
14 | 15 | public class ConnectionManagementManualTest { |
15 | 16 |
|
16 | 17 | @Test |
17 | | - public void givenRunningZookeeper_whenOpenConnection_thenClientIsOpened() throws Exception { |
| 18 | + public void givenRunningZookeeper_whenOpenConnection_thenClientIsOpened() |
| 19 | + throws Exception { |
18 | 20 | int sleepMsBetweenRetries = 100; |
19 | 21 | int maxRetries = 3; |
20 | | - RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries); |
| 22 | + RetryPolicy retryPolicy = new RetryNTimes(maxRetries, |
| 23 | + sleepMsBetweenRetries); |
21 | 24 |
|
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)) { |
23 | 27 | client.start(); |
24 | | - assertThat(client |
25 | | - .checkExists() |
26 | | - .forPath("/")).isNotNull(); |
| 28 | + |
| 29 | + assertThat(client.checkExists() |
| 30 | + .forPath("/")).isNotNull(); |
27 | 31 | } |
28 | 32 | } |
29 | 33 |
|
30 | 34 | @Test |
31 | | - public void givenRunningZookeeper_whenOpenConnectionUsingAsyncNotBlocking_thenClientIsOpened() throws InterruptedException { |
| 35 | + public void givenRunningZookeeper_whenOpenConnectionUsingAsyncNotBlocking_thenClientIsOpened() |
| 36 | + throws InterruptedException { |
32 | 37 | int sleepMsBetweenRetries = 100; |
33 | 38 | int maxRetries = 3; |
34 | | - RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries); |
| 39 | + RetryPolicy retryPolicy = new RetryNTimes(maxRetries, |
| 40 | + sleepMsBetweenRetries); |
35 | 41 |
|
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)) { |
37 | 44 | client.start(); |
38 | 45 | AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client); |
39 | 46 |
|
40 | 47 | 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()); |
47 | 54 | } |
48 | 55 | } |
49 | 56 |
|
50 | 57 | @Test |
51 | | - public void givenRunningZookeeper_whenOpenConnectionUsingAsyncBlocking_thenClientIsOpened() throws InterruptedException { |
| 58 | + public void givenRunningZookeeper_whenOpenConnectionUsingAsyncBlocking_thenClientIsOpened() |
| 59 | + throws InterruptedException { |
52 | 60 | int sleepMsBetweenRetries = 100; |
53 | 61 | int maxRetries = 3; |
54 | | - RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries); |
| 62 | + RetryPolicy retryPolicy = new RetryNTimes(maxRetries, |
| 63 | + sleepMsBetweenRetries); |
55 | 64 |
|
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)) { |
57 | 67 | client.start(); |
58 | 68 | AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client); |
59 | 69 |
|
60 | 70 | 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()); |
67 | 77 | } |
68 | 78 | } |
69 | 79 | } |
0 commit comments