Skip to content

Commit

Permalink
Merge branch 'master' into compat-jdk21
Browse files Browse the repository at this point in the history
  • Loading branch information
tisonkun committed Oct 17, 2023
2 parents 5766d43 + 2a94b2d commit 10b3078
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 257 deletions.
6 changes: 0 additions & 6 deletions curator-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
Expand Down
12 changes: 8 additions & 4 deletions curator-client/src/test/java/org/apache/curator/BasicTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.Duration;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.curator.ensemble.fixed.FixedEnsembleProvider;
import org.apache.curator.retry.RetryOneTime;
import org.apache.curator.test.BaseClassForTests;
Expand All @@ -40,13 +41,16 @@
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class BasicTests extends BaseClassForTests {
@Test
public void testFactory() throws Exception {
final ZooKeeper mockZookeeper = Mockito.mock(ZooKeeper.class);
ZookeeperFactory zookeeperFactory = (connectString, sessionTimeout, watcher, canBeReadOnly) -> mockZookeeper;
final AtomicReference<ZooKeeper> expectedZooKeeper = new AtomicReference<>();
ZookeeperFactory zookeeperFactory = (connectString, sessionTimeout, watcher, canBeReadOnly) -> {
final ZooKeeper zooKeeper = new ZooKeeper(connectString, sessionTimeout, watcher, canBeReadOnly);
expectedZooKeeper.set(zooKeeper);
return zooKeeper;
};
CuratorZookeeperClient client = new CuratorZookeeperClient(
zookeeperFactory,
new FixedEnsembleProvider(server.getConnectString()),
Expand All @@ -56,7 +60,7 @@ public void testFactory() throws Exception {
new RetryOneTime(1),
false);
client.start();
assertEquals(client.getZooKeeper(), mockZookeeper);
assertEquals(client.getZooKeeper(), expectedZooKeeper.get());
}

@Test
Expand Down
115 changes: 0 additions & 115 deletions curator-client/src/test/java/org/apache/curator/TestEnsurePath.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.times;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryForever;
import org.apache.curator.retry.RetryOneTime;
Expand All @@ -34,7 +34,6 @@
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class TestRetryLoop extends BaseClassForTests {
@Test
Expand Down Expand Up @@ -133,13 +132,14 @@ public void testRetryLoop() throws Exception {
@Test
public void testRetryForever() throws Exception {
int retryIntervalMs = 1;
RetrySleeper sleeper = Mockito.mock(RetrySleeper.class);
AtomicLong retryTimes = new AtomicLong();
RetrySleeper sleeper = (time, unit) -> retryTimes.incrementAndGet();
RetryForever retryForever = new RetryForever(retryIntervalMs);

for (int i = 0; i < 10; i++) {
boolean allowed = retryForever.allowRetry(i, 0, sleeper);
assertTrue(allowed);
Mockito.verify(sleeper, times(i + 1)).sleepFor(retryIntervalMs, TimeUnit.MILLISECONDS);
assertEquals(i + 1, retryTimes.get());
}
}

Expand Down
12 changes: 0 additions & 12 deletions curator-framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,6 @@
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ public interface CuratorWatcher {
* @param event the event
* @throws Exception any exceptions to log
*/
public void process(WatchedEvent event) throws Exception;
void process(WatchedEvent event) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -115,6 +113,7 @@ public void teardown() throws Exception {
super.teardown();
}

@Test
public void testWaitForShutdownTimeoutMs() throws Exception {
final BlockingQueue<Integer> timeoutQueue = new ArrayBlockingQueue<>(1);
ZookeeperFactory zookeeperFactory = new ZookeeperFactory() {
Expand Down Expand Up @@ -755,19 +754,6 @@ public void testNullNamespace() {
CloseableUtils.closeQuietly(client);
}

@Test
public void testNoPartialConstruction() {
try (MockedConstruction<CuratorFrameworkImpl> construction =
Mockito.mockConstruction(CuratorFrameworkImpl.class)) {
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
.connectString(server.getConnectString())
.retryPolicy(new RetryOneTime(1));
// Invoke constructor directly to make the construction more visible.
CuratorFramework client = new CuratorFrameworkImpl(builder);
assertEquals(Collections.singletonList(client), construction.constructed());
}
}

@Test
public void testCustomCallback() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
package org.apache.curator.framework.imps;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.google.common.collect.Sets;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
Expand All @@ -37,11 +40,9 @@
import org.apache.zookeeper.Watcher;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class TestWatcherIdentity extends BaseClassForTests {
private static final String PATH = "/foo";
private static final int TIMEOUT_MS = 100000;

private static class CountZKWatcher implements Watcher {
private final AtomicInteger count = new AtomicInteger(0);
Expand All @@ -52,6 +53,34 @@ public void process(WatchedEvent event) {
}
}

private static class TestEventWatcher implements CuratorWatcher {
private final Timing timing;
private final BlockingQueue<WatchedEvent> events = new LinkedBlockingQueue<>();

private TestEventWatcher(Timing timing) {
this.timing = timing;
}

@Override
public void process(WatchedEvent event) {
events.add(event);
}

private void assertEvent(Watcher.Event.EventType type, String path) throws InterruptedException {
WatchedEvent event = events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS);
assertNotNull(event);
assertEquals(type, event.getType());
assertEquals(path, event.getPath());
}

private void assertNoMoreEvents() throws InterruptedException {
timing.sleepABit();
assertTrue(
events.isEmpty(),
String.format("Expected no events, found %d; first event: %s", events.size(), events.peek()));
}
}

@Test
public void testSameWatcherPerZKDocs() throws Exception {
CountZKWatcher actualWatcher = new CountZKWatcher();
Expand Down Expand Up @@ -81,10 +110,8 @@ public void testSameWatcherPerZKDocs() throws Exception {

@Test
public void testSameCuratorWatcherPerZKDocs() throws Exception {
// Construct mock object
CuratorWatcher actualWatcher = mock(CuratorWatcher.class);

Timing timing = new Timing();
TestEventWatcher actualWatcher = new TestEventWatcher(timing);
CuratorFramework client = CuratorFrameworkFactory.newClient(
server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
Expand All @@ -97,28 +124,26 @@ public void testSameCuratorWatcherPerZKDocs() throws Exception {

client.setData().forPath("/test", "foo".getBytes());
client.delete().forPath("/test");
Mockito.verify(actualWatcher, Mockito.timeout(TIMEOUT_MS).times(1)).process(any(WatchedEvent.class));
actualWatcher.assertEvent(Watcher.Event.EventType.NodeDataChanged, "/test");

client.create().forPath("/test");
client.checkExists().usingWatcher(actualWatcher).forPath("/test");
client.delete().forPath("/test");
Mockito.verify(actualWatcher, Mockito.timeout(TIMEOUT_MS).times(2)).process(any(WatchedEvent.class));
actualWatcher.assertEvent(Watcher.Event.EventType.NodeDeleted, "/test");
actualWatcher.assertNoMoreEvents();
} finally {
CloseableUtils.closeQuietly(client);
}
}

@Test
public void testSetAddition() {
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {}
};
Watcher watcher = event -> {};
NamespaceWatcher namespaceWatcher1 = new NamespaceWatcher(null, watcher, "/foo");
NamespaceWatcher namespaceWatcher2 = new NamespaceWatcher(null, watcher, "/foo");
assertEquals(namespaceWatcher1, namespaceWatcher2);
assertFalse(namespaceWatcher1.equals(watcher));
assertFalse(watcher.equals(namespaceWatcher1));
assertNotEquals(namespaceWatcher1, watcher);
assertNotEquals(watcher, namespaceWatcher1);
Set<Watcher> set = Sets.newHashSet();
set.add(namespaceWatcher1);
set.add(namespaceWatcher2);
Expand All @@ -128,8 +153,7 @@ public void process(WatchedEvent event) {}
@Test
public void testCuratorWatcher() throws Exception {
Timing timing = new Timing();
// Construct mock object
CuratorWatcher watcher = mock(CuratorWatcher.class);
TestEventWatcher watcher = new TestEventWatcher(timing);
CuratorFramework client = CuratorFrameworkFactory.newClient(
server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
Expand All @@ -140,7 +164,8 @@ public void testCuratorWatcher() throws Exception {
client.getData().usingWatcher(watcher).forPath(PATH);
// Ok, let's test it
client.setData().forPath(PATH, new byte[] {});
Mockito.verify(watcher, Mockito.timeout(TIMEOUT_MS).times(1)).process(any(WatchedEvent.class));
watcher.assertEvent(Watcher.Event.EventType.NodeDataChanged, PATH);
watcher.assertNoMoreEvents();
} finally {
CloseableUtils.closeQuietly(client);
}
Expand Down
Loading

0 comments on commit 10b3078

Please sign in to comment.