Package io.netty.channel.embedded

Examples of io.netty.channel.embedded.EmbeddedChannel


    private static FullHttpResponse writeFrame(final Frame frame, final boolean withCallback) {
        final String queryUrl = withCallback ? "/jsonp?c=callback" : "/jsonp";
        final DefaultFullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, queryUrl);
        final SockJsConfig config = SockJsConfig.withPrefix(queryUrl).cookiesNeeded().build();
        final JsonpPollingTransport jsonpPollingOutbound = new JsonpPollingTransport(config, request);
        final EmbeddedChannel ch = new EmbeddedChannel(jsonpPollingOutbound);
        ch.writeInbound(request);
        ch.writeOutbound(frame);
        final FullHttpResponse response =  ch.readOutbound();
        ch.finish();
        return response;
    }
View Full Code Here


    }

    private static FullHttpResponse processHttpRequest(final FullHttpRequest request) {
        final XhrSendTransport transport = new XhrSendTransport(SockJsConfig.withPrefix("/test")
                .cookiesNeeded().build());
        final EmbeddedChannel channel = new EmbeddedChannel(transport);
        channel.writeInbound(request);
        final FullHttpResponse response = channel.readOutbound();
        channel.finish();
        return response;
    }
View Full Code Here

            throw new Error();
        }

        return new Result(
                targetContentEncoding,
                new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(
                        wrapper, compressionLevel, windowBits, memLevel)));
    }
View Full Code Here

    protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception {
        if (decoder == null) {
            if (!(msg instanceof TextWebSocketFrame) && !(msg instanceof BinaryWebSocketFrame)) {
                throw new CodecException("unexpected initial frame type: " + msg.getClass().getName());
            }
            decoder = new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE));
        }

        decoder.writeInbound(msg.content().retain());
        if (appendFrameTail(msg)) {
            decoder.writeInbound(Unpooled.wrappedBuffer(FRAME_TAIL));
View Full Code Here

    @Override
    protected void encode(ChannelHandlerContext ctx, WebSocketFrame msg,
            List<Object> out) throws Exception {
        if (encoder == null) {
            encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(
                    ZlibWrapper.NONE, compressionLevel, windowSize, 8));
        }

        encoder.writeOutbound(msg.content().retain());
View Full Code Here

            throw new Error();
        }

        return new Result(
                targetContentEncoding,
                new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(
                        wrapper, compressionLevel, windowBits, memLevel)));
    }
View Full Code Here

    final MemorySegment segment = new MemorySegment(new byte[MAX_BUFFER_SIZE]);

    final Buffer buffer = mock(Buffer.class);
    when(buffer.getMemorySegment()).thenReturn(segment);

    final EmbeddedChannel channel = new EmbeddedChannel(new OutboundEnvelopeEncoder());

    int numBuffers = 0;
    for (int i = 0; i < NUM_RANDOM_ENVELOPES; i++) {
      Envelope env = new Envelope(i, new JobID(), new ChannelID());
      int expectedEncodedMsgSize = OutboundEnvelopeEncoder.HEADER_SIZE;

      if (random.nextBoolean()) {
        int eventsSize = random.nextInt(MAX_EVENTS_SIZE + 1);
        expectedEncodedMsgSize += eventsSize;

        events.clear();
        events.limit(eventsSize);

        env.setEventsSerialized(events);
      }

      if (random.nextBoolean()) {
        numBuffers++;

        int bufferSize = random.nextInt(MAX_BUFFER_SIZE + 1);
        when(buffer.size()).thenReturn(bufferSize);
        env.setBuffer(buffer);

        expectedEncodedMsgSize += bufferSize;
      }

      Assert.assertTrue(channel.writeOutbound(env));

      // --------------------------------------------------------------------
      // verify encoded ByteBuf size
      // --------------------------------------------------------------------
      ByteBuf encodedMsg = (ByteBuf) channel.readOutbound();
      Assert.assertEquals(expectedEncodedMsgSize, encodedMsg.readableBytes());

      encodedMsg.release();
    }
View Full Code Here

  }

  @Test
  public void testBufferStaging() 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);

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

    Envelope[] envelopes = nextEnvelopes(3, true);

    ByteBuf buf = encode(ch, envelopes);

    when(this.bufferProvider.registerBufferAvailabilityListener(Matchers.<BufferAvailabilityListener>anyObject()))
        .thenReturn(BufferAvailabilityRegistration.SUCCEEDED_REGISTERED);

    Buffer buffer = allocBuffer(envelopes[2].getBuffer().size());

    when(this.bufferProvider.requestBuffer(anyInt()))
        .thenReturn(null, null, buffer, null);

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

    // slices: [0] => full envelope, [1] => half envelope, [2] => remaining half + full envelope
    ByteBuf[] slices = slice(buf,
        OutboundEnvelopeEncoder.HEADER_SIZE + envelopes[0].getBuffer().size(),
        OutboundEnvelopeEncoder.HEADER_SIZE + envelopes[1].getBuffer().size() / 2);

    // 1. no buffer available, incoming slice contains all data
    int refCount = slices[0].refCnt();

    decodeAndVerify(ch, slices[0]);

    Assert.assertEquals(refCount + 1, slices[0].refCnt());
    Assert.assertFalse(ch.config().isAutoRead());

    // notify of available buffer (=> bufferAvailable() callback does return a buffer
    // of the current network buffer size; the decoder needs to adjust its size to the
    // requested size
    decoder.bufferAvailable(allocBuffer(envelopes[0].getBuffer().size() * 2));
    ch.runPendingTasks();

    Assert.assertEquals(refCount - 1, slices[0].refCnt());
    Assert.assertTrue(ch.config().isAutoRead());

    decodeAndVerify(ch, envelopes[0]);

    // 2. no buffer available, incoming slice does NOT contain all data
    refCount = slices[1].refCnt();

    decodeAndVerify(ch, slices[1]);

    Assert.assertEquals(refCount + 1, slices[1].refCnt());
    Assert.assertFalse(ch.config().isAutoRead());

    decoder.bufferAvailable(allocBuffer());
    ch.runPendingTasks();

    Assert.assertEquals(refCount - 1, slices[1].refCnt());
    Assert.assertTrue(ch.config().isAutoRead());

    decodeAndVerify(ch);

    // 3. buffer available
    refCount = slices[2].refCnt();

    decodeAndVerify(ch, slices[2], envelopes[1], envelopes[2]);

    Assert.assertEquals(refCount - 1, slices[2].refCnt());
    Assert.assertTrue(ch.config().isAutoRead());

    Assert.assertEquals(1, buf.refCnt());
    buf.release();
  }
View Full Code Here

    buf.release();
  }

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

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

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

    ByteBuf buf = encode(ch, nextEnvelope(true));

    when(this.bufferProvider.requestBuffer(anyInt()))
        .thenReturn(null);

    when(this.bufferProvider.registerBufferAvailabilityListener(Matchers.<BufferAvailabilityListener>anyObject()))
        .thenReturn(BufferAvailabilityRegistration.SUCCEEDED_REGISTERED);

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

    int refCount = buf.refCnt();

    decodeAndVerify(ch, buf);

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

    try {
      decodeAndVerify(ch, buf);
      Assert.fail("Expected IllegalStateException not thrown");
View Full Code Here

    buf.release();
  }

  @Test
  public void testBufferAvailabilityRegistrationBufferAvailable() 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

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.