use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestDistributedQueue method testFlush.
@Test
public void testFlush() throws Exception {
final Timing timing = new Timing();
final CountDownLatch latch = new CountDownLatch(1);
DistributedQueue<TestQueueItem> queue = null;
final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
try {
final AtomicBoolean firstTime = new AtomicBoolean(true);
queue = new DistributedQueue<TestQueueItem>(client, null, serializer, "/test", new ThreadFactoryBuilder().build(), MoreExecutors.directExecutor(), 10, true, null, QueueBuilder.NOT_SET, true, 0) {
@Override
void internalCreateNode(final String path, final byte[] bytes, final BackgroundCallback callback) throws Exception {
if (firstTime.compareAndSet(true, false)) {
Executors.newSingleThreadExecutor().submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
latch.await();
timing.sleepABit();
client.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).inBackground(callback).forPath(path, bytes);
return null;
}
});
} else {
super.internalCreateNode(path, bytes, callback);
}
}
};
queue.start();
queue.put(new TestQueueItem("1"));
Assert.assertFalse(queue.flushPuts(timing.forWaiting().seconds(), TimeUnit.SECONDS));
latch.countDown();
Assert.assertTrue(queue.flushPuts(timing.forWaiting().seconds(), TimeUnit.SECONDS));
} finally {
if (latch.getCount() > 0) {
latch.countDown();
}
CloseableUtils.closeQuietly(queue);
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestSharedCount method testMultiClientVersioned.
@Test
public void testMultiClientVersioned() throws Exception {
Timing timing = new Timing();
CuratorFramework client1 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
CuratorFramework client2 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
SharedCount count1 = new SharedCount(client1, "/count", 0);
SharedCount count2 = new SharedCount(client2, "/count", 0);
try {
client1.start();
client2.start();
count1.start();
count2.start();
VersionedValue<Integer> versionedValue = count1.getVersionedValue();
Assert.assertTrue(count1.trySetCount(versionedValue, 10));
timing.sleepABit();
versionedValue = count2.getVersionedValue();
Assert.assertTrue(count2.trySetCount(versionedValue, 20));
timing.sleepABit();
VersionedValue<Integer> versionedValue1 = count1.getVersionedValue();
VersionedValue<Integer> versionedValue2 = count2.getVersionedValue();
Assert.assertTrue(count2.trySetCount(versionedValue2, 30));
Assert.assertFalse(count1.trySetCount(versionedValue1, 40));
versionedValue1 = count1.getVersionedValue();
Assert.assertTrue(count1.trySetCount(versionedValue1, 40));
} finally {
CloseableUtils.closeQuietly(count2);
CloseableUtils.closeQuietly(count1);
CloseableUtils.closeQuietly(client2);
CloseableUtils.closeQuietly(client1);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestPathChildrenCache method testUpdateWhenNotCachingData.
@Test
public void testUpdateWhenNotCachingData() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
try {
final CountDownLatch updatedLatch = new CountDownLatch(1);
final CountDownLatch addedLatch = new CountDownLatch(1);
client.create().creatingParentsIfNeeded().forPath("/test");
PathChildrenCache cache = new PathChildrenCache(client, "/test", false);
cache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
if (event.getType() == PathChildrenCacheEvent.Type.CHILD_UPDATED) {
updatedLatch.countDown();
} else if (event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED) {
addedLatch.countDown();
}
}
});
cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
client.create().forPath("/test/foo", "first".getBytes());
Assert.assertTrue(timing.awaitLatch(addedLatch));
client.setData().forPath("/test/foo", "something new".getBytes());
Assert.assertTrue(timing.awaitLatch(updatedLatch));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestPathChildrenCache method testAsyncInitialPopulation.
@Test
public void testAsyncInitialPopulation() throws Exception {
Timing timing = new Timing();
PathChildrenCache cache = null;
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
client.create().forPath("/test");
client.create().forPath("/test/one", "hey there".getBytes());
final BlockingQueue<PathChildrenCacheEvent> events = new LinkedBlockingQueue<PathChildrenCacheEvent>();
cache = new PathChildrenCache(client, "/test", true);
cache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
events.offer(event);
}
});
cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
PathChildrenCacheEvent event = events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS);
Assert.assertEquals(event.getType(), PathChildrenCacheEvent.Type.CHILD_ADDED);
event = events.poll(timing.forWaiting().seconds(), TimeUnit.SECONDS);
Assert.assertEquals(event.getType(), PathChildrenCacheEvent.Type.INITIALIZED);
Assert.assertEquals(event.getInitialData().size(), 1);
} finally {
CloseableUtils.closeQuietly(cache);
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestPathChildrenCache method testIssue27.
// see https://github.com/Netflix/curator/issues/27 - was caused by not comparing old->new data
@Test
public void testIssue27() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
try {
client.create().forPath("/base");
client.create().forPath("/base/a");
client.create().forPath("/base/b");
client.create().forPath("/base/c");
client.getChildren().forPath("/base");
final List<PathChildrenCacheEvent.Type> events = Lists.newArrayList();
final Semaphore semaphore = new Semaphore(0);
PathChildrenCache cache = new PathChildrenCache(client, "/base", true);
cache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
events.add(event.getType());
semaphore.release();
}
});
cache.start();
Assert.assertTrue(timing.acquireSemaphore(semaphore, 3));
client.delete().forPath("/base/a");
Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
client.create().forPath("/base/a");
Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
List<PathChildrenCacheEvent.Type> expected = Lists.newArrayList(PathChildrenCacheEvent.Type.CHILD_ADDED, PathChildrenCacheEvent.Type.CHILD_ADDED, PathChildrenCacheEvent.Type.CHILD_ADDED, PathChildrenCacheEvent.Type.CHILD_REMOVED, PathChildrenCacheEvent.Type.CHILD_ADDED);
Assert.assertEquals(expected, events);
} finally {
client.close();
}
}
Aggregations