Search in sources :

Example 16 with Http2Stream

use of io.netty.handler.codec.http2.Http2Stream in project vert.x by eclipse.

the class Http2ClientConnection method createStream.

synchronized HttpClientStream createStream() throws Http2Exception {
    Http2Connection conn = handler.connection();
    Http2Stream stream = conn.local().createStream(conn.local().incrementAndGetNextStreamId(), false);
    boolean writable = handler.encoder().flowController().isWritable(stream);
    Http2ClientStream clientStream = new Http2ClientStream(this, stream, writable);
    streams.put(clientStream.stream.id(), clientStream);
    return clientStream;
}
Also used : Http2Connection(io.netty.handler.codec.http2.Http2Connection) Http2Stream(io.netty.handler.codec.http2.Http2Stream)

Example 17 with Http2Stream

use of io.netty.handler.codec.http2.Http2Stream in project vert.x by eclipse.

the class VertxHttp2ConnectionHandler method _writeFrame.

private void _writeFrame(Http2Stream stream, byte type, short flags, ByteBuf payload) {
    encoder().writeFrame(ctx, type, stream.id(), new Http2Flags(flags), payload, ctx.newPromise());
    ctx.flush();
}
Also used : Http2Flags(io.netty.handler.codec.http2.Http2Flags)

Example 18 with Http2Stream

use of io.netty.handler.codec.http2.Http2Stream in project netty by netty.

the class Http2ConnectionRoundtripTest method headersUsingHigherValuedStreamIdPreventsUsingLowerStreamId.

