Related to issue 38888: Failing SS Build
authorMartin Taal <martin.taal@openbravo.com>
Mon, 26 Nov 2018 10:34:55 +0100
changeset 7907 620ffec4c95f
parent 7906 08c34265a179
child 7908 916c79b47ce6
Related to issue 38888: Failing SS Build
Change the check for outgoing batches, to be sure that all are synced
src-test/org/openbravo/test/mobile/retail/multiserver/MultiserverUtils.java
src-test/org/openbravo/test/mobile/retail/multiserver/selenium/terminals/WebPOSCentralOfflineTerminalHelper.java
--- a/src-test/org/openbravo/test/mobile/retail/multiserver/MultiserverUtils.java	Sun Nov 25 20:46:01 2018 +0100
+++ b/src-test/org/openbravo/test/mobile/retail/multiserver/MultiserverUtils.java	Mon Nov 26 10:34:55 2018 +0100
@@ -22,7 +22,6 @@
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
-import java.util.List;
 
 import org.openbravo.test.mobile.core.junit.SequentialTestInfo;
 import org.openbravo.test.mobile.core.utils.ConfigurationProperties;
@@ -40,80 +39,42 @@
   private static final long routePeriodMillis = ConfigurationProperties.INSTANCE
       .getRoutePeriodMillis();
   private static final long waitMillis = (long) ((pushPeriodMillis + routePeriodMillis) * 1.2);
-  private static final long waitMinimum = pushPeriodMillis + routePeriodMillis + 4000;
 
   public static void waitForSynchronization() {
     waitForSynchronization(SequentialTestInfo.getServerType());
   }
 
+  public static void waitForCentralOutgoingSynced() {
+    waitForOutgoingSynced(waitMillis / 10, 10, ServerType.CENTRAL,
+        SequentialTestInfo.getServerType());
+  }
+
   public static void waitForSynchronization(final ServerType storeServerType) {
-    TestLogger.getLogger().info(
-        String.format("Waiting for symetric.ds to synchronize the servers (max %d seconds)",
-            (int) waitMillis / 1000));
-
-    if (storeServerType == ServerType.UNKNOWN || waitMillis > 0) {
-      System.out.println("Waiting " + waitMillis + " milliseconds");
-      OBUtils.pause(waitMillis);
-      return;
-    }
-
-    // start chrono
-    final long startTime = System.currentTimeMillis();
 
     // initial pause
-    OBUtils.pause(5000);
-
-    final List<Integer> countOfIntents = new ArrayList<>();
-
-    final int intervalMultiplier = 5;
-    new OBWait(1000 * intervalMultiplier, (int) waitMillis / 1000 / intervalMultiplier) {
-
-      @Override
-      protected boolean until(final int currentIteration) {
-        countOfIntents.add(currentIteration);
-        // Wait until the current server and the Central server have finished the synchronization
-        boolean isServerSynchronized = isServerSynchronized(storeServerType);
-        if (!isServerSynchronized) {
-          return isServerSynchronized;
-        }
-        OBUtils.pause(2000);
-        isServerSynchronized = isServerSynchronized(ServerType.CENTRAL);
-        if (!isServerSynchronized) {
-          return isServerSynchronized;
-        }
-        return isServerSynchronized;
-      }
-
-      @Override
-      protected void timeout() {
-        final long estimatedTime = System.currentTimeMillis() - startTime;
-
-        throw new TimeoutException(String.format(
-            "The servers have not been synchronized after %s iterations and %s seconds",
-            countOfIntents.size(), estimatedTime / 1000));
-      }
-    };
-
-    final long estimatedTime = System.currentTimeMillis() - startTime;
-
-    TestLogger.getLogger().info(
-        String.format("symetric.ds required %s iterations that took %s seconds",
-            countOfIntents.size(), estimatedTime / 1000));
-
-    final long remainingTime = waitMinimum - estimatedTime;
-    if (remainingTime > 0) {
-      TestLogger.getLogger().info(
-          String.format("Minimum wait must be %s. Waiting the remaining %s seconds...",
-              waitMinimum / 1000, remainingTime / 1000));
-    }
-
-    // final pause
-    // OBUtils.pause(5000);
-
+    OBUtils.pause(2000);
+    // wait for the store to sync
+    waitForOutgoingSynced(waitMillis / 10, 10, storeServerType, ServerType.CENTRAL);
+    // wait for central to sync
+    waitForOutgoingSynced(waitMillis / 10, 10, ServerType.CENTRAL, storeServerType);
   }
 
