use of org.eclipse.jetty.http2.frames.DataFrame in project jetty.project by eclipse.
the class IdleTimeoutTest method testStreamIdleTimeoutIsNotEnforcedWhenReceiving.
@Test
public void testStreamIdleTimeoutIsNotEnforcedWhenReceiving() 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;
}
};
}
});
Session session = newClient(new Session.Listener.Adapter());
MetaData.Request metaData = newRequest("GET", new HttpFields());
HeadersFrame requestFrame = new HeadersFrame(metaData, null, false);
FuturePromise<Stream> promise = new FuturePromise<>();
session.newStream(requestFrame, promise, new Stream.Listener.Adapter());
final Stream stream = promise.get(5, TimeUnit.SECONDS);
sleep(idleTimeout / 2);
final CountDownLatch dataLatch = new CountDownLatch(1);
stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), false), new Callback() {
private int sends;
@Override
public void succeeded() {
sleep(idleTimeout / 2);
final boolean last = ++sends == 2;
stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), last), !last ? this : new Callback() {
@Override
public InvocationType getInvocationType() {
return InvocationType.NON_BLOCKING;
}
@Override
public void succeeded() {
// Idle timeout should not fire while receiving.
Assert.assertEquals(1, timeoutLatch.getCount());
dataLatch.countDown();
}
});
}
});
Assert.assertTrue(dataLatch.await(5 * idleTimeout, TimeUnit.MILLISECONDS));
// The server did not send a response, so it will eventually timeout.
Assert.assertTrue(timeoutLatch.await(5 * idleTimeout, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.http2.frames.DataFrame in project jetty.project by eclipse.
the class IdleTimeoutTest method testStreamIdleTimeoutIsNotEnforcedWhenSending.
@Test
public void testStreamIdleTimeoutIsNotEnforcedWhenSending() throws Exception {
final CountDownLatch resetLatch = new CountDownLatch(1);
start(new ServerSessionListener.Adapter() {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
return null;
}
@Override
public void onReset(Session session, ResetFrame frame) {
resetLatch.countDown();
}
});
Session session = newClient(new Session.Listener.Adapter());
MetaData.Request metaData = newRequest("GET", new HttpFields());
HeadersFrame requestFrame = new HeadersFrame(metaData, null, false);
FuturePromise<Stream> promise = new FuturePromise<Stream>() {
@Override
public void succeeded(Stream stream) {
stream.setIdleTimeout(idleTimeout);
super.succeeded(stream);
}
};
session.newStream(requestFrame, promise, new Stream.Listener.Adapter());
final Stream stream = promise.get(5, TimeUnit.SECONDS);
Callback.Completable completable1 = new Callback.Completable();
sleep(idleTimeout / 2);
stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), false), completable1);
completable1.thenCompose(nil -> {
Callback.Completable completable2 = new Callback.Completable();
sleep(idleTimeout / 2);
stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), false), completable2);
return completable2;
}).thenRun(() -> {
sleep(idleTimeout / 2);
stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), true), Callback.NOOP);
});
Assert.assertFalse(resetLatch.await(1, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.http2.frames.DataFrame in project jetty.project by eclipse.
the class DataBodyParser method onData.
private void onData(ByteBuffer buffer, boolean fragment, int padding) {
DataFrame frame = new DataFrame(getStreamId(), buffer, !fragment && isEndStream(), padding);
notifyData(frame);
}
use of org.eclipse.jetty.http2.frames.DataFrame 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));
}
use of org.eclipse.jetty.http2.frames.DataFrame in project jetty.project by eclipse.
the class FlowControlStalledTest method testStreamStalledIsInvokedOnlyOnce.
@Test
public void testStreamStalledIsInvokedOnlyOnce() throws Exception {
AtomicReference<CountDownLatch> stallLatch = new AtomicReference<>(new CountDownLatch(1));
CountDownLatch unstallLatch = new CountDownLatch(1);
start(() -> new BufferingFlowControlStrategy(0.5f) {
@Override
public void onStreamStalled(IStream stream) {
super.onStreamStalled(stream);
stallLatch.get().countDown();
}
@Override
protected void onStreamUnstalled(IStream stream) {
super.onStreamUnstalled(stream);
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 stream 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 session window so that only the stream gets stalled.
client.setInitialSessionRecvWindow(5 * FlowControlStrategy.DEFAULT_WINDOW_SIZE);
Session client = newClient(new Session.Listener.Adapter());
CountDownLatch latch = new CountDownLatch(1);
Queue<Callback> callbacks = new ArrayDeque<>();
MetaData.Request request = newRequest("GET", "/stall", new HttpFields());
client.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));
// First stream is now stalled, check that writing a second stream
// does not result in the first be notified again of being stalled.
stallLatch.set(new CountDownLatch(1));
request = newRequest("GET", "/", new HttpFields());
client.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