Repository: incubator-tephra Updated Branches: refs/heads/master b9eae01f3 -> 307a585cb
Fix PooledClientProviderTest. TransactionService may not be registered for discovery once TransactionServiceMain#start returns. This closes #5 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/307a585c Tree: http://git-wip-us.apache.org/repos/asf/incubator-tephra/tree/307a585c Diff: http://git-wip-us.apache.org/repos/asf/incubator-tephra/diff/307a585c Branch: refs/heads/master Commit: 307a585cb6c288c38362cb85cb4f3ce62b3e607a Parents: cd251d3 Author: Ali Anwar <[email protected]> Authored: Thu Sep 8 21:28:59 2016 -0700 Committer: poorna <[email protected]> Committed: Thu Sep 8 22:08:52 2016 -0700 ---------------------------------------------------------------------- .../distributed/PooledClientProviderTest.java | 38 ++++++++++++++++---- 1 file changed, 32 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tephra/blob/307a585c/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 90a69e9..507cefb 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.thrift.TException; import org.apache.twill.discovery.DiscoveryServiceClient; import org.apache.twill.internal.zookeeper.InMemoryZKServer; import org.apache.twill.zookeeper.ZKClientService; @@ -47,12 +48,13 @@ 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 { - public static final int MAX_CLIENT_COUNT = 3; - public static final long CLIENT_OBTAIN_TIMEOUT = 10; + private static final int MAX_CLIENT_COUNT = 3; + private static final long CLIENT_OBTAIN_TIMEOUT = 10; @ClassRule public static TemporaryFolder tmpFolder = new TemporaryFolder(); @@ -114,12 +116,25 @@ public class PooledClientProviderTest { final PooledClientProvider clientProvider = new PooledClientProvider(conf, injector.getInstance(DiscoveryServiceClient.class)); - // test simple case of get + return. Note: this also initializes the provider's pool, which + // Test simple case of get + return. Note: this also initializes the provider's pool, which // takes about one second (discovery). Doing it before we test the threads makes it so that one // thread doesn't take exceptionally longer than the others. - try (CloseableThriftClient closeableThriftClient = clientProvider.getCloseableClient()) { - // do nothing with the client - } + + // 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>() { + @Override + public Boolean call() throws Exception { + try (CloseableThriftClient closeableThriftClient = clientProvider.getCloseableClient()) { + // do nothing with the client + } catch (TException e) { + // simply retry + return false; + } + return true; + } + }); + //Now race to get MAX_CLIENT_COUNT+1 clients, exhausting the pool and requesting 1 more. List<Future<Integer>> clientIds = new ArrayList<Future<Integer>>(); @@ -161,6 +176,17 @@ public class PooledClientProviderTest { executor.shutdown(); } + 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 long holdClientMs;
