use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.
the class IdleTimeoutTest method testClientEnforcingIdleTimeout.
@Test
public void testClientEnforcingIdleTimeout() throws Exception {
final CountDownLatch closeLatch = new CountDownLatch(1);
start(new ServerSessionListener.Adapter() {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
stream.setIdleTimeout(10 * idleTimeout);
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;
}
@Override
public void onClose(Session session, GoAwayFrame frame) {
closeLatch.countDown();
}
});
client.setIdleTimeout(idleTimeout);
Session session = newClient(new Session.Listener.Adapter());
MetaData.Request metaData = newRequest("GET", new HttpFields());
HeadersFrame requestFrame = new HeadersFrame(metaData, null, true);
session.newStream(requestFrame, new Promise.Adapter<Stream>() {
@Override
public void succeeded(Stream stream) {
stream.setIdleTimeout(10 * idleTimeout);
}
}, new Stream.Listener.Adapter());
Assert.assertTrue(closeLatch.await(5 * idleTimeout, TimeUnit.MILLISECONDS));
Assert.assertTrue(session.isClosed());
}
use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.
the class PingTest method testPing.
@Test
public void testPing() throws Exception {
start(new ServerSessionListener.Adapter());
final byte[] payload = new byte[8];
new Random().nextBytes(payload);
final CountDownLatch latch = new CountDownLatch(1);
Session session = newClient(new Session.Listener.Adapter() {
@Override
public void onPing(Session session, PingFrame frame) {
Assert.assertTrue(frame.isReply());
Assert.assertArrayEquals(payload, frame.getPayload());
latch.countDown();
}
});
PingFrame frame = new PingFrame(payload, false);
session.ping(frame, Callback.NOOP);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.
the class PriorityTest method testPriorityAfterHeaders.
@Test
public void testPriorityAfterHeaders() throws Exception {
CountDownLatch beforeRequests = new CountDownLatch(1);
CountDownLatch afterRequests = new CountDownLatch(2);
start(new ServerSessionListener.Adapter() {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
try {
beforeRequests.await(5, TimeUnit.SECONDS);
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);
afterRequests.countDown();
return null;
} catch (InterruptedException x) {
x.printStackTrace();
return null;
}
}
});
CountDownLatch responses = new CountDownLatch(2);
Stream.Listener.Adapter listener = new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
if (frame.isEndStream())
responses.countDown();
}
};
Session session = newClient(new Session.Listener.Adapter());
MetaData metaData1 = newRequest("GET", "/one", new HttpFields());
HeadersFrame headersFrame1 = new HeadersFrame(metaData1, null, true);
FuturePromise<Stream> promise1 = new FuturePromise<>();
session.newStream(headersFrame1, promise1, listener);
Stream stream1 = promise1.get(5, TimeUnit.SECONDS);
MetaData metaData2 = newRequest("GET", "/two", new HttpFields());
HeadersFrame headersFrame2 = new HeadersFrame(metaData2, null, true);
FuturePromise<Stream> promise2 = new FuturePromise<>();
session.newStream(headersFrame2, promise2, listener);
Stream stream2 = promise2.get(5, TimeUnit.SECONDS);
int streamId = session.priority(new PriorityFrame(stream1.getId(), stream2.getId(), 13, false), Callback.NOOP);
Assert.assertEquals(stream1.getId(), streamId);
// Give time to the PRIORITY frame to arrive to server.
Thread.sleep(1000);
beforeRequests.countDown();
Assert.assertTrue(afterRequests.await(5, TimeUnit.SECONDS));
Assert.assertTrue(responses.await(5, TimeUnit.SECONDS));
}
use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.
the class IdleTimeoutTest method testServerEnforcingStreamIdleTimeout.
@Test
public void testServerEnforcingStreamIdleTimeout() throws Exception {
final CountDownLatch timeoutLatch = new CountDownLatch(1);
start(new ServerSessionListener.Adapter() {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
stream.setIdleTimeout(idleTimeout);
return new Stream.Listener.Adapter() {
@Override
public boolean onIdleTimeout(Stream stream, Throwable x) {
timeoutLatch.countDown();
return true;
}
};
}
});
final CountDownLatch resetLatch = new CountDownLatch(1);
Session session = newClient(new Session.Listener.Adapter());
MetaData.Request metaData = newRequest("GET", new HttpFields());
// Stream does not end here, but we won't send any DATA frame.
HeadersFrame requestFrame = new HeadersFrame(metaData, null, false);
session.newStream(requestFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {
@Override
public void onReset(Stream stream, ResetFrame frame) {
resetLatch.countDown();
}
});
Assert.assertTrue(timeoutLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
// Stream must be gone.
Assert.assertTrue(session.getStreams().isEmpty());
// Session must not be closed, nor disconnected.
Assert.assertFalse(session.isClosed());
Assert.assertFalse(((HTTP2Session) session).isDisconnected());
}
use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.
the class FlowControlStalledTest method testSessionStalledIsInvokedOnlyOnce.
@Test
public void testSessionStalledIsInvokedOnlyOnce() throws Exception {
AtomicReference<CountDownLatch> stallLatch = new AtomicReference<>(new CountDownLatch(1));
CountDownLatch unstallLatch = new CountDownLatch(1);
start(() -> new BufferingFlowControlStrategy(0.5f) {
@Override
public void onSessionStalled(ISession session) {
super.onSessionStalled(session);
stallLatch.get().countDown();
}
@Override
protected void onSessionUnstalled(ISession session) {
super.onSessionUnstalled(session);
unstallLatch.countDown();
}
}, new ServerSessionListener.Adapter() {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
MetaData.Request request = (MetaData.Request) frame.getMetaData();
MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
if (request.getURIString().endsWith("/stall")) {
stream.headers(new HeadersFrame(stream.getId(), response, null, false), new Callback() {
@Override
public void succeeded() {
// Send a large chunk of data so the session gets stalled.
ByteBuffer data = ByteBuffer.allocate(FlowControlStrategy.DEFAULT_WINDOW_SIZE + 1);
stream.data(new DataFrame(stream.getId(), data, true), NOOP);
}
});
} else {
stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
}
return null;
}
});
// Use a large stream window so that only the session gets stalled.
client.setInitialStreamRecvWindow(5 * FlowControlStrategy.DEFAULT_WINDOW_SIZE);
Session session = newClient(new Session.Listener.Adapter() {
@Override
public Map<Integer, Integer> onPreface(Session session) {
Map<Integer, Integer> settings = new HashMap<>();
settings.put(SettingsFrame.INITIAL_WINDOW_SIZE, client.getInitialStreamRecvWindow());
return settings;
}
});
CountDownLatch latch = new CountDownLatch(1);
Queue<Callback> callbacks = new ArrayDeque<>();
MetaData.Request request = newRequest("GET", "/stall", new HttpFields());
session.newStream(new HeadersFrame(request, null, true), new Promise.Adapter<>(), new Stream.Listener.Adapter() {
@Override
public void onData(Stream stream, DataFrame frame, Callback callback) {
callbacks.offer(callback);
if (frame.isEndStream())
latch.countDown();
}
});
Assert.assertTrue(stallLatch.get().await(5, TimeUnit.SECONDS));
// The session is now stalled, check that writing a second stream
// does not result in the session be notified again of being stalled.
stallLatch.set(new CountDownLatch(1));
request = newRequest("GET", "/", new HttpFields());
session.newStream(new HeadersFrame(request, null, true), new Promise.Adapter<>(), new Stream.Listener.Adapter());
Assert.assertFalse(stallLatch.get().await(1, TimeUnit.SECONDS));
// Consume all data.
while (!latch.await(10, TimeUnit.MILLISECONDS)) {
Callback callback = callbacks.poll();
if (callback != null)
callback.succeeded();
}
// Make sure the unstall callback is invoked.
Assert.assertTrue(unstallLatch.await(5, TimeUnit.SECONDS));
}
Aggregations