You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@twill.apache.org by ch...@apache.org on 2015/05/21 18:24:02 UTC

incubator-twill git commit: Fixed an easy to fail test caused by race condition in the test

Repository: incubator-twill
Updated Branches:
  refs/heads/master 97f2c37b0 -> 86017426d


Fixed an easy to fail test caused by race condition in the test


Project: http://git-wip-us.apache.org/repos/asf/incubator-twill/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-twill/commit/86017426
Tree: http://git-wip-us.apache.org/repos/asf/incubator-twill/tree/86017426
Diff: http://git-wip-us.apache.org/repos/asf/incubator-twill/diff/86017426

Branch: refs/heads/master
Commit: 86017426d9ee1d6bbeeebd826ac6e9d0f734c07c
Parents: 97f2c37
Author: Terence Yim <ch...@apache.org>
Authored: Thu May 21 09:16:43 2015 -0700
Committer: Terence Yim <ch...@apache.org>
Committed: Thu May 21 09:23:48 2015 -0700

----------------------------------------------------------------------
 .../twill/yarn/ServiceDiscoveryTestRun.java     | 28 +++++++++++++++-----
 1 file changed, 21 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-twill/blob/86017426/twill-yarn/src/test/java/org/apache/twill/yarn/ServiceDiscoveryTestRun.java
----------------------------------------------------------------------
diff --git a/twill-yarn/src/test/java/org/apache/twill/yarn/ServiceDiscoveryTestRun.java b/twill-yarn/src/test/java/org/apache/twill/yarn/ServiceDiscoveryTestRun.java
index ac2c748..d34308a 100644
--- a/twill-yarn/src/test/java/org/apache/twill/yarn/ServiceDiscoveryTestRun.java
+++ b/twill-yarn/src/test/java/org/apache/twill/yarn/ServiceDiscoveryTestRun.java
@@ -18,15 +18,18 @@
 package org.apache.twill.yarn;
 
 import org.apache.twill.api.AbstractTwillRunnable;
+import org.apache.twill.api.Command;
 import org.apache.twill.api.TwillApplication;
 import org.apache.twill.api.TwillContext;
 import org.apache.twill.api.TwillController;
 import org.apache.twill.api.TwillRunner;
 import org.apache.twill.api.TwillSpecification;
 import org.apache.twill.api.logging.PrinterLogHandler;
+import org.apache.twill.common.Cancellable;
 import org.apache.twill.common.Threads;
 import org.apache.twill.discovery.Discoverable;
 import org.apache.twill.discovery.ServiceDiscovered;
+import org.junit.Assert;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -52,7 +55,9 @@ public final class ServiceDiscoveryTestRun extends BaseYarnTest {
       .withArguments("r2", "45678")
       .start();
 
-
+    ServiceDiscovered completed = controller.discoverService("completed");
+    Assert.assertTrue(YarnTestUtils.waitForSize(completed, 2, 120));
+    controller.sendCommand(Command.Builder.of("done").build());
     controller.awaitTerminated(120, TimeUnit.SECONDS);
   }
 
@@ -80,13 +85,12 @@ public final class ServiceDiscoveryTestRun extends BaseYarnTest {
 
     private static final Logger LOG = LoggerFactory.getLogger(ServiceRunnable.class);
     private static final String SERVICE_NAME = "service";
-    private volatile Thread runThread;
+    private final CountDownLatch stopLatch = new CountDownLatch(1);
 
     @Override
     public void run() {
-      this.runThread = Thread.currentThread();
       final int port = Integer.parseInt(getContext().getArguments()[0]);
-      getContext().announce(SERVICE_NAME, port);
+      Cancellable cancelService = getContext().announce(SERVICE_NAME, port);
 
       final CountDownLatch discoveredLatch = new CountDownLatch(1);
 
@@ -110,12 +114,22 @@ public final class ServiceDiscoveryTestRun extends BaseYarnTest {
       } catch (InterruptedException e) {
         LOG.warn("Interrupted.", e);
       }
+
+      // Announce the "complete" service so that the driver knows this runnable has discovered the other
+      Cancellable cancelCompleted = getContext().announce("completed", port);
+      try {
+        stopLatch.await();
+        cancelService.cancel();
+        cancelCompleted.cancel();
+      } catch (InterruptedException e) {
+        LOG.warn("Interrupted.", e);
+      }
     }
 
     @Override
-    public void stop() {
-      if (runThread != null) {
-        runThread.interrupt();
+    public void handleCommand(Command command) throws Exception {
+      if ("done".equals(command.getCommand())) {
+        stopLatch.countDown();
       }
     }
   }