fineract-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From my...@apache.org
Subject [fineract-cn-service-starter] 21/23: Fiddling with wait code.
Date Mon, 22 Jan 2018 15:21:33 GMT
This is an automated email from the ASF dual-hosted git repository.

myrle pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract-cn-service-starter.git

commit 20b9da2f8a5b009b112f800b50d79748c17baed1
Author: myrle-krantz <mkrantz@mifos.org>
AuthorDate: Tue Jun 13 20:51:01 2017 +0200

    Fiddling with wait code.
---
 .../core/test/servicestarter/Microservice.java     | 54 +++++++++++++++-------
 1 file changed, 38 insertions(+), 16 deletions(-)

diff --git a/src/main/java/io/mifos/core/test/servicestarter/Microservice.java b/src/main/java/io/mifos/core/test/servicestarter/Microservice.java
index baf247f..52d0920 100644
--- a/src/main/java/io/mifos/core/test/servicestarter/Microservice.java
+++ b/src/main/java/io/mifos/core/test/servicestarter/Microservice.java
@@ -29,7 +29,6 @@ import java.io.File;
 import java.io.IOException;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
-import java.util.stream.Stream;
 
 /**
  * @author Myrle Krantz
@@ -37,6 +36,8 @@ import java.util.stream.Stream;
 @SuppressWarnings({"unused", "WeakerAccess"})
 public class Microservice<T> extends ExternalResource {
 
+  private static final long MAX_WAIT_DEFAULT = 150;
+
   private final Class<T> clazz;
   private final String artifactName;
   private final String artifactVersion;
@@ -47,6 +48,7 @@ public class Microservice<T> extends ExternalResource {
   private Process process;
   private T api;
   private String debuggingParams = null;
+  private long maxWait = MAX_WAIT_DEFAULT;
 
   public Microservice(
           final Class<T> clazz,
@@ -90,17 +92,42 @@ public class Microservice<T> extends ExternalResource {
     return this;
   }
 
-  public void waitTillRegistered(final DiscoveryClient discoveryClient, final Microservice...
dependentOnServices) {
-    if (discoveryClient != null) {
-      Stream.concat(Stream.of(applicationName), Stream.of(dependentOnServices).map(Microservice::name))
-              .forEach(x -> {
-                boolean found = false;
-                while (!found) {
-                  final List<ServiceInstance> thisApp = discoveryClient.getInstances(x);
-                  found = !thisApp.isEmpty();
-                }
-              });
+  public Microservice<T> maxWait(final long maxWait) {
+    this.maxWait = maxWait;
+    return this;
+  }
+
+  public Microservice<T> setApiFactory(final ApiFactory newValue)
+  {
+    this.apiFactory = newValue;
+    return this;
+  }
+
+  public boolean waitTillRegistered(final DiscoveryClient discoveryClient) throws InterruptedException
{
+    if (discoveryClient == null) {
+      return false;
     }
+
+    long nextWait = 1;
+    long sumWait = 0;
+
+    boolean found = false;
+
+    while (!found) {
+      final List<ServiceInstance> thisApp = discoveryClient.getInstances(applicationName);
+      found = !thisApp.isEmpty();
+      if (!found) {
+        TimeUnit.SECONDS.sleep(nextWait);
+        sumWait += nextWait;
+        nextWait = nextWait * 2;
+
+        if (sumWait > maxWait) {
+          return false;
+        }
+      }
+    }
+
+    return true;
   }
 
   @Override
@@ -145,11 +172,6 @@ public class Microservice<T> extends ExternalResource {
     return process.exitValue();
   }
 
-  public void setApiFactory(final ApiFactory newValue)
-  {
-    this.apiFactory = newValue;
-  }
-
   public T api() {
     if (this.api == null) {
       this.api = this.apiFactory.create(clazz, processEnvironment.serverURI());

-- 
To stop receiving notification emails like this one, please contact
myrle@apache.org.

Mime
View raw message