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

    https://github.com/apache/brooklyn-server/pull/480#discussion_r103446823
  
    --- 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;
    --- End diff --
    
    Under what code path do we get here? Looking at 
`ValueResolver.getMaybeInternal(...)`, `allowImmediateExecution` will be false 
if the object is a task (rather than a `TaskFactory` or `DeferredSupplier`). 
    
    For example, the test below fails (added to `ValueResolverTest`). It leaves 
a single instance of the task executing.
    
    ```
        public void testTaskGetImmediatelyDoesNotBlock() {
            final AtomicInteger executingCount = new AtomicInteger();
            
            final Task<String> task = new BasicTask<>(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(task).as(String.class).context(app).immediately(true).getMaybe();
                Asserts.assertTrue(result.isAbsent(), "result="+result);
            }
    
            Asserts.assertFalse(task.isSubmitted());
            
            // The call below default times out after 30s while the task above 
is still running
            // Expect the task to not be left running.
            Asserts.succeedsEventually(new Runnable() {
                public void run() {
                    Asserts.assertEquals(executingCount.get(), 0);
                }
            });
        }
    ```
    
    I'm not convinced that we want to support setting a config key with a value 
of type `Task` (long term). But I guess that will require more work, to change 
how `DependentConfiguration` is implemented. So we do need to support it 
short-term :-(


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to