示例#1
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());
  }
示例#2
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());
  }
示例#3
0
  @Test
  public void noExecutableFound() {
    System.err.println("Starting test noExecutableFound()");

    final Semaphore semaphore = new Semaphore(0);
    final AtomicInteger exitCode = new AtomicInteger();

    NuProcessHandler processListener =
        new NuAbstractProcessHandler() {
          @Override
          public void onExit(int statusCode) {
            exitCode.set(statusCode);
            semaphore.release();
          }
        };

    NuProcessBuilder pb = new NuProcessBuilder(processListener, "/bin/zxczxc");
    NuProcess process = pb.start();
    semaphore.acquireUninterruptibly();
    Assert.assertFalse("Process incorrectly reported running", process.isRunning());
    Assert.assertEquals(
        "Output did not matched expected result", Integer.MIN_VALUE, exitCode.get());

    System.err.println("Completed test noExecutableFound()");
  }
示例#4
0
  @Test
  public void badExit() throws InterruptedException {
    System.err.println("Starting test badExit()");

    final AtomicInteger asyncExitCode = new AtomicInteger();
    final CountDownLatch exitLatch = new CountDownLatch(1);

    NuProcessHandler processListener =
        new NuAbstractProcessHandler() {
          @Override
          public void onExit(int statusCode) {
            asyncExitCode.set(statusCode);
            exitLatch.countDown();
          }
        };

    NuProcessBuilder pb = new NuProcessBuilder(processListener, command, "/tmp/sdfadsf");
    NuProcess nuProcess = pb.start();
    int syncExitCode = nuProcess.waitFor(5, TimeUnit.SECONDS);
    boolean countedDown = exitLatch.await(5, TimeUnit.SECONDS);
    Assert.assertTrue("Async exit latch was not triggered", countedDown);

    int expectedExitCode = System.getProperty("os.name").toLowerCase().contains("win") ? -1 : 1;
    Assert.assertEquals(
        "Exit code (synchronous) did not match expectation", expectedExitCode, syncExitCode);
    Assert.assertEquals(
        "Exit code (asynchronous) did not match expectation",
        expectedExitCode,
        asyncExitCode.get());

    System.err.println("Completed test badExit()");
  }
示例#5
0
 // TODO: DirectWriteBig will explore a bug when using writeStdin at onStart()
 //       (has problem on Mac OS X and Linux, but works on Win32)
 // @Test
 public void testDirectWriteBig() throws InterruptedException {
   ProcessHandler2 processListener = new ProcessHandler2();
   NuProcessBuilder pb = new NuProcessBuilder(processListener, command);
   NuProcess nuProcess = pb.start();
   nuProcess.waitFor(0, TimeUnit.SECONDS);
   Assert.assertEquals(
       "Checksums did not match", processListener.checksum, processListener.checksum2);
 }
示例#6
0
 @Test
 public void testDirectWrite() throws InterruptedException {
   ProcessHandler1 processListener = new ProcessHandler1();
   NuProcessBuilder pb = new NuProcessBuilder(processListener, command);
   NuProcess nuProcess = pb.start();
   nuProcess.waitFor(0, TimeUnit.SECONDS);
   Assert.assertEquals("Results did not match", "This is a test", processListener.result);
 }
示例#7
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);
 }
示例#8
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();
      }
    }
示例#9
0
    @Override
    public void onStart(NuProcess nuProcess) {
      this.nuProcess = nuProcess;

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

      System.out.println("Writing: This is a test");
      nuProcess.writeStdin(buffer);
    }
示例#10
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);
    }
示例#11
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);
      }
    }
示例#12
0
    @Override
    public void onStart(NuProcess nuProcess) {
      this.nuProcess = nuProcess;

      ByteBuffer buffer = ByteBuffer.allocate(1024 * 128);
      for (int i = 0; i < buffer.capacity(); i++) {
        byte b = (byte) (i % 256);
        buffer.put(b);
        checksum += b;
      }

      buffer.flip();

      System.out.println("Writing: 128K of data, waiting for checksum " + checksum);
      nuProcess.writeStdin(buffer);
    }
示例#13
0
 @Override
 public void onStart(NuProcess nuProcess) {
   this.nuProcess = nuProcess;
   nuProcess.wantWrite();
 }