/** Starts a retriable task */ private Promise<? extends T> run(Context context) { _startedAt = System.currentTimeMillis(); Task<T> task = wrap(0); context.run(task); return task; } }
@Override public Promise<String> run(final Context context) throws Exception { context.run(innerTask); return innerTask; } };
@Override protected Promise<? extends Integer> run(final Context context) throws Exception { final int x = fetchX.get(); if (x < 42) { final int toAdd = 42 - x; final Task<Integer> addTo42 = add(x, toAdd); context.run(addTo42); return addTo42; } return Promises.value(x); } };
@Override protected Promise<Integer> run(final Context context) throws Exception { final int x = fetchX.get(); if (x < 42) { final int toAdd = 42 - x; final Task<Integer> addTo42 = add(x, toAdd); context.run(addTo42); return addTo42; } return Promises.value(x); } };
@Override protected Promise<? extends T> run(final Context context) throws Exception { final SettablePromise<T> result = Promises.settable(); Task<?> prevTask = _tasks.get(0); for (int i = 1; i < _tasks.size(); i++) { final Task<?> currTask = _tasks.get(i); context.after(prevTask).run(currTask); prevTask = currTask; } // This is unsafe, but we don't have the ability to do type checking // with varargs. @SuppressWarnings("unchecked") final Task<T> typedPrevTask = (Task<T>) prevTask; Promises.propagateResult(typedPrevTask, result); context.run(_tasks.get(0)); _tasks = null; return result; } }
@Override protected Promise<? extends String> run(final Context context) throws Exception { context.run(innerTask); return Promises.value("value1"); } };
@Override protected Promise<? extends String> run(final Context context) throws Exception { context.run(innerTask); return Promises.value("value2"); } };
@Override public void run() throws Exception { context.run(TestUtil.noop()); } });
@Override protected Promise<? extends String> run(final Context context) throws Exception { // We kick off a task that won't finish before the containing task // (this task) is finished. context.run(innerTask); return Promises.value("value"); } };
@Override protected Promise<? extends String> run(final Context context) throws Exception { context.run(innerTask); return Promises.value("value2"); } };
@Override protected Promise<? extends String> run(Context context) throws Exception { context.run(innerTask); return Promises.value("value3"); } };
@Override protected Promise<? extends String> run(final Context context) throws Exception { // We kick off a task that won't finish before the containing task // (this task) is finished. context.run(innerTask); return Promises.value("value"); } };
@Override protected Promise<? extends String> run(final Context context) throws Exception { context.run(innerTask); return Promises.value("value1"); } };
@Override public Promise<String> run(final Context context) throws Exception { context.run(predecessorTask); context.after(predecessorTask).run(successorTask); return successorTask; } };
@Test public void testAsyncWithContext() { final Task<String> t = Task.callable(() -> "done"); Task<String> task = Task.async(ctx -> { ctx.run(t); return t; }); String value = runAndWait("TestTaskFactoryMethods.testAsyncWithContext", task); assertEquals(value, "done"); assertEquals(countTasks(task.getTrace()), 2); }
@Override public Promise<String> run(final Context ctx) { final Task<String> twoStage = seq(par(fetchAndLog("http://www.bing.com"), fetchAndLog("http://www.yahoo.com")), par(fetchAndLog("http://www.google.com"), fetchAndLog("https://duckduckgo.com/")), buildResult()); ctx.run(twoStage); return twoStage; }
@Override protected Promise<? extends String> run(final Context context) throws Exception { context.after(a).run(b); context.after(a).run(c); context.after(b, c).run(d); context.run(a); return d; } };
@Override public Promise<Queue<Integer>> run(final Context context) throws Exception { final SettablePromise<Queue<Integer>> promise = Promises.settable(); final Queue<Integer> queue = new LinkedList<Integer>(); final Task<?> t1 = enqueueTask(queue, 1); t1.setPriority(-5); final Task<?> t2 = enqueueTask(queue, 2); t2.setPriority(10); final Task<?> t3 = enqueueTask(queue, 3); t3.setPriority(0); context.run(t1, t2, t3); context.after(t1, t2, t3).run(Task.action("done", () -> promise.done(queue))); return promise; } };
@Override protected Promise<Tuple2<T1, T2>> run(final Context context) throws Exception { final SettablePromise<Tuple2<T1, T2>> result = Promises.settable(); InternalUtil.fastFailAfter(p -> { if (p.isFailed()) { result.fail(p.getError()); } else { result.done(tuple(_tasks._1().get(), _tasks._2().get())); } }, _tasks._1(), _tasks._2()); _tasks.forEach(t -> context.run((Task<?>)t)); return result; }
@Override protected Promise<Tuple3<T1, T2, T3>> run(final Context context) throws Exception { final SettablePromise<Tuple3<T1, T2, T3>> result = Promises.settable(); InternalUtil.fastFailAfter(p -> { if (p.isFailed()) { result.fail(p.getError()); } else { result.done(tuple(_tasks._1().get(), _tasks._2().get(), _tasks._3().get())); } }, _tasks._1(), _tasks._2(), _tasks._3()); _tasks.forEach(t -> context.run((Task<?>)t)); return result; }