@Test
public void headersUsingHigherValuedStreamIdPreventsUsingLowerStreamId() throws Exception {
    bootstrapEnv(1, 1, 1, 0);
    final Http2Headers headers = dummyHeaders();
    runInChannel(clientChannel, new Http2Runnable() {

        @Override
        public void run() throws Http2Exception {
            http2Client.encoder().writeHeaders(ctx(), 5, headers, 0, (short) 16, false, 0, false, newPromise());
            http2Client.encoder().frameWriter().writeHeaders(ctx(), 3, headers, 0, (short) 16, false, 0, false, newPromise());
            http2Client.flush(ctx());
        }
    });
    assertTrue(serverSettingsAckLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    assertTrue(requestLatch.await(DEFAULT_AWAIT_TIMEOUT_SECONDS, SECONDS));
    verify(serverListener).onHeadersRead(any(ChannelHandlerContext.class), eq(5), eq(headers), eq(0), eq((short) 16), eq(false), eq(0), eq(false));
    verify(serverListener, never()).onHeadersRead(any(ChannelHandlerContext.class), eq(3), any(Http2Headers.class), anyInt(), anyShort(), anyBoolean(), anyInt(), anyBoolean());
    // Client should receive a RST_STREAM for stream 3, but there is not Http2Stream object so the listener is never
    // notified.
    verify(serverListener, never()).onGoAwayRead(any(ChannelHandlerContext.class), anyInt(), anyLong(), any(ByteBuf.class));
    verify(serverListener, never()).onRstStreamRead(any(ChannelHandlerContext.class), anyInt(), anyLong());
    verify(clientListener, never()).onGoAwayRead(any(ChannelHandlerContext.class), anyInt(), anyLong(), any(ByteBuf.class));
    verify(clientListener, never()).onRstStreamRead(any(ChannelHandlerContext.class), anyInt(), anyLong());
}
Also used : Http2Runnable(io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 19 with Http2Stream

use of io.netty.handler.codec.http2.Http2Stream in project netty by netty.

the class DefaultHttp2ConnectionTest method removeAllStreamsWhileIteratingActiveStreamsAndExceptionOccurs.

@Test
public void removeAllStreamsWhileIteratingActiveStreamsAndExceptionOccurs() throws InterruptedException, Http2Exception {
    final Endpoint<Http2RemoteFlowController> remote = client.remote();
    final Endpoint<Http2LocalFlowController> local = client.local();
    for (int c = 3, s = 2; c < 5000; c += 2, s += 2) {
        local.createStream(c, false);
        remote.createStream(s, false);
    }
    final Promise<Void> promise = group.next().newPromise();
    final CountDownLatch latch = new CountDownLatch(1);
    try {
        client.forEachActiveStream(new Http2StreamVisitor() {

            @Override
            public boolean visit(Http2Stream stream) throws Http2Exception {
                // This close call is basically a noop, because the following statement will throw an exception.
                client.close(promise);
                // Do an invalid operation while iterating.
                remote.createStream(3, false);
                return true;
            }
        });
    } catch (Http2Exception ignored) {
        client.close(promise).addListener(new FutureListener<Void>() {

            @Override
            public void operationComplete(Future<Void> future) throws Exception {
                assertTrue(promise.isDone());
                latch.countDown();
            }
        });
    }
    assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(io.netty.handler.codec.http2.Http2Connection.Endpoint) Future(io.netty.util.concurrent.Future) Test(org.junit.Test)

Example 20 with Http2Stream

use of io.netty.handler.codec.http2.Http2Stream in project netty by netty.

the class DefaultHttp2ConnectionTest method listenerThrowShouldNotPreventOtherListenersFromBeingNotified.

/**
     * We force {@link #clientListener} methods to all throw a {@link RuntimeException} and verify the following:
     * <ol>
     * <li>all listener methods are called for both {@link #clientListener} and {@link #clientListener2}</li>
     * <li>{@link #clientListener2} is notified after {@link #clientListener}</li>
     * <li>{@link #clientListener2} methods are all still called despite {@link #clientListener}'s
     * method throwing a {@link RuntimeException}</li>
     * </ol>
     */
@Test
public void listenerThrowShouldNotPreventOtherListenersFromBeingNotified() throws Http2Exception {
    final boolean[] calledArray = new boolean[128];
    // The following setup will ensure that clientListener throws exceptions, and marks a value in an array
    // such that clientListener2 will verify that is is set or fail the test.
    int methodIndex = 0;
    doAnswer(new ListenerExceptionThrower(calledArray, methodIndex)).when(clientListener).onStreamAdded(any(Http2Stream.class));
    doAnswer(new ListenerVerifyCallAnswer(calledArray, methodIndex++)).when(clientListener2).onStreamAdded(any(Http2Stream.class));
    doAnswer(new ListenerExceptionThrower(calledArray, methodIndex)).when(clientListener).onStreamActive(any(Http2Stream.class));
    doAnswer(new ListenerVerifyCallAnswer(calledArray, methodIndex++)).when(clientListener2).onStreamActive(any(Http2Stream.class));
    doAnswer(new ListenerExceptionThrower(calledArray, methodIndex)).when(clientListener).onStreamHalfClosed(any(Http2Stream.class));
    doAnswer(new ListenerVerifyCallAnswer(calledArray, methodIndex++)).when(clientListener2).onStreamHalfClosed(any(Http2Stream.class));
    doAnswer(new ListenerExceptionThrower(calledArray, methodIndex)).when(clientListener).onStreamClosed(any(Http2Stream.class));
    doAnswer(new ListenerVerifyCallAnswer(calledArray, methodIndex++)).when(clientListener2).onStreamClosed(any(Http2Stream.class));
    doAnswer(new ListenerExceptionThrower(calledArray, methodIndex)).when(clientListener).onStreamRemoved(any(Http2Stream.class));
    doAnswer(new ListenerVerifyCallAnswer(calledArray, methodIndex++)).when(clientListener2).onStreamRemoved(any(Http2Stream.class));
    doAnswer(new ListenerExceptionThrower(calledArray, methodIndex)).when(clientListener).onGoAwaySent(anyInt(), anyLong(), any(ByteBuf.class));
    doAnswer(new ListenerVerifyCallAnswer(calledArray, methodIndex++)).when(clientListener2).onGoAwaySent(anyInt(), anyLong(), any(ByteBuf.class));
    doAnswer(new ListenerExceptionThrower(calledArray, methodIndex)).when(clientListener).onGoAwayReceived(anyInt(), anyLong(), any(ByteBuf.class));
    doAnswer(new ListenerVerifyCallAnswer(calledArray, methodIndex++)).when(clientListener2).onGoAwayReceived(anyInt(), anyLong(), any(ByteBuf.class));
    doAnswer(new ListenerExceptionThrower(calledArray, methodIndex)).when(clientListener).onStreamAdded(any(Http2Stream.class));
    doAnswer(new ListenerVerifyCallAnswer(calledArray, methodIndex++)).when(clientListener2).onStreamAdded(any(Http2Stream.class));
    // Now we add clientListener2 and exercise all listener functionality
    try {
        client.addListener(clientListener2);
        Http2Stream stream = client.local().createStream(3, false);
        verify(clientListener).onStreamAdded(any(Http2Stream.class));
        verify(clientListener2).onStreamAdded(any(Http2Stream.class));
        verify(clientListener).onStreamActive(any(Http2Stream.class));
        verify(clientListener2).onStreamActive(any(Http2Stream.class));
        Http2Stream reservedStream = client.remote().reservePushStream(2, stream);
        verify(clientListener, never()).onStreamActive(streamEq(reservedStream));
        verify(clientListener2, never()).onStreamActive(streamEq(reservedStream));
        reservedStream.open(false);
        verify(clientListener).onStreamActive(streamEq(reservedStream));
        verify(clientListener2).onStreamActive(streamEq(reservedStream));
        stream.closeLocalSide();
        verify(clientListener).onStreamHalfClosed(any(Http2Stream.class));
        verify(clientListener2).onStreamHalfClosed(any(Http2Stream.class));
        stream.close();
        verify(clientListener).onStreamClosed(any(Http2Stream.class));
        verify(clientListener2).onStreamClosed(any(Http2Stream.class));
        verify(clientListener).onStreamRemoved(any(Http2Stream.class));
        verify(clientListener2).onStreamRemoved(any(Http2Stream.class));
        client.goAwaySent(client.connectionStream().id(), Http2Error.INTERNAL_ERROR.code(), Unpooled.EMPTY_BUFFER);
        verify(clientListener).onGoAwaySent(anyInt(), anyLong(), any(ByteBuf.class));
        verify(clientListener2).onGoAwaySent(anyInt(), anyLong(), any(ByteBuf.class));
        client.goAwayReceived(client.connectionStream().id(), Http2Error.INTERNAL_ERROR.code(), Unpooled.EMPTY_BUFFER);
        verify(clientListener).onGoAwayReceived(anyInt(), anyLong(), any(ByteBuf.class));
        verify(clientListener2).onGoAwayReceived(anyInt(), anyLong(), any(ByteBuf.class));
    } finally {
        client.removeListener(clientListener2);
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) Endpoint(io.netty.handler.codec.http2.Http2Connection.Endpoint) Test(org.junit.Test)

Aggregations

Http2Stream (io.netty.handler.codec.http2.Http2Stream)21 Test (org.junit.Test)16 Http2Exception (io.netty.handler.codec.http2.Http2Exception)9 ByteBuf (io.netty.buffer.ByteBuf)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 Http2StreamVisitor (io.netty.handler.codec.http2.Http2StreamVisitor)5 Metadata (io.grpc.Metadata)4 ChannelFuture (io.netty.channel.ChannelFuture)4 Http2LocalFlowController (io.netty.handler.codec.http2.Http2LocalFlowController)4 Endpoint (io.netty.handler.codec.http2.Http2Connection.Endpoint)3 FlowControlled (io.netty.handler.codec.http2.Http2RemoteFlowController.FlowControlled)3 HttpFields (org.eclipse.jetty.http.HttpFields)3 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)3 HTTP2Stream (org.eclipse.jetty.http2.HTTP2Stream)3 Session (org.eclipse.jetty.http2.api.Session)3 Stream (org.eclipse.jetty.http2.api.Stream)3 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)3 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)3 FuturePromise (org.eclipse.jetty.util.FuturePromise)3 Status (io.grpc.Status)2