Skip to content

Instantly share code, notes, and snippets.

@beiske
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save beiske/9002670 to your computer and use it in GitHub Desktop.

Select an option

Save beiske/9002670 to your computer and use it in GitHub Desktop.

Create a standard mvn folder structure by putting each file in a folder like this:

├── pom.xml
└── src
    └── test
        ├── java
        │   └── UnknownHostExceptionTest.java
        └── resources
            └── trace.btm

Run with mvn test

<?xml version="1.0" encoding="UTF-8"?>
<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">
<name>curator-unknownhostexception</name>
<modelVersion>4.0.0</modelVersion>
<groupId>no.found</groupId>
<artifactId>curator-unknownhostexception</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<description>Demo project to test handling of unknownhostexception</description>
<inceptionYear>2014</inceptionYear>
<properties>
<byteman.version>2.1.4.1</byteman.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
<exclusion>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<version>2.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.byteman</groupId>
<artifactId>byteman</artifactId>
<scope>test</scope>
<version>${byteman.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.byteman</groupId>
<artifactId>byteman-submit</artifactId>
<scope>test</scope>
<version>${byteman.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.byteman</groupId>
<artifactId>byteman-install</artifactId>
<scope>test</scope>
<version>${byteman.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.byteman</groupId>
<artifactId>byteman-bmunit</artifactId>
<scope>test</scope>
<version>${byteman.version}</version>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.6</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<!-- <version>2.4.3</version> -->
<configuration>
<useManifestOnlyJar>false</useManifestOnlyJar>
<forkMode>once</forkMode>
<parallel>false</parallel>
</configuration>
</plugin>
</plugins>
</build>
</project>
RULE make zookeeper throw unknownhostexception
CLASS org.apache.zookeeper.client.StaticHostProvider
METHOD <init>
AT ENTRY
IF true
DO traceln("*** Injecting UnknownHostException!");
throw new UnknownHostException()
ENDRULE
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.curator.RetryPolicy;
import org.apache.curator.RetrySleeper;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.test.TestingServer;
import org.jboss.byteman.contrib.bmunit.BMScript;
import org.jboss.byteman.contrib.bmunit.BMUnitRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(BMUnitRunner.class)
@BMScript(value = "trace", dir = "target/test-classes")
public class UnknownHostExceptionTest {
private TestingServer server;
@Before
public void setUp() throws Exception {
server = new TestingServer();
}
@Test
public void test() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(),
new TryEverySecond());
client.start();
client.create().forPath("/foo");
assertNotNull(client.checkExists().forPath("/foo"));
}
@After
public void cleanUp() throws IOException {
server.close();
}
static class TryEverySecond implements RetryPolicy {
@Override
public boolean allowRetry(int retryCount, long elapsedTimeMs, RetrySleeper sleeper) {
try {
sleeper.sleepFor(1000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment