Created
June 26, 2012 22:45
-
-
Save akiani/2999837 to your computer and use it in GitHub Desktop.
Test to reproduce the issue with Curator Double Barrier.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>test-curator</groupId> | |
<artifactId>test-curator</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>test-curator</name> | |
<url>http://maven.apache.org</url> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.10</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>com.netflix.curator</groupId> | |
<artifactId>curator-recipes</artifactId> | |
<version>1.1.13</version> | |
</dependency> | |
<dependency> | |
<groupId>com.netflix.curator</groupId> | |
<artifactId>curator-framework</artifactId> | |
<version>1.1.13</version> | |
</dependency> | |
<dependency> | |
<groupId>com.netflix.curator</groupId> | |
<artifactId>curator-test</artifactId> | |
<version>1.1.13</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package foo; | |
import java.io.IOException; | |
import java.util.concurrent.TimeUnit; | |
import org.junit.After; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
import com.google.common.io.Closeables; | |
import com.netflix.curator.framework.CuratorFramework; | |
import com.netflix.curator.framework.CuratorFrameworkFactory; | |
import com.netflix.curator.framework.recipes.barriers.DistributedDoubleBarrier; | |
import com.netflix.curator.retry.RetryUntilElapsed; | |
import com.netflix.curator.test.TestingServer; | |
/** | |
* | |
* @author Amirhossein Kiani (amirhkiani@gmail.com) Created on Jun 26, 2012 | |
*/ | |
public class TestDoubleBarrier { | |
private TestingServer testingServer; | |
private boolean testFailed = false; | |
private final static int MEMBERS = 3; | |
private final static int TRIALS = 100; | |
@Before | |
public void init() throws Exception { | |
testingServer = new TestingServer(12328); | |
} | |
@Test | |
public void testDoubleBarrier() throws Exception { | |
WaitingThread[] threads = new WaitingThread[MEMBERS]; | |
for (int trial = 0; trial < TRIALS; trial++) { | |
for (int i = 0; i < MEMBERS; i++) { | |
threads[i] = new WaitingThread(i); | |
threads[i].start(); | |
} | |
for (int i = 0; i < MEMBERS; i++) { | |
threads[i].join(); | |
Assert.assertFalse(testFailed); | |
} | |
} | |
} | |
class WaitingThread extends Thread { | |
DistributedDoubleBarrier barrier; | |
int id; | |
CuratorFramework framework; | |
public WaitingThread(int id) throws IOException { | |
framework = getCuratorFramework(); | |
this.barrier = new DistributedDoubleBarrier(framework, "/barrier", MEMBERS); | |
this.id = id; | |
} | |
@Override | |
public void run() { | |
try { | |
barrier.enter(10,TimeUnit.SECONDS); | |
System.out.println("Thread " + id + " entered the barrier."); | |
barrier.leave(); | |
System.out.println("Thread " + id + " left the barrier."); | |
} catch (Exception e) { | |
System.err.println("Thread " + id + " failed entering."); | |
testFailed = true; | |
e.printStackTrace(); | |
} | |
Closeables.closeQuietly(framework); | |
} | |
} | |
@After | |
public void destroyZkServer() throws IOException { | |
testingServer.close(); | |
testingServer.stop(); | |
} | |
private CuratorFramework getCuratorFramework() throws IOException { | |
CuratorFramework framework = | |
CuratorFrameworkFactory.newClient(testingServer.getConnectString(), new RetryUntilElapsed( | |
100, 10)); | |
framework.start(); | |
return framework; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment