@Override public T call() throws Exception { ec.submit(task); return task.get(); } }).build();
protected void submitIfNecessary(TaskAdaptable<?> task) { if (!task.asTask().isSubmitted()) { if (BasicExecutionContext.getCurrentExecutionContext() == null) { throw new IllegalStateException("Compound task ("+task+") launched from "+this+" missing required execution context"); } else { BasicExecutionContext.getCurrentExecutionContext().submit(task); } } }
@Override public T call() throws Exception { if (!task.asTask().isSubmitted()) { BasicExecutionContext.getCurrentExecutionContext().submit(task); } return transformer.apply(task.asTask().get()); }}); }
@Test(groups="Integration") public void testWaitForFileContentsWhenContentsAppearAfterStart() throws Exception { String fileContent = "mycontents"; String cmd = BashCommands.waitForFileContents(destFile.getAbsolutePath(), fileContent, Duration.THIRTY_SECONDS, false); ProcessTaskWrapper<String> t = execRequiringZeroAndReturningStdout(loc, cmd); exec.submit(t); // sleep for long enough to ensure the ssh command is definitely executing Thread.sleep(5*1000); assertFalse(t.isDone()); Files.write(fileContent, destFile, Charsets.UTF_8); String output = t.get(); assertFalse(output.contains("Couldn't find"), "output="+output); }
@Test(groups="Integration") public void testDownloadToStdout() throws Exception { ProcessTaskWrapper<String> t = SshTasks.newSshExecTaskFactory(loc, "cd "+destFile.getParentFile().getAbsolutePath(), BashCommands.downloadToStdout(Arrays.asList(sourceFileUrl1))+" | sed s/my/your/") .requiringZeroAndReturningStdout().newTask(); String result = exec.submit(t).get(); assertTrue(result.trim().equals("yoursource1"), "Wrong contents of stdout download: "+result); }
@Test(groups="Integration") public void testAlternatives() throws Exception { ProcessTaskWrapper<Integer> t = SshTasks.newSshExecTaskFactory(loc) .add(BashCommands.alternatives(Arrays.asList("asdfj_no_such_command_1", "exit 88"))) .newTask(); Integer returnCode = exec.submit(t).get(); log.info("alternatives for bad commands gave: "+returnCode+"; err="+new String(t.getStderr())+"; out="+new String(t.getStdout())); assertEquals(returnCode, (Integer)88); }
@Test public void testComplex() throws Exception { Task<List<?>> t = Tasks.sequential( sayTask("1"), sayTask("2"), Tasks.parallel(sayTask("4"), sayTask("3")), sayTask("5") ); ec.submit(t); Assert.assertEquals(t.get().size(), 4); Asserts.assertEqualsIgnoringOrder((List<?>)t.get().get(2), ImmutableSet.of("3", "4")); Assert.assertTrue(messages.equals(Arrays.asList("1", "2", "3", "4", "5")) || messages.equals(Arrays.asList("1", "2", "4", "3", "5")), "messages="+messages); }
@Test(groups="Integration") public void testAlternativesWhereFirstSucceeds() throws Exception { ProcessTaskWrapper<Integer> t = SshTasks.newSshExecTaskFactory(loc) .add(BashCommands.alternatives(Arrays.asList("echo first", "exit 88"))) .newTask(); Integer returnCode = exec.submit(t).get(); String stdout = t.getStdout(); String stderr = t.getStderr(); log.info("alternatives for good first command gave: "+returnCode+"; err="+stderr+"; out="+stdout); assertTrue(stdout.contains("first"), "errcode="+returnCode+"; stdout="+stdout+"; stderr="+stderr); assertEquals(returnCode, (Integer)0); }
@Test public void runParallelTask() throws Exception { BasicTask<String> t1 = taskReturning("a"); BasicTask<String> t2 = taskReturning("b"); BasicTask<String> t3 = taskReturning("c"); BasicTask<String> t4 = taskReturning("d"); Task<List<String>> tSequence = ec.submit(new ParallelTask<String>(t4, t2, t1, t3)); assertEquals(new HashSet<String>(tSequence.get()), ImmutableSet.of("a", "b", "c", "d")); }
@Test(groups="Integration") public void testRequireFileHandlesFailure() throws Exception { ProcessTaskWrapper<?> t = SshTasks.newSshExecTaskFactory(loc) .add(BashCommands.requireFile(sourceNonExistantFile.getPath())).newTask(); exec.submit(t).get(); assertNotEquals(t.getExitCode(), 0); assertTrue(t.getStderr().contains("required file"), "Expected message in: "+t.getStderr()); assertTrue(t.getStderr().contains(sourceNonExistantFile.getPath()), "Expected message in: "+t.getStderr()); assertTrue(t.getStdout().contains("required file"), "Expected message in: "+t.getStdout()); assertTrue(t.getStdout().contains(sourceNonExistantFile.getPath()), "Expected message in: "+t.getStdout()); }
@Test public void runSequenceTask() throws Exception { BasicTask<String> t1 = taskReturning("a"); BasicTask<String> t2 = taskReturning("b"); BasicTask<String> t3 = taskReturning("c"); BasicTask<String> t4 = taskReturning("d"); Task<List<String>> tSequence = ec.submit(new SequentialTask<String>(t1, t2, t3, t4)); assertEquals(tSequence.get(), ImmutableList.of("a", "b", "c", "d")); }
@Test(groups="Integration") public void testRequireTestHandlesSuccess() throws Exception { ProcessTaskWrapper<?> t = SshTasks.newSshExecTaskFactory(loc) .add(BashCommands.requireTest("-f "+sourceFile1.getPath(), "The requested file does not exist")).newTask(); exec.submit(t).get(); assertEquals(t.getExitCode(), (Integer)0); assertTrue(t.getStderr().equals(""), "Expected no stderr messages, but got: "+t.getStderr()); }
@Test(groups="Integration") public void testRequireTestHandlesFailure() throws Exception { ProcessTaskWrapper<?> t = SshTasks.newSshExecTaskFactory(loc) .add(BashCommands.requireTest("-f "+sourceNonExistantFile.getPath(), "The requested file does not exist")).newTask(); exec.submit(t).get(); assertNotEquals(t.getExitCode(), 0); assertTrue(t.getStderr().contains("The requested file"), "Expected message in: "+t.getStderr()); assertTrue(t.getStdout().contains("The requested file"), "Expected message in: "+t.getStdout()); }
@Test(groups="Integration") public void testRequireFileHandlesSuccess() throws Exception { ProcessTaskWrapper<?> t = SshTasks.newSshExecTaskFactory(loc) .add(BashCommands.requireFile(sourceFile1.getPath())).newTask(); exec.submit(t).get(); assertEquals(t.getExitCode(), (Integer)0); assertTrue(t.getStderr().equals(""), "Expected no stderr messages, but got: "+t.getStderr()); }
private ProcessTaskWrapper<String> execRequiringZeroAndReturningStdout(SshMachineLocation loc, String... cmds) { ProcessTaskWrapper<String> t = SshTasks.newSshExecTaskFactory(loc, cmds) .requiringZeroAndReturningStdout().newTask(); exec.submit(t); return t; }
@Test(groups="Integration") public void testRequireFailureExitsImmediately() throws Exception { ProcessTaskWrapper<?> t = SshTasks.newSshExecTaskFactory(loc) .add(BashCommands.requireTest("-f "+sourceNonExistantFile.getPath(), "The requested file does not exist")) .add("echo shouldnae come here").newTask(); exec.submit(t).get(); assertNotEquals(t.getExitCode(), 0); assertTrue(t.getStderr().contains("The requested file"), "Expected message in: "+t.getStderr()); assertTrue(t.getStdout().contains("The requested file"), "Expected message in: "+t.getStdout()); Assert.assertFalse(t.getStdout().contains("shouldnae"), "Expected message in: "+t.getStdout()); }
@Test public void testByDefaultChildrenFailureAbortsSecondaryFailsPrimaryButNotAbortsPrimary() { Task<String> t1 = monitorableTask(null, "1", new FailCallable()); Task<String> t = Tasks.<String>builder().dynamic(true) .body(monitorableJob("main")) .add(t1).add(monitorableTask("2")).build(); ec.submit(t); releaseAndWaitForMonitorableJob("1"); Assert.assertFalse(t.blockUntilEnded(TINY_TIME)); releaseMonitorableJob("main"); Assert.assertTrue(t.blockUntilEnded(TIMEOUT)); Assert.assertEquals(messages, MutableList.of("1", "main")); Assert.assertTrue(stopwatch.elapsed(TimeUnit.MILLISECONDS) < TIMEOUT.toMilliseconds(), "took too long: "+stopwatch); Assert.assertTrue(t.isError()); Assert.assertTrue(t1.isError()); }
@Test public void testChildrenRunConcurrentlyWithPrimary() { Task<String> t = Tasks.<String>builder().dynamic(true) .body(monitorableJob("main")) .add(monitorableTask("1")).add(monitorableTask("2")).build(); ec.submit(t); releaseAndWaitForMonitorableJob("1"); releaseAndWaitForMonitorableJob("main"); Assert.assertFalse(t.blockUntilEnded(TINY_TIME)); releaseMonitorableJob("2"); Assert.assertTrue(t.blockUntilEnded(TIMEOUT)); Assert.assertEquals(messages, MutableList.of("1", "main", "2")); Assert.assertTrue(stopwatch.elapsed(TimeUnit.MILLISECONDS) < TIMEOUT.toMilliseconds(), "took too long: "+stopwatch); Assert.assertFalse(t.isError()); }
@Test public void testTaskBuilderUsingAddAllChildren() { Task<String> t = Tasks.<String>builder().dynamic(true) .body(monitorableJob("main")) .addAll(ImmutableList.of(monitorableTask("1"), monitorableTask("2"))) .build(); ec.submit(t); releaseAndWaitForMonitorableJob("1"); releaseAndWaitForMonitorableJob("2"); releaseAndWaitForMonitorableJob("main"); Assert.assertEquals(messages, MutableList.of("1", "2", "main")); } }
@Test public void testTaskBuilderUsingAddVarargChildren() { Task<String> t = Tasks.<String>builder().dynamic(true) .body(monitorableJob("main")) .add(monitorableTask("1"), monitorableTask("2")) .build(); ec.submit(t); releaseAndWaitForMonitorableJob("1"); releaseAndWaitForMonitorableJob("2"); releaseAndWaitForMonitorableJob("main"); Assert.assertEquals(messages, MutableList.of("1", "2", "main")); }