Repository: incubator-tephra
Updated Branches:
  refs/heads/master 67eaa768c -> 14858e4af


Wait for Transaction Service to announce itself to fix flaky tests

This closes #9

Signed-off-by: poorna <[email protected]>


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

Branch: refs/heads/master
Commit: 14858e4af2ff225f763faf47c23d20e681aac260
Parents: e02e705
Author: poorna <[email protected]>
Authored: Fri Sep 9 02:31:12 2016 -0700
Committer: poorna <[email protected]>
Committed: Fri Sep 9 12:02:41 2016 -0700

----------------------------------------------------------------------
 .../tephra/ThriftTransactionSystemTest.java     |  5 ++
 .../org/apache/tephra/TransactionAdminTest.java |  4 ++
 .../distributed/PooledClientProviderTest.java   | 16 +-----
 .../ThriftTransactionServerTest.java            | 21 +++----
 .../test/java/org/apache/tephra/util/Tests.java | 59 ++++++++++++++++++++
 .../tephra/examples/BalanceBooksTest.java       |  3 +
 6 files changed, 81 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tephra/blob/14858e4a/tephra-core/src/test/java/org/apache/tephra/ThriftTransactionSystemTest.java
----------------------------------------------------------------------
diff --git 
a/tephra-core/src/test/java/org/apache/tephra/ThriftTransactionSystemTest.java 
b/tephra-core/src/test/java/org/apache/tephra/ThriftTransactionSystemTest.java
index 28000ff..ef9b9c2 100644
--- 
a/tephra-core/src/test/java/org/apache/tephra/ThriftTransactionSystemTest.java
+++ 
b/tephra-core/src/test/java/org/apache/tephra/ThriftTransactionSystemTest.java
@@ -32,6 +32,7 @@ import org.apache.tephra.runtime.DiscoveryModules;
 import org.apache.tephra.runtime.TransactionClientModule;
 import org.apache.tephra.runtime.TransactionModules;
 import org.apache.tephra.runtime.ZKModule;
+import org.apache.tephra.util.Tests;
 import org.apache.twill.internal.zookeeper.InMemoryZKServer;
 import org.apache.twill.zookeeper.ZKClientService;
 import org.junit.AfterClass;
@@ -88,10 +89,14 @@ public class ThriftTransactionSystemTest extends 
TransactionSystemTest {
     txClient = injector.getInstance(TransactionSystemClient.class);
     try {
       LOG.info("Starting transaction service");
+      storage.startAndWait();
       txService.startAndWait();
     } catch (Exception e) {
       LOG.error("Failed to start service: ", e);
+      throw e;
     }
+
+    Tests.waitForTxReady(txClient);
   }
   
   @Before

http://git-wip-us.apache.org/repos/asf/incubator-tephra/blob/14858e4a/tephra-core/src/test/java/org/apache/tephra/TransactionAdminTest.java
----------------------------------------------------------------------
diff --git 
a/tephra-core/src/test/java/org/apache/tephra/TransactionAdminTest.java 
b/tephra-core/src/test/java/org/apache/tephra/TransactionAdminTest.java
index 9305229..27fc071 100644
--- a/tephra-core/src/test/java/org/apache/tephra/TransactionAdminTest.java
+++ b/tephra-core/src/test/java/org/apache/tephra/TransactionAdminTest.java
@@ -32,6 +32,7 @@ import org.apache.tephra.runtime.DiscoveryModules;
 import org.apache.tephra.runtime.TransactionClientModule;
 import org.apache.tephra.runtime.TransactionModules;
 import org.apache.tephra.runtime.ZKModule;
+import org.apache.tephra.util.Tests;
 import org.apache.twill.internal.zookeeper.InMemoryZKServer;
 import org.apache.twill.zookeeper.ZKClientService;
 import org.junit.AfterClass;
@@ -96,7 +97,10 @@ public class TransactionAdminTest {
       txService.startAndWait();
     } catch (Exception e) {
       LOG.error("Failed to start service: ", e);
+      throw e;
     }
+
+    Tests.waitForTxReady(txClient);
   }
 
   @Before

http://git-wip-us.apache.org/repos/asf/incubator-tephra/blob/14858e4a/tephra-core/src/test/java/org/apache/tephra/distributed/PooledClientProviderTest.java
----------------------------------------------------------------------
diff --git 
a/tephra-core/src/test/java/org/apache/tephra/distributed/PooledClientProviderTest.java
 
