use of org.apache.zookeeper.server.jersey.jaxb.ZSession in project zookeeper by apache.
the class SessionTest method testDeleteSession.
@Test
public void testDeleteSession() {
ZSession session = createSession("30");
WebResource wr = sessionsr.path(session.id);
Builder b = wr.accept(MediaType.APPLICATION_JSON);
Assert.assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id));
ClientResponse cr = b.delete(ClientResponse.class, null);
Assert.assertEquals(ClientResponse.Status.NO_CONTENT, cr.getClientResponseStatus());
Assert.assertFalse(ZooKeeperService.isConnected(CONTEXT_PATH, session.id));
}
use of org.apache.zookeeper.server.jersey.jaxb.ZSession in project zookeeper by apache.
the class SessionTest method testCreateNewSession.
@Test
public void testCreateNewSession() throws JSONException {
ZSession session = createSession();
Assert.assertEquals(session.id.length(), 36);
// use out-of-band method to verify
Assert.assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id));
}
use of org.apache.zookeeper.server.jersey.jaxb.ZSession in project zookeeper by apache.
the class SessionTest method testSendHeartbeat.
@Test
public void testSendHeartbeat() throws InterruptedException {
ZSession session = createSession("2");
Thread.sleep(1000);
WebResource wr = sessionsr.path(session.id);
Builder b = wr.accept(MediaType.APPLICATION_JSON);
ClientResponse cr = b.put(ClientResponse.class, null);
Assert.assertEquals(ClientResponse.Status.OK, cr.getClientResponseStatus());
Thread.sleep(1500);
Assert.assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id));
Thread.sleep(1000);
Assert.assertFalse(ZooKeeperService.isConnected(CONTEXT_PATH, session.id));
}
use of org.apache.zookeeper.server.jersey.jaxb.ZSession in project zookeeper by apache.
the class SessionTest method testSessionExpires.
@Test
public void testSessionExpires() throws InterruptedException {
ZSession session = createSession("1");
// use out-of-band method to verify
Assert.assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id));
// wait for the session to be closed
Thread.sleep(1500);
Assert.assertFalse(ZooKeeperService.isConnected(CONTEXT_PATH, session.id));
}
use of org.apache.zookeeper.server.jersey.jaxb.ZSession in project zookeeper by apache.
the class SessionsResource method createSession.
@POST
@Produces({ MediaType.APPLICATION_JSON, "application/javascript", MediaType.APPLICATION_XML })
public Response createSession(@QueryParam("op") String op, @DefaultValue("5") @QueryParam("expire") String expire, @Context UriInfo ui) {
if (!op.equals("create")) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(new ZError(ui.getRequestUri().toString(), "")).build());
}
int expireInSeconds;
try {
expireInSeconds = Integer.parseInt(expire);
} catch (NumberFormatException e) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).build());
}
String uuid = UUID.randomUUID().toString();
while (ZooKeeperService.isConnected(contextPath, uuid)) {
uuid = UUID.randomUUID().toString();
}
// establish the connection to the ZooKeeper cluster
try {
ZooKeeperService.getClient(contextPath, uuid, expireInSeconds);
} catch (IOException e) {
LOG.error("Failed while trying to create a new session", e);
throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).build());
}
URI uri = ui.getAbsolutePathBuilder().path(uuid).build();
return Response.created(uri).entity(new JSONWithPadding(new ZSession(uuid, uri.toString()))).build();
}
Aggregations