brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aledsage <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request #464: Avoid uncaught exceptions in testOnDoneCa...
Date Mon, 28 Nov 2016 13:20:11 GMT
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/464#discussion_r89778844
  
    --- Diff: core/src/test/java/org/apache/brooklyn/core/mgmt/internal/EntityExecutionManagerTest.java
---
    @@ -69,32 +71,47 @@
     import com.google.common.util.concurrent.Callables;
     
     /** Includes many tests for {@link BrooklynGarbageCollector} */
    +@Test
     public class EntityExecutionManagerTest extends BrooklynAppUnitTestSupport {
         
         private static final Logger LOG = LoggerFactory.getLogger(EntityExecutionManagerTest.class);
    -    
    -    @Test
    +
         public void testOnDoneCallback() throws InterruptedException {
             ExecutionManager em = mgmt.getExecutionManager();
             BasicExecutionManager bem = (BasicExecutionManager)em;
             final Map<Task<?>,Duration> completedTasks = MutableMap.of();
    -        final Semaphore sema4 = new Semaphore(-1);
    +        final CountDownLatch latch = new CountDownLatch(2);
             bem.addListener(new ExecutionListener() {
                 @Override
                 public void onTaskDone(Task<?> task) {
                     Assert.assertTrue(task.isDone());
    -                Assert.assertEquals(task.getUnchecked(), "foo");
    -                completedTasks.put(task, Duration.sinceUtc(task.getEndTimeUtc()));
    -                sema4.release();
    +                Object result = task.getUnchecked();
    +                if (result != null && result.equals("foo")) {
    +                    synchronized (completedTasks) {
    +                        completedTasks.put(task, Duration.sinceUtc(task.getEndTimeUtc()));
    +                    }
    +                    latch.countDown();
    +                }
                 }
             });
    -        Task<String> t1 = em.submit( Tasks.<String>builder().displayName("t1").dynamic(false).body(Callables.returning("foo")).build()
);
    -        t1.getUnchecked();
    -        Task<String> t2 = em.submit( Tasks.<String>builder().displayName("t2").dynamic(false).body(Callables.returning("foo")).build()
);
    -        sema4.acquire();
    -        Assert.assertEquals(completedTasks.size(), 2, "completed tasks are: "+completedTasks);
    -        completedTasks.get(t1).isShorterThan(Duration.TEN_SECONDS);
    -        completedTasks.get(t2).isShorterThan(Duration.TEN_SECONDS);
    +        Task<String> t1 = em.submit(
    +            Tasks.<String>builder()
    +                .displayName("t1")
    +                .dynamic(false)
    +                .body(Callables.returning("foo"))
    +                .build());
    +        Task<String> t2 = em.submit(
    +            Tasks.<String>builder()
    +                .displayName("t2")
    +                .dynamic(false)
    +                .body(Callables.returning("foo"))
    +                .build());
    +        latch.await(Asserts.DEFAULT_LONG_TIMEOUT.toMilliseconds(), TimeUnit.MILLISECONDS);
    --- End diff --
    
    FYI This will return `false` if not satisfied within the given time. But that's fine.
The assertion below will then fail.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message