Ejemplo n.º 1
0
  @Test
  public void testConsecutiveWrites() throws InterruptedException {
    final AtomicInteger count = new AtomicInteger();

    NuProcessHandler processListener =
        new NuAbstractProcessHandler() {
          @Override
          public void onStdout(ByteBuffer buffer, boolean closed) {
            count.addAndGet(buffer.remaining());
            buffer.position(buffer.limit());
          }
        };

    NuProcessBuilder pb = new NuProcessBuilder(processListener, command);
    NuProcess nuProcess = pb.start();
    // TODO: given a large i (e.g. 1,000, 10,000), this unit test (testConsecutiveWrites) will
    //       produce a side-effect on InterruptTest (has problem on Mac OS X, but works on Linux and
    // Win32).
    //       We do not reuse fork on surefire (reuseForks=false) to address this issue for now.
    for (int i = 0; i < 1000; i++) {
      ByteBuffer buffer = ByteBuffer.allocate(64);
      buffer.put("This is a test".getBytes());
      buffer.flip();
      nuProcess.writeStdin(buffer);
    }

    Thread.sleep(500);

    nuProcess.closeStdin(true);
    nuProcess.waitFor(0, TimeUnit.SECONDS);
    Assert.assertEquals("Count did not match", 14000, count.get());
  }
Ejemplo n.º 2
0
  @Test
  public void testFewWrites() throws InterruptedException {
    final AtomicInteger count = new AtomicInteger();

    NuProcessHandler processListener =
        new NuAbstractProcessHandler() {
          @Override
          public void onStdout(ByteBuffer buffer, boolean closed) {
            count.addAndGet(buffer.remaining());
            buffer.position(buffer.limit());
          }
        };

    NuProcessBuilder pb = new NuProcessBuilder(processListener, command);
    NuProcess nuProcess = pb.start();

    ByteBuffer buffer = ByteBuffer.allocate(64);
    buffer.put("This is a test".getBytes());
    buffer.flip();
    nuProcess.writeStdin(buffer);

    Thread.sleep(500);

    nuProcess.closeStdin(true);
    nuProcess.waitFor(0, TimeUnit.SECONDS);
    Assert.assertEquals("Count did not match", 14, count.get());
  }
Ejemplo n.º 3
0
 @Override
 public void onStdout(ByteBuffer buffer, boolean closed) {
   if (buffer.hasRemaining()) {
     byte[] chars = new byte[buffer.remaining()];
     buffer.get(chars);
     result = new String(chars);
     System.out.println("Read: " + result);
   }
   nuProcess.closeStdin(true);
 }
Ejemplo n.º 4
0
    @Override
    public void onStdoutChars(CharBuffer buffer, boolean closed, CoderResult coderResult) {
      charsRead += buffer.remaining();
      decodedStdout.append(buffer);
      buffer.position(buffer.limit());

      if (charsRead == charsWritten) {
        nuProcess.closeStdin();
      }
    }
Ejemplo n.º 5
0
    @Override
    public void onStdout(ByteBuffer buffer, boolean closed) {
      size += buffer.remaining();
      if (size == (WRITES * bytes.length)) {
        nuProcess.closeStdin();
      }

      byte[] bytes = new byte[buffer.remaining()];
      buffer.get(bytes);
      readAdler32.update(bytes);
    }
Ejemplo n.º 6
0
    @Override
    public void onStdout(ByteBuffer buffer, boolean closed) {
      while (buffer.hasRemaining()) {
        checksum2 += buffer.get();
      }

      System.out.println("Reading.  Current checksum " + checksum2);
      if (checksum2 == checksum) {
        System.out.println("Checksums matched, exiting.");
        nuProcess.closeStdin(true);
      }
    }