use of org.eclipse.jetty.http2.frames.PriorityFrame in project jetty.project by eclipse.
the class PriorityTest method testPriorityBeforeHeaders.
@Test
public void testPriorityBeforeHeaders() throws Exception {
start(new ServerSessionListener.Adapter() {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
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;
}
});
Session session = newClient(new Session.Listener.Adapter());
int streamId = session.priority(new PriorityFrame(0, 13, false), Callback.NOOP);
Assert.assertTrue(streamId > 0);
CountDownLatch latch = new CountDownLatch(2);
MetaData metaData = newRequest("GET", new HttpFields());
HeadersFrame headersFrame = new HeadersFrame(streamId, metaData, null, true);
session.newStream(headersFrame, new Promise.Adapter<Stream>() {
@Override
public void succeeded(Stream result) {
Assert.assertEquals(streamId, result.getId());
latch.countDown();
}
}, new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
if (frame.isEndStream())
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.http2.frames.PriorityFrame 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 org.eclipse.jetty.http2.frames.PriorityFrame in project jetty.project by eclipse.
the class HTTP2ServerTest method testRequestWithPriorityWithContinuationFrames.
@Test
public void testRequestWithPriorityWithContinuationFrames() throws Exception {
PriorityFrame priority = new PriorityFrame(1, 13, 200, true);
testRequestWithContinuationFrames(priority, () -> {
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, priority, true));
return lease;
});
}
use of org.eclipse.jetty.http2.frames.PriorityFrame 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 org.eclipse.jetty.http2.frames.PriorityFrame in project jetty.project by eclipse.
the class HTTP2Session method newStream.
@Override
public void newStream(HeadersFrame frame, Promise<Stream> promise, Stream.Listener listener) {
// Synchronization is necessary to atomically create
// the stream id and enqueue the frame to be sent.
boolean queued;
synchronized (this) {
int streamId = frame.getStreamId();
if (streamId <= 0) {
streamId = streamIds.getAndAdd(2);
PriorityFrame priority = frame.getPriority();
priority = priority == null ? null : new PriorityFrame(streamId, priority.getParentStreamId(), priority.getWeight(), priority.isExclusive());
frame = new HeadersFrame(streamId, frame.getMetaData(), priority, frame.isEndStream());
}
final IStream stream = createLocalStream(streamId, promise);
if (stream == null)
return;
stream.setListener(listener);
ControlEntry entry = new ControlEntry(frame, stream, new PromiseCallback<>(promise, stream));
queued = flusher.append(entry);
}
// Iterate outside the synchronized block.
if (queued)
flusher.iterate();
}
Aggregations