Java Code Examples for org.apache.curator.test.Timing#sleepABit()

The following examples show how to use org.apache.curator.test.Timing#sleepABit() . 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: TestPersistentNode.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuickClose() 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();
        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 2
Source File: TestWatcherIdentity.java    From curator with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: TestWatcherIdentity.java    From curator with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: TestPathChildrenCache.java    From curator with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteNodeAfterCloseDoesntCallExecutor()
    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("/test");

        final ExecuteCalledWatchingExecutorService exec = new ExecuteCalledWatchingExecutorService(Executors.newSingleThreadExecutor());
        try ( PathChildrenCache cache = new PathChildrenCache(client, "/test", true, false, exec) )
        {
            cache.start();
            client.create().forPath("/test/one", "hey there".getBytes());

            cache.rebuild();
            Assert.assertEquals(new String(cache.getCurrentData("/test/one").getData()), "hey there");
            Assert.assertTrue(exec.isExecuteCalled());

            exec.setExecuteCalled(false);
        }
        Assert.assertFalse(exec.isExecuteCalled());

        client.delete().forPath("/test/one");
        timing.sleepABit();
        Assert.assertFalse(exec.isExecuteCalled());
    }
    finally
    {
        TestCleanState.closeAndTestClean(client);
    }

}
 
Example 5
Source File: TestWatcherRemovalManager.java    From curator with Apache License 2.0 5 votes vote down vote up
@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 6
Source File: TestReaper.java    From xian with Apache License 2.0 5 votes vote down vote up
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 7
Source File: TestQueueSharder.java    From curator with Apache License 2.0 5 votes vote down vote up
@Test
public void     testSharderWatchSync() throws Exception
{
    Timing                  timing = new Timing();
    CuratorFramework        client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));

    final BlockingQueueConsumer<String>     consumer = makeConsumer(null);
    QueueAllocator<String, DistributedQueue<String>>    distributedQueueAllocator = makeAllocator(consumer);
    QueueSharderPolicies        policies = QueueSharderPolicies.builder().newQueueThreshold(2).thresholdCheckMs(1).build();

    QueueSharder<String, DistributedQueue<String>>  sharder1 = new QueueSharder<String, DistributedQueue<String>>(client, distributedQueueAllocator, "/queues", "/leader", policies);
    QueueSharder<String, DistributedQueue<String>>  sharder2 = new QueueSharder<String, DistributedQueue<String>>(client, distributedQueueAllocator, "/queues", "/leader", policies);
    try
    {
        client.start();
        sharder1.start();
        sharder2.start();

        for ( int i = 0; i < 20; ++i )
        {
            sharder1.getQueue().put(Integer.toString(i));
        }
        timing.sleepABit();

        Assert.assertTrue((sharder1.getShardQty() > 1) || (sharder2.getShardQty() > 1));
        timing.forWaiting().sleepABit();
        Assert.assertEquals(sharder1.getShardQty(), sharder2.getShardQty());
    }
    finally
    {
        timing.sleepABit(); // let queues clear
        CloseableUtils.closeQuietly(sharder1);
        CloseableUtils.closeQuietly(sharder2);
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 8
Source File: TestPersistentNode.java    From curator with Apache License 2.0 5 votes vote down vote up
@Test
public void testEphemeralSequentialWithProtectionReconnection() 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");

        pen = new PersistentNode(client, CreateMode.EPHEMERAL_SEQUENTIAL, true, "/test/one/two", new byte[0]);
        pen.start();
        List<String> children = client.getChildren().forPath("/test/one");
        System.out.println("children before restart: "+children);
        Assert.assertEquals(1, children.size());
        server.stop();
        timing.sleepABit();
        server.restart();
        timing.sleepABit();
        List<String> childrenAfter = client.getChildren().forPath("/test/one");
        System.out.println("children after restart: "+childrenAfter);
        Assert.assertEquals(children, childrenAfter, "unexpected znodes: "+childrenAfter);
    }
    finally
    {
        CloseableUtils.closeQuietly(pen);
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 9
Source File: TestLeaderLatchCluster.java    From xian with Apache License 2.0 5 votes vote down vote up
private ClientAndLatch waitForALeader(List<ClientAndLatch> latches, Timing timing) throws InterruptedException
{
    for ( int i = 0; i < MAX_LOOPS; ++i )
    {
        List<ClientAndLatch> leaders = getLeaders(latches);
        if ( leaders.size() != 0 )
        {
            return leaders.get(0);
        }
        timing.sleepABit();
    }
    return null;
}
 
Example 10
Source File: TestLeaderLatch.java    From xian with Apache License 2.0 5 votes vote down vote up
private List<LeaderLatch> waitForALeader(List<LeaderLatch> latches, Timing timing) throws InterruptedException
{
    for ( int i = 0; i < MAX_LOOPS; ++i )
    {
        List<LeaderLatch> leaders = getLeaders(latches);
        if ( leaders.size() != 0 )
        {
            return leaders;
        }
        timing.sleepABit();
    }
    return Lists.newArrayList();
}
 
Example 11
Source File: TestLeaderLatch.java    From xian with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateDeleteRace() throws Exception
{
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try
    {
        client.start();
        client.create().creatingParentsIfNeeded().forPath(PATH_NAME);

        LeaderLatch latch = new LeaderLatch(client, PATH_NAME);

        latch.debugResetWaitLatch = new CountDownLatch(1);

        latch.start();
        latch.close();

        timing.sleepABit();

        latch.debugResetWaitLatch.countDown();

        timing.sleepABit();

        Assert.assertEquals(client.getChildren().forPath(PATH_NAME).size(), 0);

    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 12
Source File: TestLeaderLatch.java    From xian with Apache License 2.0 5 votes vote down vote up
@Test
public void testResetRace() throws Exception
{
    Timing timing = new Timing();
    LeaderLatch latch = null;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try
    {
        client.start();
        latch = new LeaderLatch(client, PATH_NAME);

        latch.debugResetWaitLatch = new CountDownLatch(1);
        latch.start();  // will call reset()
        latch.reset();  // should not result in two nodes

        timing.sleepABit();

        latch.debugResetWaitLatch.countDown();

        timing.sleepABit();

        Assert.assertEquals(client.getChildren().forPath(PATH_NAME).size(), 1);
    }
    finally
    {
        CloseableUtils.closeQuietly(latch);
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 13
Source File: TestPathChildrenCache.java    From xian with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnsurePath() throws Exception
{
    Timing timing = new Timing();

    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try
    {
        PathChildrenCache cache = new PathChildrenCache(client, "/one/two/three", false);
        cache.start();
        timing.sleepABit();

        try
        {
            client.create().forPath("/one/two/three/four");
        }
        catch ( KeeperException.NoNodeException e )
        {
            Assert.fail("Path should exist", e);
        }
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 14
Source File: TestLeaderLatch.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void testCallbackDontNotify() throws Exception
{
    final AtomicLong masterCounter = new AtomicLong(0);
    final AtomicLong notLeaderCounter = new AtomicLong(0);

    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));

    final LeaderLatch leader = new LeaderLatch(client, PATH_NAME);
    final LeaderLatch notifiedLeader = new LeaderLatch(client, PATH_NAME, "", LeaderLatch.CloseMode.NOTIFY_LEADER);

    leader.addListener(new LeaderLatchListener()
    {
        @Override
        public void isLeader()
        {
        }

        @Override
        public void notLeader()
        {
            masterCounter.incrementAndGet();
        }
    });

    notifiedLeader.addListener(new LeaderLatchListener()
    {
        @Override
        public void isLeader()
        {
        }

        @Override
        public void notLeader()
        {
            notLeaderCounter.incrementAndGet();
        }
    });

    try
    {
        client.start();

        leader.start();

        timing.sleepABit();

        notifiedLeader.start();

        timing.sleepABit();

        notifiedLeader.close();

        timing.sleepABit();

        // Test the close override
        leader.close(LeaderLatch.CloseMode.NOTIFY_LEADER);

        Assert.assertEquals(leader.getState(), LeaderLatch.State.CLOSED);
        Assert.assertEquals(notifiedLeader.getState(), LeaderLatch.State.CLOSED);

        Assert.assertEquals(masterCounter.get(), 1);
        Assert.assertEquals(notLeaderCounter.get(), 0);
    }
    finally
    {
        if ( leader.getState() != LeaderLatch.State.CLOSED )
        {
            CloseableUtils.closeQuietly(leader);
        }
        if ( notifiedLeader.getState() != LeaderLatch.State.CLOSED )
        {
            CloseableUtils.closeQuietly(notifiedLeader);
        }
        CloseableUtils.closeQuietly(client);
    }
}
 
Example 15
Source File: TestInterProcessSemaphore.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void testReleaseInChunks() throws Exception
{
    final Timing timing = new Timing();
    final int MAX_LEASES = 11;
    final int THREADS = 100;

    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try
    {
        final Stepper latch = new Stepper();
        final Random random = new Random();
        final Counter counter = new Counter();
        ExecutorService service = Executors.newCachedThreadPool();
        ExecutorCompletionService<Object> completionService = new ExecutorCompletionService<Object>(service);
        for ( int i = 0; i < THREADS; ++i )
        {
            completionService.submit
                (
                    new Callable<Object>()
                    {
                        @Override
                        public Object call() throws Exception
                        {
                            InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", MAX_LEASES);
                            Lease lease = semaphore.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS);
                            if ( lease == null )
                            {
                                throw new Exception("timed out");
                            }
                            try
                            {
                                synchronized(counter)
                                {
                                    ++counter.currentCount;
                                    if ( counter.currentCount > counter.maxCount )
                                    {
                                        counter.maxCount = counter.currentCount;
                                    }
                                    counter.notifyAll();
                                }

                                latch.await();
                            }
                            finally
                            {
                                synchronized(counter)
                                {
                                    --counter.currentCount;
                                }
                                semaphore.returnLease(lease);
                            }
                            return null;
                        }
                    }
                );
        }

        int remaining = THREADS;
        while ( remaining > 0 )
        {
            int times = Math.min(random.nextInt(5) + 1, remaining);
            latch.countDown(times);
            remaining -= times;
            Thread.sleep(random.nextInt(100) + 1);
        }

        for ( int i = 0; i < THREADS; ++i )
        {
            completionService.take();
        }

        timing.sleepABit();
        synchronized(counter)
        {
            Assert.assertTrue(counter.currentCount == 0);
            Assert.assertTrue(counter.maxCount > 0);
            Assert.assertTrue(counter.maxCount <= MAX_LEASES);
            System.out.println(counter.maxCount);
        }
    }
    finally
    {
        client.close();
    }
}
 
Example 16
Source File: TestServiceCache.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void testViaProvider() throws Exception
{
    Timing timing = new Timing();

    List<Closeable> closeables = Lists.newArrayList();
    try
    {
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        closeables.add(client);
        client.start();

        ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/discovery").client(client).build();
        closeables.add(discovery);
        discovery.start();

        ServiceProvider<String> serviceProvider = discovery.serviceProviderBuilder().serviceName("test").build();
        closeables.add(serviceProvider);
        serviceProvider.start();

        ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
        discovery.registerService(instance);

        int count = 0;
        ServiceInstance<String> foundInstance = null;
        while ( foundInstance == null )
        {
            Assert.assertTrue(count++ < 5);
            foundInstance = serviceProvider.getInstance();
            timing.sleepABit();
        }
        Assert.assertEquals(foundInstance, instance);

        ServiceInstance<String> instance2 = ServiceInstance.<String>builder().address("foo").payload("thing").name("test").port(10064).build();
        discovery.registerService(instance2);
        timing.sleepABit();
        Collection<ServiceInstance<String>> allInstances = serviceProvider.getAllInstances();
        Assert.assertEquals(allInstances.size(), 2);
    }
    finally
    {
        Collections.reverse(closeables);
        for ( Closeable c : closeables )
        {
            CloseableUtils.closeQuietly(c);
        }
    }
}
 
Example 17
Source File: TestPathChildrenCache.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void testRebuildNode() 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().creatingParentsIfNeeded().forPath("/test/one", "one".getBytes());

        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicInteger counter = new AtomicInteger();
        final Semaphore semaphore = new Semaphore(1);
        cache = new PathChildrenCache(client, "/test", true)
        {
            @Override
            void getDataAndStat(String fullPath) throws Exception
            {
                semaphore.acquire();
                counter.incrementAndGet();
                super.getDataAndStat(fullPath);
                latch.countDown();
            }
        };
        cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);

        Assert.assertTrue(timing.awaitLatch(latch));

        int saveCounter = counter.get();
        client.setData().forPath("/test/one", "alt".getBytes());
        cache.rebuildNode("/test/one");
        Assert.assertEquals(cache.getCurrentData("/test/one").getData(), "alt".getBytes());
        Assert.assertEquals(saveCounter, counter.get());

        semaphore.release(1000);
        timing.sleepABit();
    }
    finally
    {
        CloseableUtils.closeQuietly(cache);
        TestCleanState.closeAndTestClean(client);
    }
}
 
Example 18
Source File: TestLeaderLatch.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void testCallbackDontNotify() throws Exception
{
    final AtomicLong masterCounter = new AtomicLong(0);
    final AtomicLong notLeaderCounter = new AtomicLong(0);

    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));

    final LeaderLatch leader = new LeaderLatch(client, PATH_NAME);
    final LeaderLatch notifiedLeader = new LeaderLatch(client, PATH_NAME, "", LeaderLatch.CloseMode.NOTIFY_LEADER);

    leader.addListener(new LeaderLatchListener()
    {
        @Override
        public void isLeader()
        {
        }

        @Override
        public void notLeader()
        {
            masterCounter.incrementAndGet();
        }
    });

    notifiedLeader.addListener(new LeaderLatchListener()
    {
        @Override
        public void isLeader()
        {
        }

        @Override
        public void notLeader()
        {
            notLeaderCounter.incrementAndGet();
        }
    });

    try
    {
        client.start();

        leader.start();

        timing.sleepABit();

        notifiedLeader.start();

        timing.sleepABit();

        notifiedLeader.close();

        timing.sleepABit();

        // Test the close override
        leader.close(LeaderLatch.CloseMode.NOTIFY_LEADER);

        Assert.assertEquals(leader.getState(), LeaderLatch.State.CLOSED);
        Assert.assertEquals(notifiedLeader.getState(), LeaderLatch.State.CLOSED);

        Assert.assertEquals(masterCounter.get(), 1);
        Assert.assertEquals(notLeaderCounter.get(), 0);
    }
    finally
    {
        if ( leader.getState() != LeaderLatch.State.CLOSED )
        {
            CloseableUtils.closeQuietly(leader);
        }
        if ( notifiedLeader.getState() != LeaderLatch.State.CLOSED )
        {
            CloseableUtils.closeQuietly(notifiedLeader);
        }
        TestCleanState.closeAndTestClean(client);
    }
}
 
