Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/480#discussion_r103448474
  
    --- Diff: 
core/src/main/java/org/apache/brooklyn/util/core/task/BasicExecutionContext.java
 ---
    @@ -96,7 +98,50 @@ public ExecutionManager getExecutionManager() {
         /** returns tasks started by this context (or tasks which have all the 
tags on this object) */
         @Override
         public Set<Task<?>> getTasks() { return 
executionManager.getTasksWithAllTags(tags); }
    -     
    +
    +    /** performs execution without spawning a new task thread, though it 
does temporarily set a fake task for the purpose of getting context;
    +     * currently supports suppliers or callables  */
    +    @SuppressWarnings("unchecked")
    +    @Override
    +    public <T> Maybe<T> getImmediately(Object callableOrSupplier) {
    +        BasicTask<?> fakeTaskForContext;
    +        if (callableOrSupplier instanceof BasicTask) {
    +            fakeTaskForContext = (BasicTask<?>)callableOrSupplier;
    +            if (fakeTaskForContext.isQueuedOrSubmitted()) {
    +                if (fakeTaskForContext.isDone()) {
    +                    return Maybe.of((T)fakeTaskForContext.getUnchecked());
    +                } else {
    +                    throw new ImmediateUnsupportedException("Task is in 
progress and incomplete: "+fakeTaskForContext);
    +                }
    +            }
    +            callableOrSupplier = fakeTaskForContext.getJob();
    --- End diff --
    
    Very clever, getting the underlying `Callable` to execute from the job!
    
    However, I wonder if this just reduces the chance of leaving tasks behind 
(rather than preventing it).
    
    The test below fails when added to `ValueResolverTest` (when also including 
the change to `ValueResolver.getMaybeInternal()` to set 
`allowImmediateExecution = true` so that this code path is taken). 
    
    ```
        public void testTaskGetImmediatelyDoesNotBlockWithNestedTasks() {
            final AtomicInteger executingCount = new AtomicInteger();
            
            final SequentialTask<?> outerTask = new 
SequentialTask<>(ImmutableList.of(new Callable<String>() {
                public String call() {
                    executingCount.incrementAndGet();
                    try {
                        Time.sleep(Duration.ONE_MINUTE);
                        return "myval";
                    } finally {
                        executingCount.decrementAndGet();
                    }
                }}));
            
            for (int i = 0; i < 3; i++) {
                Maybe<String> result = 
Tasks.resolving(outerTask).as(String.class).context(app).immediately(true).getMaybe();
                Asserts.assertTrue(result.isAbsent(), "result="+result);
            }
            
            Asserts.assertFalse(outerTask.isSubmitted());
            
            // the call below default times out after 30s while the task above 
is still running
            Asserts.succeedsEventually(new Runnable() {
                public void run() {
                    Asserts.assertEquals(executingCount.get(), 0);
                }
            });
        }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to