@Override public T call() throws Exception { ExecutionContext executionContext = ((EntityInternal) entity).getExecutionContext(); return executionContext.submit(Maps.newHashMap(), job).get(); } }
/** @return The value of the most recently run task */ @Override public Object get() throws InterruptedException, ExecutionException { blockUntilStarted(); blockUntilFirstScheduleStarted(); return (groovyTruth(recentRun)) ? recentRun.get() : internalFuture.get(); }
@Override public T call() throws Exception { ec.submit(task); return task.get(); } }).build();
protected Exception assertTaskFails(Task<?> t) { try { t.get(); Assert.fail("Should have failed"); } catch (Exception e) { Asserts.expectedFailure(e); return e; } return null; }
@Test public void testCanInvokeEffector() throws Exception { // invocation map syntax Task<String> task = entity.invoke(MyEntity.CONCATENATE, ImmutableMap.of("first", "a", "second", "b")); assertEquals(task.get(TIMEOUT, TimeUnit.MILLISECONDS), "ab"); // method syntax assertEquals("xy", entity.concatenate("x", "y")); }
@Test public void testObjectArgs() throws Exception { String result1 = entity.invoke(MyEntity.EFFECTOR_WITH_OBJECT_ARGS, ImmutableMap.of("objectArg", "myval")).get(); assertEquals(result1, "effectorWithObjectArgs(Object objectArg=myval)"); String result2 = entity.invoke(MyEntity.EFFECTOR_WITH_OBJECT_ARGS, ImmutableMap.of("objectArg", 1)).get(); assertEquals(result2, "effectorWithObjectArgs(Object objectArg=1)"); // method syntax String result1b = entity.effectorWithObjectArgs("myval"); assertEquals(result1b, result1); String result2b = entity.effectorWithObjectArgs(1); assertEquals(result2b, result2); }
@Override public V2 call() throws Exception { List<V> prePostProgress = DynamicTasks.queue(parallelTask).get(); return DynamicTasks.queue( Tasks.<V2>builder().displayName("post-processing").description("Applying "+postProcessFromMultiple) .body(Functionals.callable(postProcessFromMultiple, prePostProgress)) .build()).get(); } })
@Test public void testSyntaxOneDouble1() throws Exception { // just use "dynamic" support of effector Assert.assertEquals(app.invoke(DOUBLE_1, MutableMap.of("numberToDouble", 3)).get(), (Integer)6); }
@Test public void runSimpleRunnable() throws Exception { data.put(1, "a"); Task<?> t = em.submit(MutableMap.of("tag", "A"), newPutRunnable(1, "b")); assertEquals(null, t.get()); assertEquals("b", data.get(1)); }
@Test public void runSimpleCallable() throws Exception { data.put(1, "a"); Task<?> t = em.submit(MutableMap.of("tag", "A"), newPutCallable(1, "b")); assertEquals("a", t.get()); assertEquals("b", data.get(1)); }
@Test public void runSimpleBasicTask() throws Exception { BasicTask<Object> t = new BasicTask<Object>(newPutCallable(1, "b")); data.put(1, "a"); Task<Object> t2 = em.submit(MutableMap.of("tag", "A"), t); assertEquals("a", t.get()); assertEquals("a", t2.get()); assertEquals("b", data.get(1)); }
@Override public void resetPasswordOnChildren(String newPassword, Integer secretPin) throws Exception { for (TestEntityWithEffectors child : Iterables.filter(getChildren(), TestEntityWithEffectors.class)) { Entities.invokeEffector(this, child, RESET_PASSWORD, ImmutableMap.of("newPassword", newPassword, "secretPin", secretPin)).get(); child.resetPassword(newPassword, secretPin); } } }
@Test public void testRetrievingTasksWithTagsReturnsExpectedTask() throws Exception { Task<?> t = new BasicTask<Void>(newNoop()); em.submit(MutableMap.of("tag", "A"), t); t.get(); assertEquals(em.getTasksWithTag("A"), ImmutableList.of(t)); assertEquals(em.getTasksWithAnyTag(ImmutableList.of("A")), ImmutableList.of(t)); assertEquals(em.getTasksWithAnyTag(ImmutableList.of("A", "B")), ImmutableList.of(t)); assertEquals(em.getTasksWithAllTags(ImmutableList.of("A")), ImmutableList.of(t)); }
/** the composed effector should allow us to inspect its children */ @Test public void testComposedEffectorBasic() throws Exception { Entity txp1 = app.createAndManageChild(EntitySpec.create(Entity.class, Txp1Entity.class)); Task<Integer> e = txp1.invoke(TWO_X_PLUS_ONE_BASIC, MutableMap.of("numberToStartWith", 3)); Assert.assertTrue(e instanceof DynamicSequentialTask); Assert.assertEquals(e.get(), (Integer)7); Assert.assertEquals( Iterables.size( ((HasTaskChildren)e).getChildren() ), 2); }
@Test public void testSyntaxOneTaggedCorrectly() throws Exception { Task<Integer> t = app.invoke(DOUBLE_1, MutableMap.of("numberToDouble", 3)); t.get(); checkTags(t, app, DOUBLE_1, false); }
@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 // also assert it works when the effector is defined on an entity public void testSimpleEffectorOnEntity() throws Exception { Entity doubler = app.createAndManageChild(EntitySpec.create(Entity.class, DoublingEntity.class)); Assert.assertEquals(doubler.invoke(DOUBLE_1, MutableMap.of("numberToDouble", 3)).get(), (Integer)6); }
@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 public void testFormatString() throws Exception { Task<String> t = DependentConfiguration.formatString("%s://%s:%d/", "http", new BasicTask<String>(Callables.returning("localhost")), DependentConfiguration.transform(new BasicTask<Integer>(Callables.returning(8080)), incrementerFunction())); submit(t); Assert.assertEquals(t.get(TIMEOUT_MS, TimeUnit.MILLISECONDS), "http://localhost:8081/"); }
@Test public void testHttpEffectorWithJsonPath() throws Exception { new HttpCommandEffector(ConfigBag.newInstance() .configure(HttpCommandEffector.EFFECTOR_NAME, "Httpbin") .configure(HttpCommandEffector.EFFECTOR_URI, serverUrl + "/get?id=myId") .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "GET") .configure(HttpCommandEffector.JSON_PATH, "$.args.id") ).apply(entity); String val = entity.invoke(EFFECTOR_HTTPBIN, MutableMap.<String,String>of()).get(); Assert.assertEquals(val, "myId"); }