You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2014/03/05 13:08:46 UTC
[3/3] git commit: more tests copied from TestInterProcessMutexBase
more tests copied from TestInterProcessMutexBase
Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/6477a2f0
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/6477a2f0
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/6477a2f0
Branch: refs/heads/CURATOR-88
Commit: 6477a2f08d9e88dd3d934d18051e1193e44a1274
Parents: 0dcba0f
Author: randgalt <ra...@apache.org>
Authored: Tue Mar 4 21:14:46 2014 +0530
Committer: randgalt <ra...@apache.org>
Committed: Tue Mar 4 21:14:46 2014 +0530
----------------------------------------------------------------------
.../apache/curator/x/rest/api/TestLocks.java | 58 ++++++++++++++++++++
1 file changed, 58 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/curator/blob/6477a2f0/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestLocks.java
----------------------------------------------------------------------
diff --git a/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestLocks.java b/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestLocks.java
index 6625388..7aa4585 100644
--- a/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestLocks.java
+++ b/curator-x-rest/src/test/java/org/apache/curator/x/rest/api/TestLocks.java
@@ -19,6 +19,7 @@
package org.apache.curator.x.rest.api;
+import com.google.common.collect.Lists;
import org.apache.curator.framework.recipes.locks.InterProcessLock;
import org.apache.curator.test.KillSession;
import org.apache.curator.test.TestingServer;
@@ -266,4 +267,61 @@ public class TestLocks extends BaseClassForTests
KillSession.kill(getCuratorRestContext().getClient().getZookeeperClient().getZooKeeper(), server.getConnectString());
Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
}
+
+ @Test
+ public void testThreading() throws Exception
+ {
+ final int THREAD_QTY = 10;
+
+ final AtomicBoolean hasLock = new AtomicBoolean(false);
+ final AtomicBoolean isFirst = new AtomicBoolean(true);
+ final Semaphore semaphore = new Semaphore(1);
+
+ List<Future<Object>> threads = Lists.newArrayList();
+ ExecutorService service = Executors.newCachedThreadPool();
+ for ( int i = 0; i < THREAD_QTY; ++i )
+ {
+ final InterProcessLock mutex = new InterProcessLockBridge(restClient, sessionManager, uriMaker);
+ Future<Object> t = service.submit
+ (
+ new Callable<Object>()
+ {
+ @Override
+ public Object call() throws Exception
+ {
+ semaphore.acquire();
+ mutex.acquire();
+ Assert.assertTrue(hasLock.compareAndSet(false, true));
+ try
+ {
+ if ( isFirst.compareAndSet(true, false) )
+ {
+ semaphore.release(THREAD_QTY - 1);
+ while ( semaphore.availablePermits() > 0 )
+ {
+ Thread.sleep(100);
+ }
+ }
+ else
+ {
+ Thread.sleep(100);
+ }
+ }
+ finally
+ {
+ mutex.release();
+ hasLock.set(false);
+ }
+ return null;
+ }
+ }
+ );
+ threads.add(t);
+ }
+
+ for ( Future<Object> t : threads )
+ {
+ t.get();
+ }
+ }
}