@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()); }
@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()); }
@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()"); }
// 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); }
@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); }