Skip to content

Commit 4aa1a3f

Browse files
committed
Add task.result() convenience method
1 parent e0ad511 commit 4aa1a3f

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

src/main/java/org/apposed/appose/Service.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,20 @@ public void cancel() {
717717
request(RequestType.CANCEL, null);
718718
}
719719

720+
/**
721+
* Returns the result of this task's execution.
722+
* <p>
723+
* This is a convenience method equivalent to {@code outputs.get("result")}.
724+
* The result will be {@code null} if the task has not completed successfully
725+
* or if no result was set.
726+
* </p>
727+
*
728+
* @return The task's result value, or {@code null} if none exists.
729+
*/
730+
public Object result() {
731+
return outputs.get("result");
732+
}
733+
720734
/** Sends a request to the worker process. */
721735
private void request(RequestType requestType, Map<String, Object> args) {
722736
Map<String, Object> request = new HashMap<>();

src/test/java/org/apposed/appose/ServiceTest.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ public void testScopeGroovy() throws IOException, InterruptedException {
8282
inputs.put("age", 100);
8383
Task task = service.task(CALC_SQRT_GROOVY, inputs).waitFor();
8484
assertComplete(task);
85-
Number result = (Number) task.outputs.get("result");
85+
Number result = (Number) task.result();
8686
assertEquals(10, result.intValue());
8787

8888
inputs.put("age", 81);
8989
task = service.task("task.outputs['result'] = sqrt_age(age)", inputs).waitFor();
9090
assertComplete(task);
91-
result = (Number) task.outputs.get("result");
91+
result = (Number) task.result();
9292
assertEquals(9, result.intValue());
9393
}
9494
}
@@ -289,8 +289,45 @@ public void testInit() throws IOException, InterruptedException {
289289
Task task = service.task("task.outputs['result'] = init_value").waitFor();
290290
assertComplete(task);
291291

292-
String result = (String) task.outputs.get("result");
293-
assertEquals("initialized", result, "Init script should set init_value variable");
292+
String result = (String) task.result();
293+
assertEquals("initialized", result,
294+
"Init script should set init_value variable");
295+
}
296+
}
297+
298+
/** Tests {@link Task#result()} convenience method. */
299+
@Test
300+
public void testTaskResult() throws IOException, InterruptedException {
301+
Environment env = Appose.system();
302+
try (Service service = env.python()) {
303+
maybeDebug(service);
304+
305+
// Create a task that produces a result.
306+
Task task = service.task("task.outputs['result'] = 'success'").waitFor();
307+
assertComplete(task);
308+
309+
// Test the result() convenience method.
310+
Object result = task.result();
311+
assertEquals("success", result);
312+
313+
// Verify it's the same as directly accessing outputs.
314+
assertEquals(task.outputs.get("result"), result);
315+
}
316+
}
317+
318+
/** Tests {@link Task#result()} returns null when no result is set. */
319+
@Test
320+
public void testTaskResultNull() throws IOException, InterruptedException {
321+
Environment env = Appose.system();
322+
try (Service service = env.groovy()) {
323+
maybeDebug(service);
324+
325+
// Create a task that doesn't set a result
326+
Task task = service.task("println 'no result'").waitFor();
327+
assertComplete(task);
328+
329+
// result() should return null
330+
assertNull(task.result());
294331
}
295332
}
296333
}

0 commit comments

Comments
 (0)