Example 19
Source File: TestSharedCount.java    From curator with Apache License 2.0 4 votes vote down vote up
@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();

        final CountDownLatch setLatch = new CountDownLatch(2);
        SharedCountListener listener = new SharedCountListener()
        {
            @Override
            public void countHasChanged(SharedCountReader sharedCount, int newCount) throws Exception
            {
                setLatch.countDown();
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState)
            {
                // nop
            }
        };
        count1.addListener(listener);
        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));
        Assert.assertTrue(timing.awaitLatch(setLatch));
    }
    finally
    {
        CloseableUtils.closeQuietly(count2);
        CloseableUtils.closeQuietly(count1);
        CloseableUtils.closeQuietly(client2);
        CloseableUtils.closeQuietly(client1);
    }
}
 
Example 20
Source File: TestInterProcessSemaphore.java    From curator with Apache License 2.0 4 votes vote down vote up
@Test
public void testInterruptAcquire() throws Exception
{
    // CURATOR-462
    final Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try
    {
        final InterProcessSemaphoreV2 s1 = new InterProcessSemaphoreV2(client, "/test", 1);
        final InterProcessSemaphoreV2 s2 = new InterProcessSemaphoreV2(client, "/test", 1);
        final InterProcessSemaphoreV2 s3 = new InterProcessSemaphoreV2(client, "/test", 1);
        
        final CountDownLatch debugWaitLatch = s2.debugWaitLatch = new CountDownLatch(1);
        
        // Acquire exclusive semaphore
        Lease lease = s1.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS);
        Assert.assertNotNull(lease);
        
        // Queue up another semaphore on the same path
        Future<Object> handle = Executors.newSingleThreadExecutor().submit(new Callable<Object>() {
            
            @Override
            public Object call() throws Exception {
                s2.acquire();
                return null;
            }
        });

        // Wait until second lease is created and the wait is started for it to become active
        Assert.assertTrue(timing.awaitLatch(debugWaitLatch));
        
        // Interrupt the wait
        handle.cancel(true);
        
        // Assert that the second lease is gone
        timing.sleepABit();
        Assert.assertEquals(client.getChildren().forPath("/test/leases").size(), 1);
        
        // Assert that after closing the first (current) semaphore, we can acquire a new one
        s1.returnLease(lease);
        Assert.assertNotNull(s3.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS));
    }
    finally
    {
        TestCleanState.closeAndTestClean(client);
    }
}