org.apache.curator.test.Timing Java Examples
The following examples show how to use
org.apache.curator.test.Timing.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestWatcherIdentity.java From curator with Apache License 2.0 | 6 votes |
@Test public void testCuratorWatcher() throws Exception { Timing timing = new Timing(); CountCuratorWatcher watcher = new CountCuratorWatcher(); CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); try { client.start(); client.create().forPath(PATH); // Add twice the same watcher on the same path client.getData().usingWatcher(watcher).forPath(PATH); client.getData().usingWatcher(watcher).forPath(PATH); // Ok, let's test it client.setData().forPath(PATH, new byte[]{}); timing.sleepABit(); Assert.assertEquals(1, watcher.count.get()); } finally { CloseableUtils.closeQuietly(client); } }
Example #2
Source File: TestWatcherIdentity.java From curator with Apache License 2.0 | 6 votes |
@Test public void testZKWatcher() throws Exception { Timing timing = new Timing(); CountZKWatcher watcher = new CountZKWatcher(); CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); try { client.start(); client.create().forPath(PATH); // Add twice the same watcher on the same path client.getData().usingWatcher(watcher).forPath(PATH); client.getData().usingWatcher(watcher).forPath(PATH); // Ok, let's test it client.setData().forPath(PATH, new byte[]{}); timing.sleepABit(); Assert.assertEquals(1, watcher.count.get()); } finally { CloseableUtils.closeQuietly(client); } }
Example #3
Source File: TestPersistentNode.java From xian with Apache License 2.0 | 6 votes |
@Test public void testQuickCloseNodeExists() throws Exception { Timing timing = new Timing(); PersistentNode pen = null; CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); try { client.start(); client.create().creatingParentsIfNeeded().forPath("/test/one/two"); pen = new PersistentNode(client, CreateMode.PERSISTENT, false, "/test/one/two", new byte[0]); pen.start(); pen.close(); timing.sleepABit(); Assert.assertNull(client.checkExists().forPath("/test/one/two")); } finally { CloseableUtils.closeQuietly(pen); CloseableUtils.closeQuietly(client); } }
Example #4
Source File: TestWatcherIdentity.java From xian with Apache License 2.0 | 6 votes |
@Test public void testCuratorWatcher() throws Exception { Timing timing = new Timing(); CountCuratorWatcher watcher = new CountCuratorWatcher(); CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); try { client.start(); client.create().forPath(PATH); // Add twice the same watcher on the same path client.getData().usingWatcher(watcher).forPath(PATH); client.getData().usingWatcher(watcher).forPath(PATH); // Ok, let's test it client.setData().forPath(PATH, new byte[]{}); timing.sleepABit(); Assert.assertEquals(1, watcher.count.get()); } finally { CloseableUtils.closeQuietly(client); } }
Example #5
Source File: TestInterProcessMutexBase.java From curator with Apache License 2.0 | 6 votes |
@Test public void testLocker() throws Exception { final Timing timing = new Timing(); final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3)); try { client.start(); InterProcessLock lock = makeLock(client); try ( Locker locker = new Locker(lock, timing.milliseconds(), TimeUnit.MILLISECONDS) ) { Assert.assertTrue(lock.isAcquiredInThisProcess()); } Assert.assertFalse(lock.isAcquiredInThisProcess()); } finally { CloseableUtils.closeQuietly(client); } }
Example #6
Source File: TestFramework.java From curator with Apache License 2.0 | 6 votes |
@Test public void testGetSequentialChildren() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start(); try { Semaphore semaphore = new Semaphore(0); AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client); async.create().forPath("/head").thenRun(() -> { for ( int i = 0; i < 10; ++i ) { async.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/head/child").thenRun(semaphore::release); } }); Assert.assertTrue(new Timing().acquireSemaphore(semaphore, 10)); List<String> children = async.getChildren().forPath("/head").toCompletableFuture().get(); Assert.assertEquals(children.size(), 10); } finally { CloseableUtils.closeQuietly(client); } }
Example #7
Source File: TestFrameworkBackground.java From curator with Apache License 2.0 | 6 votes |
/** * Attempt a background operation while Zookeeper server is down. * Return code must be {@link org.apache.zookeeper.KeeperException.Code#CONNECTIONLOSS} */ @Test public void testCuratorCallbackOnError() throws Exception { Timing timing = new Timing(); final CountDownLatch latch = new CountDownLatch(1); try ( CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new RetryOneTime(1000)).build() ) { client.start(); AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client); // Stop the Zookeeper server server.stop(); // Attempt to retrieve children list async.getChildren().forPath("/").handle((children, e) -> { if ( e instanceof KeeperException.ConnectionLossException ) { latch.countDown(); } return null; }); // Check if the callback has been called with a correct return code Assert.assertTrue(timing.awaitLatch(latch), "Callback has not been called by curator !"); } }
Example #8
Source File: TestChildReaper.java From hadoop with Apache License 2.0 | 5 votes |
@Test public void testMultiPath() throws Exception { Timing timing = new Timing(); ChildReaper reaper = null; CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); try { client.start(); for ( int i = 0; i < 10; ++i ) { client.create().creatingParentsIfNeeded().forPath("/test1/" + Integer.toString(i)); client.create().creatingParentsIfNeeded().forPath("/test2/" + Integer.toString(i)); client.create().creatingParentsIfNeeded().forPath("/test3/" + Integer.toString(i)); } reaper = new ChildReaper(client, "/test2", Reaper.Mode.REAP_UNTIL_DELETE, 1); reaper.start(); reaper.addPath("/test1"); timing.forWaiting().sleepABit(); Stat stat = client.checkExists().forPath("/test1"); Assert.assertEquals(stat.getNumChildren(), 0); stat = client.checkExists().forPath("/test2"); Assert.assertEquals(stat.getNumChildren(), 0); stat = client.checkExists().forPath("/test3"); Assert.assertEquals(stat.getNumChildren(), 10); } finally { CloseableUtils.closeQuietly(reaper); CloseableUtils.closeQuietly(client); } }
Example #9
Source File: TestChildReaper.java From hadoop with Apache License 2.0 | 5 votes |
@Test public void testSimple() throws Exception { Timing timing = new Timing(); ChildReaper reaper = null; CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); try { client.start(); for ( int i = 0; i < 10; ++i ) { client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i)); } reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1); reaper.start(); timing.forWaiting().sleepABit(); Stat stat = client.checkExists().forPath("/test"); Assert.assertEquals(stat.getNumChildren(), 0); } finally { CloseableUtils.closeQuietly(reaper); CloseableUtils.closeQuietly(client); } }
Example #10
Source File: TestChildReaper.java From big-c with Apache License 2.0 | 5 votes |
@Test public void testNamespace() throws Exception { Timing timing = new Timing(); ChildReaper reaper = null; CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(server.getConnectString()) .sessionTimeoutMs(timing.session()) .connectionTimeoutMs(timing.connection()) .retryPolicy(new RetryOneTime(1)) .namespace("foo") .build(); try { client.start(); for ( int i = 0; i < 10; ++i ) { client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i)); } reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1); reaper.start(); timing.forWaiting().sleepABit(); Stat stat = client.checkExists().forPath("/test"); Assert.assertEquals(stat.getNumChildren(), 0); stat = client.usingNamespace(null).checkExists().forPath("/foo/test"); Assert.assertNotNull(stat); Assert.assertEquals(stat.getNumChildren(), 0); } finally { CloseableUtils.closeQuietly(reaper); CloseableUtils.closeQuietly(client); } }
Example #11
Source File: TestChildReaper.java From big-c with Apache License 2.0 | 5 votes |
@Test public void testMultiPath() throws Exception { Timing timing = new Timing(); ChildReaper reaper = null; CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); try { client.start(); for ( int i = 0; i < 10; ++i ) { client.create().creatingParentsIfNeeded().forPath("/test1/" + Integer.toString(i)); client.create().creatingParentsIfNeeded().forPath("/test2/" + Integer.toString(i)); client.create().creatingParentsIfNeeded().forPath("/test3/" + Integer.toString(i)); } reaper = new ChildReaper(client, "/test2", Reaper.Mode.REAP_UNTIL_DELETE, 1); reaper.start(); reaper.addPath("/test1"); timing.forWaiting().sleepABit(); Stat stat = client.checkExists().forPath("/test1"); Assert.assertEquals(stat.getNumChildren(), 0); stat = client.checkExists().forPath("/test2"); Assert.assertEquals(stat.getNumChildren(), 0); stat = client.checkExists().forPath("/test3"); Assert.assertEquals(stat.getNumChildren(), 10); } finally { CloseableUtils.closeQuietly(reaper); CloseableUtils.closeQuietly(client); } }
Example #12
Source File: TestInterProcessSemaphore.java From curator with Apache License 2.0 | 5 votes |
@Test public void testClientClose() throws Exception { final Timing timing = new Timing(); CuratorFramework client1 = null; CuratorFramework client2 = null; InterProcessSemaphoreV2 semaphore1; InterProcessSemaphoreV2 semaphore2; try { client1 = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); client2 = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); client1.start(); client2.start(); semaphore1 = new InterProcessSemaphoreV2(client1, "/test", 1); semaphore2 = new InterProcessSemaphoreV2(client2, "/test", 1); Lease lease = semaphore2.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS); Assert.assertNotNull(lease); lease.close(); lease = semaphore1.acquire(10, TimeUnit.SECONDS); Assert.assertNotNull(lease); client1.close(); // should release any held leases client1 = null; Assert.assertNotNull(semaphore2.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS)); } finally { TestCleanState.closeAndTestClean(client1); TestCleanState.closeAndTestClean(client2); } }
Example #13
Source File: TestChildReaper.java From big-c with Apache License 2.0 | 5 votes |
@Test public void testSomeNodes() throws Exception { Timing timing = new Timing(); ChildReaper reaper = null; CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); try { client.start(); Random r = new Random(); int nonEmptyNodes = 0; for ( int i = 0; i < 10; ++i ) { client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i)); if ( r.nextBoolean() ) { client.create().forPath("/test/" + Integer.toString(i) + "/foo"); ++nonEmptyNodes; } } reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1); reaper.start(); timing.forWaiting().sleepABit(); Stat stat = client.checkExists().forPath("/test"); Assert.assertEquals(stat.getNumChildren(), nonEmptyNodes); } finally { CloseableUtils.closeQuietly(reaper); CloseableUtils.closeQuietly(client); } }
Example #14
Source File: TestChildReaper.java From hadoop with Apache License 2.0 | 5 votes |
@Test public void testNamespace() throws Exception { Timing timing = new Timing(); ChildReaper reaper = null; CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(server.getConnectString()) .sessionTimeoutMs(timing.session()) .connectionTimeoutMs(timing.connection()) .retryPolicy(new RetryOneTime(1)) .namespace("foo") .build(); try { client.start(); for ( int i = 0; i < 10; ++i ) { client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i)); } reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1); reaper.start(); timing.forWaiting().sleepABit(); Stat stat = client.checkExists().forPath("/test"); Assert.assertEquals(stat.getNumChildren(), 0); stat = client.usingNamespace(null).checkExists().forPath("/foo/test"); Assert.assertNotNull(stat); Assert.assertEquals(stat.getNumChildren(), 0); } finally { CloseableUtils.closeQuietly(reaper); CloseableUtils.closeQuietly(client); } }
Example #15
Source File: SemaphoreClient.java From xian with Apache License 2.0 | 5 votes |
SemaphoreClient(String connectionString, String semaphorePath, Callable<Void> operation) throws IOException { Timing timing = new Timing(); this.client = CuratorFrameworkFactory.newClient(connectionString, timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3)); client.start(); this.semaphorePath = semaphorePath; this.operation = operation; }
Example #16
Source File: TestFramework.java From xian with Apache License 2.0 | 5 votes |
@Test public void testOverrideCreateParentContainers() throws Exception { if ( !checkForContainers() ) { return; } CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(server.getConnectString()) .retryPolicy(new RetryOneTime(1)) .dontUseContainerParents() .build(); try { client.start(); client.create().creatingParentContainersIfNeeded().forPath("/one/two/three", "foo".getBytes()); byte[] data = client.getData().forPath("/one/two/three"); Assert.assertEquals(data, "foo".getBytes()); client.delete().forPath("/one/two/three"); new Timing().sleepABit(); Assert.assertNotNull(client.checkExists().forPath("/one/two")); new Timing().sleepABit(); Assert.assertNotNull(client.checkExists().forPath("/one")); } finally { CloseableUtils.closeQuietly(client); } }
Example #17
Source File: TestFramework.java From xian with Apache License 2.0 | 5 votes |
@Test public void testCreateParentContainers() throws Exception { if ( !checkForContainers() ) { return; } CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); CuratorFramework client = builder.connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).build(); try { client.start(); client.create().creatingParentContainersIfNeeded().forPath("/one/two/three", "foo".getBytes()); byte[] data = client.getData().forPath("/one/two/three"); Assert.assertEquals(data, "foo".getBytes()); client.delete().forPath("/one/two/three"); new Timing().sleepABit(); Assert.assertNull(client.checkExists().forPath("/one/two")); new Timing().sleepABit(); Assert.assertNull(client.checkExists().forPath("/one")); } finally { CloseableUtils.closeQuietly(client); } }
Example #18
Source File: SemaphoreClient.java From curator with Apache License 2.0 | 5 votes |
SemaphoreClient(String connectionString, String semaphorePath, Callable<Void> operation) throws IOException { Timing timing = new Timing(); this.client = CuratorFrameworkFactory.newClient(connectionString, timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3)); client.start(); this.semaphorePath = semaphorePath; this.operation = operation; }
Example #19
Source File: TestWatchesBuilder.java From curator with Apache License 2.0 | 5 votes |
/** * Test the case where we try and remove an unregistered watcher but have the quietly flag set. In this case we expect success. * @throws Exception */ @Test public void testRemoveUnregisteredWatcherQuietly() throws Exception { Timing timing = new Timing(); CuratorFramework client = CuratorFrameworkFactory.builder(). connectString(server.getConnectString()). retryPolicy(new RetryOneTime(1)). build(); try { client.start(); final AtomicBoolean watcherRemoved = new AtomicBoolean(false); final String path = "/"; Watcher watcher = new BooleanWatcher(path, watcherRemoved, EventType.DataWatchRemoved); client.watches().remove(watcher).quietly().forPath(path); timing.sleepABit(); //There should be no watcher removed as none were registered. Assert.assertEquals(watcherRemoved.get(), false); } finally { CloseableUtils.closeQuietly(client); } }
Example #20
Source File: TestExhibitorEnsembleProvider.java From xian with Apache License 2.0 | 5 votes |
@Test public void testSimple() throws Exception { Exhibitors exhibitors = new Exhibitors(Lists.newArrayList("foo", "bar"), 1000, dummyConnectionStringProvider); ExhibitorRestClient mockRestClient = new ExhibitorRestClient() { @Override public String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception { return "count=1&port=" + server.getPort() + "&server0=localhost"; } }; ExhibitorEnsembleProvider provider = new ExhibitorEnsembleProvider(exhibitors, mockRestClient, "/foo", 10, new RetryOneTime(1)); provider.pollForInitialEnsemble(); Timing timing = new Timing(); CuratorZookeeperClient client = new CuratorZookeeperClient(provider, timing.session(), timing.connection(), null, new ExponentialBackoffRetry(timing.milliseconds(), 3)); client.start(); try { client.blockUntilConnectedOrTimedOut(); client.getZooKeeper().exists("/", false); } catch ( Exception e ) { Assert.fail("provider.getConnectionString(): " + provider.getConnectionString() + " server.getPort(): " + server.getPort(), e); } finally { client.close(); } }
Example #21
Source File: TestServiceDiscovery.java From curator with Apache License 2.0 | 5 votes |
@Test public void testCrashedInstance() throws Exception { CuratorFramework client = null; ServiceDiscovery<String> discovery = null; try { Timing timing = new Timing(); client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); client.start(); ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build(); discovery = new ServiceDiscoveryImpl<String>(client, "/test", new JsonInstanceSerializer<String>(String.class), instance, false); discovery.start(); Assert.assertEquals(discovery.queryForInstances("test").size(), 1); client.getZookeeperClient().getZooKeeper().getTestable().injectSessionExpiration(); Thread.sleep(timing.multiple(1.5).session()); Assert.assertEquals(discovery.queryForInstances("test").size(), 1); } finally { CloseableUtils.closeQuietly(discovery); CloseableUtils.closeQuietly(client); } }
Example #22
Source File: TestWatcherRemovalManager.java From curator with Apache License 2.0 | 5 votes |
@Test public void testSameWatcherDifferentPaths1Triggered() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); try { client.start(); WatcherRemovalFacade removerClient = (WatcherRemovalFacade)client.newWatcherRemoveCuratorFramework(); final CountDownLatch latch = new CountDownLatch(1); Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { latch.countDown(); } }; removerClient.checkExists().usingWatcher(watcher).forPath("/a/b/c"); removerClient.checkExists().usingWatcher(watcher).forPath("/d/e/f"); removerClient.create().creatingParentsIfNeeded().forPath("/d/e/f"); Timing timing = new Timing(); Assert.assertTrue(timing.awaitLatch(latch)); timing.sleepABit(); removerClient.removeWatchers(); } finally { TestCleanState.closeAndTestClean(client); } }
Example #23
Source File: TestReaper.java From xian with Apache License 2.0 | 5 votes |
private void testRemove(String namespace) throws Exception { Timing timing = new Timing(); Reaper reaper = null; CuratorFramework client = makeClient(timing, namespace); try { client.start(); client.create().creatingParentsIfNeeded().forPath("/one/two/three"); Assert.assertNotNull(client.checkExists().forPath("/one/two/three")); reaper = new Reaper(client, 100); reaper.start(); reaper.addPath("/one/two/three"); timing.sleepABit(); Assert.assertNull(client.checkExists().forPath("/one/two/three")); Assert.assertTrue(reaper.removePath("/one/two/three")); client.create().forPath("/one/two/three"); timing.sleepABit(); Assert.assertNotNull(client.checkExists().forPath("/one/two/three")); } finally { CloseableUtils.closeQuietly(reaper); CloseableUtils.closeQuietly(client); } }
Example #24
Source File: TestReaper.java From xian with Apache License 2.0 | 5 votes |
private CuratorFramework makeClient(Timing timing, String namespace) throws IOException { CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectionTimeoutMs(timing.connection()).sessionTimeoutMs(timing.session()).connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)); if ( namespace != null ) { builder = builder.namespace(namespace); } return builder.build(); }
Example #25
Source File: TestReaper.java From xian with Apache License 2.0 | 5 votes |
private void testReapUntilGone(String namespace) throws Exception { Timing timing = new Timing(); Reaper reaper = null; CuratorFramework client = makeClient(timing, namespace); try { client.start(); reaper = new Reaper(client, 100); reaper.start(); reaper.addPath("/one/two/three", Reaper.Mode.REAP_UNTIL_GONE); timing.sleepABit(); client.create().creatingParentsIfNeeded().forPath("/one/two/three"); Assert.assertNotNull(client.checkExists().forPath("/one/two/three")); reaper.addPath("/one/two/three", Reaper.Mode.REAP_UNTIL_GONE); timing.sleepABit(); Assert.assertNull(client.checkExists().forPath("/one/two/three")); } finally { CloseableUtils.closeQuietly(reaper); CloseableUtils.closeQuietly(client); } }
Example #26
Source File: TestWatchesBuilder.java From curator with Apache License 2.0 | 5 votes |
@Test public void testRemoveWatch() throws Exception { Timing timing = new Timing(); CuratorFrameworkImpl client = (CuratorFrameworkImpl)CuratorFrameworkFactory.builder(). connectString(server.getConnectString()). retryPolicy(new RetryOneTime(1)). build(); try { client.start(); final CountDownLatch removedLatch = new CountDownLatch(1); final String path = "/"; Watcher watcher = new CountDownWatcher(path, removedLatch, EventType.DataWatchRemoved); client.checkExists().usingWatcher(watcher).forPath(path); client.watches().remove(watcher).forPath(path); Assert.assertTrue(timing.awaitLatch(removedLatch), "Timed out waiting for watch removal"); } finally { CloseableUtils.closeQuietly(client); } }
Example #27
Source File: TestSharedCount.java From xian with Apache License 2.0 | 5 votes |
@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); } }
Example #28
Source File: TestLeaderSelectorWithExecutor.java From curator with Apache License 2.0 | 5 votes |
@Test public void test() throws Exception { Timing timing = new Timing(); LeaderSelector leaderSelector = null; CuratorFramework client = CuratorFrameworkFactory.builder() .retryPolicy(new ExponentialBackoffRetry(100, 3)) .connectString(server.getConnectString()) .sessionTimeoutMs(timing.session()) .connectionTimeoutMs(timing.connection()) .build(); try { client.start(); MyLeaderSelectorListener listener = new MyLeaderSelectorListener(); ExecutorService executorPool = Executors.newFixedThreadPool(20); leaderSelector = new LeaderSelector(client, "/test", threadFactory, executorPool, listener); leaderSelector.autoRequeue(); leaderSelector.start(); timing.sleepABit(); Assert.assertEquals(listener.getLeaderCount(), 1); } finally { CloseableUtils.closeQuietly(leaderSelector); CloseableUtils.closeQuietly(client); } }
Example #29
Source File: TestWatchesBuilder.java From curator with Apache License 2.0 | 5 votes |
@Test public void testRemoveAllDataWatches() throws Exception { Timing timing = new Timing(); CuratorFramework client = CuratorFrameworkFactory.builder(). connectString(server.getConnectString()). retryPolicy(new RetryOneTime(1)). build(); try { client.start(); final String path = "/"; final AtomicBoolean removedFlag = new AtomicBoolean(false); final CountDownLatch removedLatch = new CountDownLatch(1); Watcher watcher1 = new BooleanWatcher(path, removedFlag, EventType.ChildWatchRemoved); Watcher watcher2 = new CountDownWatcher(path, removedLatch, EventType.DataWatchRemoved); client.getChildren().usingWatcher(watcher1).forPath(path); client.checkExists().usingWatcher(watcher2).forPath(path); client.watches().removeAll().ofType(WatcherType.Data).forPath(path); Assert.assertTrue(timing.awaitLatch(removedLatch), "Timed out waiting for watch removal"); Assert.assertEquals(removedFlag.get(), false); } finally { CloseableUtils.closeQuietly(client); } }
Example #30
Source File: TestInterProcessSemaphore.java From xian with Apache License 2.0 | 5 votes |
@Test public void testClientClose() throws Exception { final Timing timing = new Timing(); CuratorFramework client1 = null; CuratorFramework client2 = null; InterProcessSemaphoreV2 semaphore1; InterProcessSemaphoreV2 semaphore2; try { client1 = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); client2 = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); client1.start(); client2.start(); semaphore1 = new InterProcessSemaphoreV2(client1, "/test", 1); semaphore2 = new InterProcessSemaphoreV2(client2, "/test", 1); Lease lease = semaphore2.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS); Assert.assertNotNull(lease); lease.close(); lease = semaphore1.acquire(10, TimeUnit.SECONDS); Assert.assertNotNull(lease); client1.close(); // should release any held leases client1 = null; Assert.assertNotNull(semaphore2.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS)); } finally { CloseableUtils.closeQuietly(client1); CloseableUtils.closeQuietly(client2); } }