Могу ли я смоделировать медленное соединение с ChannelTrafficShapingHandler и EmbeddedChannel в модульном тесте? - PullRequest
0 голосов
/ 01 февраля 2020

Интересно, смогу ли я использовать вышеупомянутые два компонента netty для имитации c ситуации, когда один канал ограничен по скорости и большие данные отправляются в канал, и для его получения требуется много времени. Я пытался написать что-то вроде ниже, но это никогда не работает. Спасибо.

 @Test
  public void nettyTest() {
    EmbeddedChannel channel = new EmbeddedChannel( );
    ChannelPipeline pipeline = channel.pipeline();
    pipeline.addFirst(new ChannelTrafficShapingHandler(1024, 1024, 1000, 10))
        .addLast(new StringDecoder()).addLast(new StringEncoder());

    ByteBuf byteBuf;

    CountDownLatch countDownLatch = new CountDownLatch(1);

    try {
      File file = new File("testSample.txt");
      FileInputStream fileInputStream = new FileInputStream(file);
      FileChannel fileChannel = fileInputStream.getChannel();
      MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());

      byteBuf = Unpooled.wrappedBuffer(mappedByteBuffer);

      pipeline.writeAndFlush(byteBuf).addListener(future -> {
        countDownLatch.countDown();
      });

      String res = ((EmbeddedChannel)pipeline.channel()).readInbound();
      System.out.println(res); 

      countDownLatch.await(10, TimeUnit.SECONDS);

    } catch (FileNotFoundException | InterruptedException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
...