Package io.netty.channel.embedded

Examples of io.netty.channel.embedded.EmbeddedChannel


    Assert.assertEquals(0, buf.refCnt());
  }

  @Test
  public void testBufferAvailabilityRegistrationBufferPoolDestroyedSkipBytes() throws Exception {
    final EmbeddedChannel ch = new EmbeddedChannel(
        new OutboundEnvelopeEncoder(),
        new InboundEnvelopeDecoder(this.bufferProviderBroker));

    when(this.bufferProviderBroker.getBufferProvider(anyJobId(), anyChannelId()))
        .thenReturn(this.bufferProvider);
View Full Code Here


    Assert.assertEquals(1, buf.refCnt());
  }

  @Test
  public void testEncodeDecode() throws Exception {
    final EmbeddedChannel ch = new EmbeddedChannel(
        new OutboundEnvelopeEncoder(), new InboundEnvelopeDecoder(this.bufferProviderBroker));

    when(this.bufferProviderBroker.getBufferProvider(anyJobId(), anyChannelId()))
        .thenReturn(this.bufferProvider);

    when(this.bufferProvider.requestBuffer(anyInt())).thenAnswer(new Answer<Object>() {
      @Override
      public Object answer(InvocationOnMock invocation) throws Throwable {
        // fulfill the buffer request
        return allocBuffer((Integer) invocation.getArguments()[0]);
      }
    });

    // --------------------------------------------------------------------

    Envelope[] envelopes = new Envelope[]{
        nextEnvelope(0),
        nextEnvelope(2),
        nextEnvelope(32768),
        nextEnvelope(3782, new TestEvent1(34872527)),
        nextEnvelope(88, new TestEvent1(8749653), new TestEvent1(365345)),
        nextEnvelope(0, new TestEvent2(34563456), new TestEvent1(598432), new TestEvent2(976293845)),
        nextEnvelope(23)
    };

    ByteBuf buf = encode(ch, envelopes);

    // 1. complete ByteBuf as input
    int refCount = buf.retain().refCnt();

    decodeAndVerify(ch, buf, envelopes);
    Assert.assertEquals(refCount - 1, buf.refCnt());

    // 2. random slices
    buf.readerIndex(0);
    ByteBuf[] slices = randomSlices(buf);

    ch.writeInbound((Object[]) slices);

    for (ByteBuf slice : slices) {
      Assert.assertEquals(1, slice.refCnt());
    }
View Full Code Here

  }

  @Test
  public void testEncodeDecodeRandomEnvelopes() throws Exception {
    final InboundEnvelopeDecoder decoder = new InboundEnvelopeDecoder(this.bufferProviderBroker);
    final EmbeddedChannel ch = new EmbeddedChannel(
        new OutboundEnvelopeEncoder(), decoder);

    when(this.bufferProviderBroker.getBufferProvider(anyJobId(), anyChannelId()))
        .thenReturn(this.bufferProvider);

    when(this.bufferProvider.requestBuffer(anyInt())).thenAnswer(new Answer<Object>() {
      @Override
      public Object answer(InvocationOnMock invocation) throws Throwable {
        // fulfill the buffer request with the requested size
        return allocBuffer((Integer) invocation.getArguments()[0]);
      }
    });

    Random randomAnswerSource = new Random(RANDOM_SEED);

    RandomBufferRequestAnswer randomBufferRequestAnswer = new RandomBufferRequestAnswer(randomAnswerSource);

    RandomBufferAvailabilityRegistrationAnswer randomBufferAvailabilityRegistrationAnswer =
        new RandomBufferAvailabilityRegistrationAnswer(randomAnswerSource, randomBufferRequestAnswer);

    when(this.bufferProvider.requestBuffer(anyInt())).thenAnswer(randomBufferRequestAnswer);

    when(this.bufferProvider.registerBufferAvailabilityListener(Matchers.<BufferAvailabilityListener>anyObject()))
        .thenAnswer(randomBufferAvailabilityRegistrationAnswer);

    // --------------------------------------------------------------------

    Envelope[] envelopes = nextRandomEnvelopes(1024);

    ByteBuf buf = encode(ch, envelopes);

    ByteBuf[] slices = randomSlices(buf);

    for (ByteBuf slice : slices) {
      int refCount = slice.refCnt();
      ch.writeInbound(slice);

      // registered BufferAvailabilityListener => call bufferAvailable(buffer)
      while (randomBufferAvailabilityRegistrationAnswer.isRegistered()) {
        randomBufferAvailabilityRegistrationAnswer.unregister();

        Assert.assertFalse(ch.config().isAutoRead());
        Assert.assertEquals(refCount + 1, slice.refCnt());

        // return a buffer of max size => decoder needs to limit buffer size
        decoder.bufferAvailable(allocBuffer(MAX_BUFFER_SIZE));
        ch.runPendingTasks();
      }

      Assert.assertEquals(refCount - 1, slice.refCnt());
      Assert.assertTrue(ch.config().isAutoRead());
    }

    Envelope[] expected = randomBufferAvailabilityRegistrationAnswer.removeSkippedEnvelopes(envelopes);

    decodeAndVerify(ch, expected);
View Full Code Here

     */
    @Test
    public void webSocketHixie76TestHAProxy() throws Exception {
        final SockJsServiceFactory echoFactory = echoService();
        final String sessionUrl = echoFactory.config().prefix() + "/222/" + UUID.randomUUID().toString();
        final EmbeddedChannel ch = wsChannelForService(echoFactory);

        final FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, sessionUrl + "/websocket");
        request.headers().set(HOST, "server.test.com");
        request.headers().set(UPGRADE, WEBSOCKET.toString());
        request.headers().set(CONNECTION, "Upgrade");
        request.headers().set(CONNECTION, "Upgrade");
        request.headers().set(SEC_WEBSOCKET_KEY1, "4 @1  46546xW%0l 1 5");
        request.headers().set(SEC_WEBSOCKET_KEY2, "12998 5 Y3 1  .P00");
        request.headers().set(ORIGIN, "http://example.com");
        ch.writeInbound(request);

        final HttpResponse upgradeResponse = HttpUtil.decode(ch);
        assertThat(upgradeResponse.getStatus(), equalTo(HttpResponseStatus.SWITCHING_PROTOCOLS));

        ch.writeInbound(Unpooled.copiedBuffer("^n:ds[4U", CharsetUtil.US_ASCII));

        final ByteBuf key = (ByteBuf) readOutboundDiscardEmpty(ch);
        assertThat(key.toString(CharsetUtil.US_ASCII), equalTo("8jKS'y:G*Co,Wxa-"));

        ch.readOutbound();
        final ByteBuf openFrame = ch.readOutbound();
        assertThat(openFrame.toString(UTF_8), equalTo("o"));

        final TextWebSocketFrame textWebSocketFrame = new TextWebSocketFrame("\"a\"");
        ch.writeInbound(textWebSocketFrame);

        ch.readOutbound();
        ch.readOutbound();
        final ByteBuf textFrame = ch.readOutbound();
        assertThat(textFrame.toString(UTF_8), equalTo("a[\"a\"]"));
    }
View Full Code Here

    @Test
    public void webSocketHixie76TestBrokenJSON() throws Exception {
        final String serviceName = "/close";
        final String sessionUrl = serviceName + "/222/" + UUID.randomUUID().toString();
        final SockJsConfig config = SockJsConfig.withPrefix(serviceName).build();
        final EmbeddedChannel ch = wsChannelForService(echoService(config));

        final FullHttpRequest request = webSocketUpgradeRequest(sessionUrl + "/websocket", WebSocketVersion.V00);
        ch.writeInbound(request);
        final FullHttpResponse upgradeResponse = HttpUtil.decodeFullHttpResponse(ch);
        assertThat(upgradeResponse.getStatus(), equalTo(HttpResponseStatus.SWITCHING_PROTOCOLS));
        assertThat(upgradeResponse.content().toString(UTF_8), equalTo("8jKS'y:G*Co,Wxa-"));
        assertThat(((ByteBufHolder) ch.readOutbound()).content().toString(UTF_8), equalTo("o"));

        final TextWebSocketFrame webSocketFrame = new TextWebSocketFrame("[\"a\"");
        ch.writeInbound(webSocketFrame);
        assertThat(ch.isActive(), is(false));
        webSocketTestBrokenJSON(V13);
    }
View Full Code Here

     * Equivalent to WebsocketHybi10.test_firefox_602_connection_header in sockjs-protocol-0.3.3.py.
     */
    @Test
    public void webSocketHybi10Firefox602ConnectionHeader() throws Exception {
        final SockJsServiceFactory echoFactory = echoService();
        final EmbeddedChannel ch = ChannelUtil.webSocketChannel(echoFactory.config());
        final FullHttpRequest request = HttpUtil.webSocketUpgradeRequest("/websocket", WebSocketVersion.V08);
        request.headers().set(CONNECTION, "keep-alive, Upgrade");
        ch.writeInbound(request);
        final HttpResponse response = HttpUtil.decode(ch);
        assertThat(response.getStatus(), is(HttpResponseStatus.SWITCHING_PROTOCOLS));
        assertThat(response.headers().get(CONNECTION), equalTo("Upgrade"));
    }
View Full Code Here

     */
    @Test
    public void xhrStreamingTestOptions() throws Exception {
        final SockJsServiceFactory serviceFactory = echoService();
        final String sessionUrl = serviceFactory.config().prefix() + "/222/" + UUID.randomUUID().toString();
        final EmbeddedChannel ch = channelForService(serviceFactory);
        removeLastInboundMessageHandlers(ch);

        final FullHttpRequest request = httpRequest(sessionUrl + Transports.Type.XHR_STREAMING.path(), GET);
        ch.writeInbound(request);

        final HttpResponse response =  ch.readOutbound();
        assertThat(response.getStatus(), equalTo(HttpResponseStatus.OK));
        assertThat(response.headers().get(CONTENT_TYPE), equalTo(Transports.CONTENT_TYPE_JAVASCRIPT));
        SockJsTestUtil.assertCORSHeaders(response, "*");
        SockJsTestUtil.verifyNoCacheHeaders(response);

        final DefaultHttpContent prelude = ch.readOutbound();
        assertThat(prelude.content().readableBytes(), is(PreludeFrame.CONTENT_SIZE + 1));
        final ByteBuf buffer = Unpooled.buffer(PreludeFrame.CONTENT_SIZE + 1);
        prelude.content().readBytes(buffer);
        buffer.release();

        final DefaultHttpContent openResponse = ch.readOutbound();
        assertThat(openResponse.content().toString(UTF_8), equalTo("o\n"));

        final FullHttpResponse validSend = xhrSendRequest(sessionUrl, "[\"x\"]", serviceFactory);
        assertThat(validSend.getStatus(), is(HttpResponseStatus.NO_CONTENT));

        final DefaultHttpContent chunk = ch.readOutbound();
        assertThat(chunk.content().toString(UTF_8), equalTo("a[\"x\"]\n"));
    }
View Full Code Here

    @Test
    public void xhrStreamingTestResponseLimit() throws Exception {
        final SockJsConfig config = SockJsConfig.withPrefix("/echo").maxStreamingBytesSize(4096).build();
        final SockJsServiceFactory echoFactory = echoService(config);
        final String sessionUrl = echoFactory.config().prefix() + "/222/" + UUID.randomUUID().toString();
        final EmbeddedChannel ch = channelForService(echoFactory);
        removeLastInboundMessageHandlers(ch);

        final FullHttpRequest request = httpRequest(sessionUrl + Transports.Type.XHR_STREAMING.path(), GET);
        ch.writeInbound(request);
        final HttpResponse response =  ch.readOutbound();
        assertThat(response.getStatus(), equalTo(HttpResponseStatus.OK));
        assertThat(response.headers().get(CONTENT_TYPE), equalTo(Transports.CONTENT_TYPE_JAVASCRIPT));
        SockJsTestUtil.assertCORSHeaders(response, "*");
        SockJsTestUtil.verifyNoCacheHeaders(response);

        final DefaultHttpContent prelude = ch.readOutbound();
        assertThat(prelude.content().readableBytes(), is(PreludeFrame.CONTENT_SIZE + 1));
        final ByteBuf buf = Unpooled.buffer(PreludeFrame.CONTENT_SIZE + 1);
        prelude.content().readBytes(buf);
        buf.release();

        final DefaultHttpContent openResponse = ch.readOutbound();
        assertThat(openResponse.content().toString(UTF_8), equalTo("o\n"));

        final String msg = generateMessage(128);
        for (int i = 0; i < 31; i++) {
            final FullHttpResponse validSend = xhrSendRequest(sessionUrl, "[\"" + msg + "\"]", echoFactory);
            assertThat(validSend.getStatus(), is(HttpResponseStatus.NO_CONTENT));
            final DefaultHttpContent chunk = ch.readOutbound();
            assertThat(chunk.content().toString(UTF_8), equalTo("a[\"" + msg + "\"]\n"));
        }
        final LastHttpContent lastChunk = ch.readOutbound();
        assertThat(lastChunk.content().readableBytes(), is(0));
        assertThat(ch.isOpen(), is(false));
    }
