Skip to content

Commit

Permalink
try refactor
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed Feb 14, 2024
1 parent 0e75fa8 commit ed12072
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,8 @@ public class CuratorTransactionResult {
* @param forPath path
* @return predicate
*/
public static Predicate<CuratorTransactionResult> ofTypeAndPath(final OperationType type, final String forPath) {
return new Predicate<CuratorTransactionResult>() {
@Override
public boolean apply(CuratorTransactionResult result) {
return (result.getType() == type) && result.getForPath().equals(forPath);
}
};
public static Predicate<CuratorTransactionResult> ofTypeAndPath(OperationType type, String forPath) {
return result -> (result.getType() == type) && result.getForPath().equals(forPath);
}

public CuratorTransactionResult(OperationType type, String forPath, String resultPath, Stat resultStat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ public void testErrors() throws Exception {
CuratorOp createOp1 = client.transactionOp().create().forPath("/bar");
CuratorOp createOp2 = client.transactionOp().create().forPath("/z/blue");
final BlockingQueue<CuratorEvent> callbackQueue = new LinkedBlockingQueue<>();
BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
callbackQueue.add(event);
}
};
BackgroundCallback callback = (client1, event) -> callbackQueue.add(event);
client.transaction().inBackground(callback).forOperations(createOp1, createOp2);
CuratorEvent event = callbackQueue.poll(new Timing().milliseconds(), TimeUnit.MILLISECONDS);
assertNotNull(event);
Expand Down Expand Up @@ -124,10 +119,10 @@ public void testWithNamespace() throws Exception {
Collection<CuratorTransactionResult> results =
client.transaction().forOperations(createOp1, createOp2, setDataOp, createOp3, deleteOp);

assertTrue(client.checkExists().forPath("/foo") != null);
assertTrue(client.usingNamespace(null).checkExists().forPath("/galt/foo") != null);
assertNotNull(client.checkExists().forPath("/foo"));
assertNotNull(client.usingNamespace(null).checkExists().forPath("/galt/foo"));
assertArrayEquals(client.getData().forPath("/foo"), "two".getBytes());
assertTrue(client.checkExists().forPath("/foo/bar") == null);
assertNull(client.checkExists().forPath("/foo/bar"));

CuratorTransactionResult ephemeralResult =
Iterables.find(results, CuratorTransactionResult.ofTypeAndPath(OperationType.CREATE, "/test-"));
Expand All @@ -149,7 +144,7 @@ public void testBasic() throws Exception {

Collection<CuratorTransactionResult> results = client.transaction().forOperations(createOp1, createOp2);

assertTrue(client.checkExists().forPath("/foo/bar") != null);
assertNotNull(client.checkExists().forPath("/foo/bar"));
assertArrayEquals(client.getData().forPath("/foo/bar"), "snafu".getBytes());

CuratorTransactionResult fooResult =
Expand All @@ -175,17 +170,12 @@ public void testBackground() throws Exception {
CuratorOp createOp2 = client.transactionOp().create().forPath("/foo/bar", "snafu".getBytes());

final BlockingQueue<List<CuratorTransactionResult>> queue = Queues.newLinkedBlockingQueue();
BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
queue.add(event.getOpResults());
}
};
BackgroundCallback callback = (client1, event) -> queue.add(event.getOpResults());
client.transaction().inBackground(callback).forOperations(createOp1, createOp2);
Collection<CuratorTransactionResult> results = queue.poll(5, TimeUnit.SECONDS);

assertNotNull(results);
assertTrue(client.checkExists().forPath("/foo/bar") != null);
assertNotNull(client.checkExists().forPath("/foo/bar"));
assertArrayEquals(client.getData().forPath("/foo/bar"), "snafu".getBytes());

CuratorTransactionResult fooResult =
Expand Down Expand Up @@ -221,23 +211,18 @@ public void testBackgroundWithNamespace() throws Exception {
CuratorOp deleteOp = client.transactionOp().delete().forPath("/foo/bar");

final BlockingQueue<List<CuratorTransactionResult>> queue = Queues.newLinkedBlockingQueue();
BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
queue.add(event.getOpResults());
}
};
BackgroundCallback callback = (client1, event) -> queue.add(event.getOpResults());
client.transaction()
.inBackground(callback)
.forOperations(createOp1, createOp2, setDataOp, createOp3, deleteOp);

Collection<CuratorTransactionResult> results = queue.poll(5, TimeUnit.SECONDS);

assertNotNull(results);
assertTrue(client.checkExists().forPath("/foo") != null);
assertTrue(client.usingNamespace(null).checkExists().forPath("/galt/foo") != null);
assertNotNull(client.checkExists().forPath("/foo"));
assertNotNull(client.usingNamespace(null).checkExists().forPath("/galt/foo"));
assertArrayEquals(client.getData().forPath("/foo"), "two".getBytes());
assertTrue(client.checkExists().forPath("/foo/bar") == null);
assertNull(client.checkExists().forPath("/foo/bar"));

CuratorTransactionResult ephemeralResult =
Iterables.find(results, CuratorTransactionResult.ofTypeAndPath(OperationType.CREATE, "/test-"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ public void testWithNamespace() throws Exception {
.and()
.commit();

assertTrue(client.checkExists().forPath("/foo") != null);
assertTrue(client.usingNamespace(null).checkExists().forPath("/galt/foo") != null);
assertNotNull(client.checkExists().forPath("/foo"));
assertNotNull(client.usingNamespace(null).checkExists().forPath("/galt/foo"));
assertArrayEquals(client.getData().forPath("/foo"), "two".getBytes());
assertTrue(client.checkExists().forPath("/foo/bar") == null);
assertNull(client.checkExists().forPath("/foo/bar"));

CuratorTransactionResult ephemeralResult =
Iterables.find(results, CuratorTransactionResult.ofTypeAndPath(OperationType.CREATE, "/test-"));
Expand Down Expand Up @@ -154,10 +154,10 @@ public void testWithCompression() throws Exception {
.and()
.commit();

assertTrue(client.checkExists().forPath("/foo") != null);
assertNotNull(client.checkExists().forPath("/foo"));
assertArrayEquals(client.getData().decompressed().forPath("/foo"), "five".getBytes());

assertTrue(client.checkExists().forPath("/bar") != null);
assertNotNull(client.checkExists().forPath("/bar"));
assertArrayEquals(client.getData().decompressed().forPath("/bar"), "two".getBytes());
assertEquals(client.getACL().forPath("/bar"), ZooDefs.Ids.READ_ACL_UNSAFE);

Expand All @@ -167,7 +167,7 @@ public void testWithCompression() throws Exception {
assertNotEquals(ephemeralResult.getResultPath(), "/test-");
assertTrue(ephemeralResult.getResultPath().startsWith("/test-"));

assertTrue(client.checkExists().forPath("/baz") != null);
assertNotNull(client.checkExists().forPath("/baz"));
assertArrayEquals(client.getData().decompressed().forPath("/baz"), "four".getBytes());
assertEquals(client.getACL().forPath("/baz"), ZooDefs.Ids.READ_ACL_UNSAFE);
} finally {
Expand All @@ -189,7 +189,7 @@ public void testBasic() throws Exception {
.and()
.commit();

assertTrue(client.checkExists().forPath("/foo/bar") != null);
assertNotNull(client.checkExists().forPath("/foo/bar"));
assertArrayEquals(client.getData().forPath("/foo/bar"), "snafu".getBytes());

CuratorTransactionResult fooResult =
Expand Down

0 comments on commit ed12072

Please sign in to comment.