Skip to content

Commit

Permalink
Add a test for the UnixConnector in the simple mode
Browse files Browse the repository at this point in the history
  • Loading branch information
arteam committed Jan 18, 2017
1 parent d44a272 commit b30c295
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
6 changes: 6 additions & 0 deletions dropwizard-unixsocket/pom.xml
Expand Up @@ -34,5 +34,11 @@
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-unixsocket</artifactId>
</dependency>

<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-testing</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Expand Up @@ -34,7 +34,7 @@
* <p/>
*/
@JsonTypeName("unixsocket")
public class UnixSocketFactory extends HttpConnectorFactory {
public class UnixSocketConnectorFactory extends HttpConnectorFactory {

private String socketFilename = "/tmp/dropwizard.sock";

Expand Down Expand Up @@ -65,6 +65,6 @@ protected Connector buildConnector(Server server, Scheduler scheduler, ByteBuffe

@Override
protected String httpConnections() {
return name(UnixSocketFactory.class, socketFilename, "connections");
return name(UnixSocketConnectorFactory.class, socketFilename, "connections");
}
}
@@ -1 +1 @@
io.dropwizard.unixsocket.UnixSocketFactory
io.dropwizard.unixsocket.UnixSocketConnectorFactory
@@ -0,0 +1,72 @@
package io.dropwizard.unixsocket;

import com.codahale.metrics.health.HealthCheck;
import io.dropwizard.Application;
import io.dropwizard.Configuration;
import io.dropwizard.setup.Environment;
import io.dropwizard.testing.ResourceHelpers;
import io.dropwizard.testing.junit.DropwizardAppRule;
import jnr.unixsocket.UnixSocketAddress;
import jnr.unixsocket.UnixSocketChannel;
import org.junit.Rule;
import org.junit.Test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.nio.channels.Channels;

public class UnixSocketConnectorFactoryTest {

@Rule
public DropwizardAppRule<Configuration> rule = new DropwizardAppRule<>(HelloWorldApplication.class,
ResourceHelpers.resourceFilePath("yaml/usock-server.yml"));

private File socket = new File("/tmp/dropwizard.sock");

@Test
public void testClient() throws Exception {
String httpRequest = "GET /app/hello HTTP/1.1\r\n" +
"Host: dropwizard-unixsock\r\n" +
"\r\n";
try (UnixSocketChannel channel = UnixSocketChannel.open(new UnixSocketAddress(socket));
PrintWriter writer = new PrintWriter(Channels.newOutputStream(channel));
Reader reader = new InputStreamReader(Channels.newInputStream(channel))) {
writer.print(httpRequest);
writer.flush();

char[] buf = new char[4096];
int read = reader.read(buf);
System.out.println("Read from the server: " + new String(buf, 0, read));
}
}

public static class HelloWorldApplication extends Application<Configuration> {

@Override
public void run(Configuration configuration, Environment environment) throws Exception {
environment.jersey().register(new FakeResource());
environment.healthChecks().register("hello-check", new HealthCheck() {
@Override
protected Result check() throws Exception {
return Result.healthy();
}
});
}

@Path("/hello")
@Produces(MediaType.APPLICATION_JSON)
public static class FakeResource {

@GET
public String get() throws Exception {
return "{\"hello\": \"World\"}";
}
}
}
}
@@ -0,0 +1,6 @@
server:
type: simple
connector:
type: unixsocket
applicationContextPath: /app
adminContextPath: /admin

0 comments on commit b30c295

Please sign in to comment.