b/tephra-core/src/test/java/org/apache/tephra/distributed/PooledClientProviderTest.java
index dce8078..bee0ec2 100644
--- 
a/tephra-core/src/test/java/org/apache/tephra/distributed/PooledClientProviderTest.java
+++ 
b/tephra-core/src/test/java/org/apache/tephra/distributed/PooledClientProviderTest.java
@@ -29,6 +29,7 @@ import org.apache.tephra.runtime.DiscoveryModules;
 import org.apache.tephra.runtime.TransactionClientModule;
 import org.apache.tephra.runtime.TransactionModules;
 import org.apache.tephra.runtime.ZKModule;
+import org.apache.tephra.util.Tests;
 import org.apache.thrift.TException;
 import org.apache.twill.discovery.DiscoveryServiceClient;
 import org.apache.twill.internal.zookeeper.InMemoryZKServer;
@@ -48,7 +49,6 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
 public class PooledClientProviderTest {
@@ -122,7 +122,8 @@ public class PooledClientProviderTest {
 
     // Need to retry, since TransactionServiceMain#start returning doesn't 
indicate that the TransactionService
     // has registered itself for discovery yet
-    waitFor("Failed to get client.", new Callable<Boolean>() {
+    Tests.waitFor("Failed to get client.", new Callable<Boolean>() {
+      @SuppressWarnings({"unused", "EmptyTryBlock"})
       @Override
       public Boolean call() throws Exception {
         try (CloseableThriftClient closeableThriftClient = 
clientProvider.getCloseableClient()) {
@@ -177,17 +178,6 @@ public class PooledClientProviderTest {
     executor.shutdownNow();
   }
 
-  private void waitFor(String errorMessage, Callable<Boolean> callable) throws 
Exception {
-    for (int i = 0; i < 600; i++) {
-      boolean value = callable.call();
-      if (value) {
-        return;
-      }
-      TimeUnit.MILLISECONDS.sleep(50);
-    }
-    Assert.fail(errorMessage);
-  }
-
   private static class RetrieveClient implements Callable<Integer> {
     private final PooledClientProvider pool;
     private final CountDownLatch done;

http://git-wip-us.apache.org/repos/asf/incubator-tephra/blob/14858e4a/tephra-core/src/test/java/org/apache/tephra/distributed/ThriftTransactionServerTest.java
----------------------------------------------------------------------
diff --git 
a/tephra-core/src/test/java/org/apache/tephra/distributed/ThriftTransactionServerTest.java
 
b/tephra-core/src/test/java/org/apache/tephra/distributed/ThriftTransactionServerTest.java
index 4f9a79f..6075452 100644
--- 
a/tephra-core/src/test/java/org/apache/tephra/distributed/ThriftTransactionServerTest.java
+++ 
b/tephra-core/src/test/java/org/apache/tephra/distributed/ThriftTransactionServerTest.java
@@ -39,6 +39,7 @@ import org.apache.tephra.runtime.DiscoveryModules;
 import org.apache.tephra.runtime.TransactionClientModule;
 import org.apache.tephra.runtime.TransactionModules;
 import org.apache.tephra.runtime.ZKModule;
+import org.apache.tephra.util.Tests;
 import org.apache.twill.internal.zookeeper.InMemoryZKServer;
 import org.apache.twill.zookeeper.ZKClientService;
 import org.apache.zookeeper.WatchedEvent;
@@ -125,8 +126,11 @@ public class ThriftTransactionServerTest {
       txService.startAndWait();
     } catch (Exception e) {
       LOG.error("Failed to start service: ", e);
+      throw e;
     }
 
+    Tests.waitForTxReady(injector.getInstance(TransactionSystemClient.class));
+
     getClient().resetState();
 
     storageWaitLatch = new CountDownLatch(1);
@@ -200,7 +204,7 @@ public class ThriftTransactionServerTest {
     waitForThriftStop();
 
     // wait for the thrift rpc server to be in running state again
-    waitFor("Failed to wait for txService to be running.", new 
Callable<Boolean>() {
+    Tests.waitFor("Failed to wait for txService to be running.", new 
Callable<Boolean>() {
       @Override
       public Boolean call() throws Exception {
         return Service.State.RUNNING == txService.thriftRPCServerState();
@@ -235,19 +239,8 @@ public class ThriftTransactionServerTest {
     dupZookeeper.close();
   }
 
-  private void waitFor(String errorMessage, Callable<Boolean> callable) throws 
Exception {
-    for (int i = 0; i < 600; i++) {
-      boolean value = callable.call();
-      if (value) {
-        return;
-      }
-      TimeUnit.MILLISECONDS.sleep(50);
-    }
-    Assert.fail(errorMessage);
-  }
-
   private void waitForThriftStop() throws Exception {
-    waitFor("Failed to wait for txService to stop", new Callable<Boolean>() {
+    Tests.waitFor("Failed to wait for txService to stop", new 
Callable<Boolean>() {
       @Override
       public Boolean call() throws Exception {
         return Service.State.RUNNING != txService.thriftRPCServerState();
@@ -264,7 +257,7 @@ public class ThriftTransactionServerTest {
   }
 
   private static class SlowTransactionLog extends 
InMemoryTransactionStateStorage.InMemoryTransactionLog {
-    public SlowTransactionLog(long timestamp) {
+    SlowTransactionLog(long timestamp) {
       super(timestamp);
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tephra/blob/14858e4a/tephra-core/src/test/java/org/apache/tephra/util/Tests.java
----------------------------------------------------------------------
diff --git a/tephra-core/src/test/java/org/apache/tephra/util/Tests.java 
b/tephra-core/src/test/java/org/apache/tephra/util/Tests.java
new file mode 100644
index 0000000..243aa5c
--- /dev/null
+++ b/tephra-core/src/test/java/org/apache/tephra/util/Tests.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tephra.util;
+
+import org.apache.tephra.TransactionSystemClient;
+import org.apache.tephra.TxConstants;
+import org.junit.Assert;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Common methods used by Tephra tests.
+ */
+public final class Tests {
+
+  private Tests() {}
+
+  public static void waitFor(String errorMessage, Callable<Boolean> callable) 
throws Exception {
+    for (int i = 0; i < 600; i++) {
+      if (callable.call()) {
+        return;
+      }
+      TimeUnit.MILLISECONDS.sleep(50);
+    }
+    Assert.fail(errorMessage);
+  }
+
+  public static void waitForTxReady(final TransactionSystemClient txClient) 
throws Exception {
+    waitFor("Timeout waiting for transaction manager to be running", new 
Callable<Boolean>() {
+      @Override
+      public Boolean call() throws Exception {
+        try {
+          String status = txClient.status();
+          return TxConstants.STATUS_OK.equals(status);
+        } catch (Exception e) {
+          return false;
+        }
+      }
+    });
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tephra/blob/14858e4a/tephra-examples/src/test/java/org/apache/tephra/examples/BalanceBooksTest.java
----------------------------------------------------------------------
diff --git 
a/tephra-examples/src/test/java/org/apache/tephra/examples/BalanceBooksTest.java
 
b/tephra-examples/src/test/java/org/apache/tephra/examples/BalanceBooksTest.java
index 1abeece..4dfe107 100644
--- 
a/tephra-examples/src/test/java/org/apache/tephra/examples/BalanceBooksTest.java
+++ 
b/tephra-examples/src/test/java/org/apache/tephra/examples/BalanceBooksTest.java
@@ -25,6 +25,7 @@ import com.google.inject.Scopes;
 import com.google.inject.util.Modules;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.HBaseTestingUtility;
+import org.apache.tephra.TransactionSystemClient;
 import org.apache.tephra.TxConstants;
 import org.apache.tephra.distributed.TransactionService;
 import org.apache.tephra.persist.InMemoryTransactionStateStorage;
@@ -34,6 +35,7 @@ import org.apache.tephra.runtime.DiscoveryModules;
 import org.apache.tephra.runtime.TransactionClientModule;
 import org.apache.tephra.runtime.TransactionModules;
 import org.apache.tephra.runtime.ZKModule;
+import org.apache.tephra.util.Tests;
 import org.apache.twill.zookeeper.ZKClientService;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -110,6 +112,7 @@ public class BalanceBooksTest {
       throw e;
     }
 
+    Tests.waitForTxReady(injector.getInstance(TransactionSystemClient.class));
   }
 
   @AfterClass

Reply via email to