View Full Code Here

    @Test
    public void eventSourceTestResponseLimit() throws Exception {
        final SockJsConfig config = SockJsConfig.withPrefix("/echo").maxStreamingBytesSize(4096).build();
        final SockJsServiceFactory echoFactory = echoService(config);
        final String sessionUrl = echoFactory.config().prefix() + "/222/" + UUID.randomUUID().toString();
        final EmbeddedChannel ch = channelForService(echoFactory);
        removeLastInboundMessageHandlers(ch);

        final FullHttpRequest request = httpRequest(sessionUrl + Transports.Type.EVENTSOURCE.path(), GET);
        ch.writeInbound(request);
        final HttpResponse response =  ch.readOutbound();
        assertThat(response.getStatus(), equalTo(HttpResponseStatus.OK));
        assertThat(response.headers().get(CONTENT_TYPE), equalTo(EventSourceTransport.CONTENT_TYPE_EVENT_STREAM));

        final DefaultHttpContent newLinePrelude = ch.readOutbound();
        assertThat(newLinePrelude.content().toString(UTF_8), equalTo("\r\n"));

        final DefaultHttpContent data = ch.readOutbound();
        assertThat(data.content().toString(UTF_8), equalTo("data: o\r\n\r\n"));

        final String msg = generateMessage(4096);
        final FullHttpResponse validSend = xhrSendRequest(sessionUrl, "[\"" + msg + "\"]", echoFactory);
        assertThat(validSend.getStatus(), is(HttpResponseStatus.NO_CONTENT));
        final DefaultHttpContent chunk = ch.readOutbound();
        assertThat(chunk.content().toString(UTF_8), equalTo("data: a[\"" + msg + "\"]\r\n\r\n"));

        assertThat(ch.isOpen(), is(false));
    }
View Full Code Here

    @Test
    public void eventSourceTestTransport() throws Exception {
        final SockJsConfig config = SockJsConfig.withPrefix("/echo").maxStreamingBytesSize(4096).build();
        final SockJsServiceFactory echoFactory = echoService(config);
        final String sessionUrl = echoFactory.config().prefix() + "/222/" + UUID.randomUUID().toString();
        final EmbeddedChannel ch = channelForService(echoFactory);
        removeLastInboundMessageHandlers(ch);

        final FullHttpRequest request = httpRequest(sessionUrl + Transports.Type.EVENTSOURCE.path(), GET);
        ch.writeInbound(request);
        final HttpResponse response =  ch.readOutbound();
        assertThat(response.getStatus(), equalTo(HttpResponseStatus.OK));
        assertThat(response.headers().get(CONTENT_TYPE), equalTo(EventSourceTransport.CONTENT_TYPE_EVENT_STREAM));

        final DefaultHttpContent newLinePrelude = ch.readOutbound();
        assertThat(newLinePrelude.content().toString(UTF_8), equalTo("\r\n"));

        final DefaultHttpContent data = ch.readOutbound();
        assertThat(data.content().toString(UTF_8), equalTo("data: o\r\n\r\n"));

        final String msg = "[\"  \\u0000\\n\\r \"]";
        final FullHttpResponse validSend = xhrSendRequest(sessionUrl, msg, echoFactory);
        assertThat(validSend.getStatus(), is(HttpResponseStatus.NO_CONTENT));

        final DefaultHttpContent chunk = ch.readOutbound();
        assertThat(chunk.content().toString(UTF_8), equalTo("data: a[\"  \\u0000\\n\\r \"]\r\n\r\n"));
    }
View Full Code Here

TOP

Related Classes of io.netty.channel.embedded.EmbeddedChannel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.