-  public static void waitForCentralOutgoingSynced(final long interval, final int tries) {
-    final String sqlOutgoing = "select COUNT(1) AS count from sym_outgoing_batch where node_id in (select mobile_server_key from obmobc_server_definition where servertype = 'MAIN') and status not in ('OK', 'IG')";
+  public static void waitForOutgoingSynced(final long interval, final int tries,
+      final ServerType fromServerType, final ServerType toServerType) {
+
+    String toServerKey = "Central";
+    if (toServerType.equals(ServerType.STORE1)) {
+      toServerKey = "Store1";
+    } else if (toServerType.equals(ServerType.STORE2)) {
+      toServerKey = "Store2";
+    } else if (toServerType.equals(ServerType.CENTRAL)) {
+      toServerKey = "Central";
+    } else {
+      throw new IllegalStateException("Server type " + toServerType + " not supported here");
+    }
+    
+    final String sqlOutgoing = "select COUNT(1) AS count from sym_outgoing_batch where node_id = '"
+        + toServerKey + " and status not in ('OK', 'IG')";
 
     new OBWait(interval, (int) tries) {
 
@@ -138,14 +99,14 @@
 
             isServerSynchronized.set(0, isSynchronized);
           }
-        }.execute(sqlOutgoing, 1, ServerType.CENTRAL);
+        }.execute(sqlOutgoing, 1, fromServerType);
         return isServerSynchronized.get(0);
       }
 
       @Override
       protected void timeout() {
-        throw new TimeoutException(String.format(
-                    "The central server has not send the data to the store after %s iterations and %s interval seconds",
+        throw new TimeoutException(String.format("The " + fromServerType.name()
+            + " has not send the data after %s iterations and %s interval seconds",
             tries, interval / 1000));
       }
     };
@@ -218,57 +179,4 @@
         DatabaseHelperInsertUpdateOrDelete.UNKNOWN_AFFECTED_ROWS, ServerType.CENTRAL);
   }
 
-  private static boolean isServerSynchronized(final ServerType serverType) {
-    final String sqlIncomming = "SELECT COUNT(1) FROM (SELECT 1 FROM sym_data d LEFT JOIN sym_data_event de ON d.data_id = de.data_id WHERE de.data_id IS NULL) AS foo";
-    // final String sqlIncomming =
-    // "SELECT CASE WHEN EXISTS (SELECT 1 FROM sym_data d LEFT JOIN sym_data_event de ON d.data_id = de.data_id WHERE de.data_id IS NULL) THEN 'Y' ELSE 'N' END";
-    final String sqlOutgoing = "select COUNT(1) AS count from sym_outgoing_batch where node_id in (select mobile_server_key from obmobc_server_definition where servertype = 'MAIN') and status not in ('OK', 'IG')";
-    final ArrayList<Boolean> isServerSynchronized = new ArrayList<Boolean>();
-    isServerSynchronized.add(false);
-
-    // incoming
-    new DatabaseHelperSelect() {
-      @Override
-      protected void yieldResultSet(final ResultSet rs) throws SQLException {
-        final Long count = rs.getLong("count");
-        final boolean isSynchronized = count == 0L;
-        if (!isSynchronized) {
-          TestLogger
-              .getLogger()
-              .info(
-                  String
-                      .format(
-                          "Waiting for symetricds to synchronize %s. Incoming INSERT, UPDATE or DELETE: %s",
-                          serverType, count));
-        }
-
-        isServerSynchronized.set(0, isSynchronized);
-      }
-    }.execute(sqlIncomming, 1, serverType);
-
-    if (!isServerSynchronized.get(0)) {
-      return isServerSynchronized.get(0);
-    }
-
-    // outgoing
-    new DatabaseHelperSelect() {
-      @Override
-      protected void yieldResultSet(final ResultSet rs) throws SQLException {
-        final Long count = rs.getLong("count");
-        final boolean isSynchronized = count == 0L;
-        if (!isSynchronized) {
-          TestLogger
-              .getLogger()
-              .info(
-                  String
-                      .format(
-                          "Waiting for symetricds to synchronize %s. Outgoing INSERT, UPDATE or DELETE: %s",
-                          serverType, count));
-        }
-
-        isServerSynchronized.set(0, isSynchronized);
-      }
-    }.execute(sqlOutgoing, 1, serverType);
-    return isServerSynchronized.get(0);
-  }
 }
--- a/src-test/org/openbravo/test/mobile/retail/multiserver/selenium/terminals/WebPOSCentralOfflineTerminalHelper.java	Sun Nov 25 20:46:01 2018 +0100
+++ b/src-test/org/openbravo/test/mobile/retail/multiserver/selenium/terminals/WebPOSCentralOfflineTerminalHelper.java	Mon Nov 26 10:34:55 2018 +0100
@@ -58,7 +58,7 @@
 
     TestLogger.getLogger().info("Force transitioning to offline");
 
-    MultiserverUtils.waitForCentralOutgoingSynced(5000, 20);
+    MultiserverUtils.waitForCentralOutgoingSynced();
 
     // switch between modes that forces request
     // works for high volume mode, but not for normal mode