use of java.nio.channels.WritePendingException in project jetty.project by eclipse.
the class HTTP2Test method testInvalidAPIUsageOnClient.
@Test
public void testInvalidAPIUsageOnClient() throws Exception {
start(new ServerSessionListener.Adapter() {
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
Callback.Completable completable = new Callback.Completable();
MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
stream.headers(new HeadersFrame(stream.getId(), response, null, false), completable);
return new Stream.Listener.Adapter() {
@Override
public void onData(Stream stream, DataFrame frame, Callback callback) {
callback.succeeded();
if (frame.isEndStream()) {
completable.thenRun(() -> {
DataFrame endFrame = new DataFrame(stream.getId(), BufferUtil.EMPTY_BUFFER, true);
stream.data(endFrame, Callback.NOOP);
});
}
}
};
}
});
Session session = newClient(new Session.Listener.Adapter());
MetaData.Request metaData = newRequest("GET", new HttpFields());
HeadersFrame frame = new HeadersFrame(metaData, null, false);
Promise.Completable<Stream> completable = new Promise.Completable<>();
CountDownLatch completeLatch = new CountDownLatch(2);
session.newStream(frame, completable, new Stream.Listener.Adapter() {
@Override
public void onData(Stream stream, DataFrame frame, Callback callback) {
callback.succeeded();
if (frame.isEndStream())
completeLatch.countDown();
}
});
Stream stream = completable.get(5, TimeUnit.SECONDS);
long sleep = 1000;
DataFrame data1 = new DataFrame(stream.getId(), ByteBuffer.allocate(1024), false) {
@Override
public ByteBuffer getData() {
sleep(2 * sleep);
return super.getData();
}
};
DataFrame data2 = new DataFrame(stream.getId(), BufferUtil.EMPTY_BUFFER, true);
new Thread(() -> {
// The first data() call is legal, but slow.
stream.data(data1, new Callback() {
@Override
public void succeeded() {
stream.data(data2, NOOP);
}
});
}).start();
// Wait for the first data() call to happen.
sleep(sleep);
// This data call is illegal because it does not
// wait for the previous callback to complete.
stream.data(data2, new Callback() {
@Override
public void failed(Throwable x) {
if (x instanceof WritePendingException) {
// Expected.
completeLatch.countDown();
}
}
});
Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
}
Aggregations