use of java.util.concurrent.CountDownLatch in project jetty.project by eclipse.
the class WebSocketScopeSessionTest method testMultiSession_Overlapping.
@Test
public void testMultiSession_Overlapping() throws Exception {
final CountDownLatch midLatch = new CountDownLatch(2);
final CountDownLatch end1Latch = new CountDownLatch(1);
Callable<Session> call1 = new Callable<Session>() {
@Override
public Session call() throws Exception {
Session ret = null;
ScopedInstance<WebSocketScopeContext> wsScope1Bean = newInstance(WebSocketScopeContext.class);
WebSocketScopeContext wsScope1 = wsScope1Bean.instance;
wsScope1.create();
try {
// Scope 1
wsScope1.begin();
BogusSession sess = new BogusSession("1");
wsScope1.setSession(sess);
midLatch.countDown();
midLatch.await(1, TimeUnit.SECONDS);
ScopedInstance<BogusSocket> sock1Bean = newInstance(BogusSocket.class);
BogusSocket sock1 = sock1Bean.instance;
assertThat("Socket 1 Session", sock1.getSession(), sameInstance((Session) sess));
ret = sock1.getSession();
sock1Bean.destroy();
} finally {
wsScope1.end();
}
wsScope1.destroy();
wsScope1Bean.destroy();
end1Latch.countDown();
return ret;
}
};
final CountDownLatch end2Latch = new CountDownLatch(1);
Callable<Session> call2 = new Callable<Session>() {
@Override
public Session call() throws Exception {
Session ret = null;
ScopedInstance<WebSocketScopeContext> wsScope2Bean = newInstance(WebSocketScopeContext.class);
WebSocketScopeContext wsScope2 = wsScope2Bean.instance;
wsScope2.create();
try {
// Scope 2
wsScope2.begin();
BogusSession sess = new BogusSession("2");
wsScope2.setSession(sess);
ScopedInstance<BogusSocket> sock2Bean = newInstance(BogusSocket.class);
midLatch.countDown();
midLatch.await(1, TimeUnit.SECONDS);
BogusSocket sock2 = sock2Bean.instance;
ret = sock2.getSession();
assertThat("Socket 2 Session", sock2.getSession(), sameInstance((Session) sess));
sock2Bean.destroy();
} finally {
wsScope2.end();
}
wsScope2.destroy();
wsScope2Bean.destroy();
end2Latch.countDown();
return ret;
}
};
ExecutorService svc = Executors.newFixedThreadPool(4);
Future<Session> fut1 = svc.submit(call1);
Future<Session> fut2 = svc.submit(call2);
Session sess1 = fut1.get(1, TimeUnit.SECONDS);
Session sess2 = fut2.get(1, TimeUnit.SECONDS);
assertThat("Sessions are different", sess1, not(sameInstance(sess2)));
}
use of java.util.concurrent.CountDownLatch in project jetty.project by eclipse.
the class HttpRedirector method redirect.
/**
* Redirects the given {@code response}, blocking until the redirect is complete.
*
* @param request the original request that triggered the redirect
* @param response the response to the original request
* @return a {@link Result} object containing the request to the redirected location and its response
* @throws InterruptedException if the thread is interrupted while waiting for the redirect to complete
* @throws ExecutionException if the redirect failed
* @see #redirect(Request, Response, Response.CompleteListener)
*/
public Result redirect(Request request, Response response) throws InterruptedException, ExecutionException {
final AtomicReference<Result> resultRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
Request redirect = redirect(request, response, new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
resultRef.set(new Result(result.getRequest(), result.getRequestFailure(), new HttpContentResponse(result.getResponse(), getContent(), getMediaType(), getEncoding()), result.getResponseFailure()));
latch.countDown();
}
});
try {
latch.await();
Result result = resultRef.get();
if (result.isFailed())
throw new ExecutionException(result.getFailure());
return result;
} catch (InterruptedException x) {
// If the application interrupts, we need to abort the redirect
redirect.abort(x);
throw x;
}
}
use of java.util.concurrent.CountDownLatch in project jetty.project by eclipse.
the class HTTP2ServerTest method testRequestResponseNoContent.
@Test
public void testRequestResponseNoContent() throws Exception {
final CountDownLatch latch = new CountDownLatch(3);
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
latch.countDown();
}
});
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, new PrefaceFrame());
generator.control(lease, new SettingsFrame(new HashMap<>(), false));
MetaData.Request metaData = newRequest("GET", new HttpFields());
generator.control(lease, new HeadersFrame(1, metaData, null, true));
try (Socket client = new Socket("localhost", connector.getLocalPort())) {
OutputStream output = client.getOutputStream();
for (ByteBuffer buffer : lease.getByteBuffers()) {
output.write(BufferUtil.toArray(buffer));
}
final AtomicReference<HeadersFrame> frameRef = new AtomicReference<>();
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {
@Override
public void onSettings(SettingsFrame frame) {
latch.countDown();
}
@Override
public void onHeaders(HeadersFrame frame) {
frameRef.set(frame);
latch.countDown();
}
}, 4096, 8192);
parseResponse(client, parser);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
HeadersFrame response = frameRef.get();
Assert.assertNotNull(response);
MetaData.Response responseMetaData = (MetaData.Response) response.getMetaData();
Assert.assertEquals(200, responseMetaData.getStatus());
}
}
use of java.util.concurrent.CountDownLatch in project jetty.project by eclipse.
the class HTTP2ServerTest method testRequestWithContinuationFrames.
private void testRequestWithContinuationFrames(PriorityFrame priorityFrame, Callable<ByteBufferPool.Lease> frames) throws Exception {
final CountDownLatch serverLatch = new CountDownLatch(1);
startServer(new ServerSessionListener.Adapter() {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
if (priorityFrame != null) {
PriorityFrame priority = frame.getPriority();
Assert.assertNotNull(priority);
Assert.assertEquals(priorityFrame.getStreamId(), priority.getStreamId());
Assert.assertEquals(priorityFrame.getParentStreamId(), priority.getParentStreamId());
Assert.assertEquals(priorityFrame.getWeight(), priority.getWeight());
Assert.assertEquals(priorityFrame.isExclusive(), priority.isExclusive());
}
serverLatch.countDown();
MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
HeadersFrame responseFrame = new HeadersFrame(stream.getId(), metaData, null, true);
stream.headers(responseFrame, Callback.NOOP);
return null;
}
});
generator = new Generator(byteBufferPool, 4096, 4);
ByteBufferPool.Lease lease = frames.call();
try (Socket client = new Socket("localhost", connector.getLocalPort())) {
OutputStream output = client.getOutputStream();
for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
output.flush();
Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
final CountDownLatch clientLatch = new CountDownLatch(1);
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {
@Override
public void onHeaders(HeadersFrame frame) {
if (frame.isEndStream())
clientLatch.countDown();
}
}, 4096, 8192);
boolean closed = parseResponse(client, parser);
Assert.assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
Assert.assertFalse(closed);
}
}
use of java.util.concurrent.CountDownLatch in project jetty.project by eclipse.
the class HTTP2ServerTest method testBadPingWrongPayload.
@Test
public void testBadPingWrongPayload() throws Exception {
startServer(new HttpServlet() {
});
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, new PrefaceFrame());
generator.control(lease, new SettingsFrame(new HashMap<>(), false));
generator.control(lease, new PingFrame(new byte[8], false));
// Modify the length of the frame to a wrong one.
lease.getByteBuffers().get(2).putShort(0, (short) 7);
final CountDownLatch latch = new CountDownLatch(1);
try (Socket client = new Socket("localhost", connector.getLocalPort())) {
OutputStream output = client.getOutputStream();
for (ByteBuffer buffer : lease.getByteBuffers()) {
output.write(BufferUtil.toArray(buffer));
}
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {
@Override
public void onGoAway(GoAwayFrame frame) {
Assert.assertEquals(ErrorCode.FRAME_SIZE_ERROR.code, frame.getError());
latch.countDown();
}
}, 4096, 8192);
parseResponse(client, parser);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}
Aggregations