tmf: Fix intermittent fail in ProjectModelOutputTest.testListOutputs
authorMarc-Andre Laperle <marc-andre.laperle@ericsson.com>
Wed, 12 Oct 2016 17:45:33 +0000 (13:45 -0400)
committerMarc-Andre Laperle <marc-andre.laperle@ericsson.com>
Tue, 18 Oct 2016 14:38:23 +0000 (10:38 -0400)
This adds a waitUntil/condition that waits until there is the correct
number of children under the analysis.

To reproduce the issue, in TmfAnalysisElement.refreshChildren, add a
Thread.sleep(1000); at the beginning.

Bug: 491823
Change-Id: Iaae81093d90f26403cc58b7286c8691db7db3644
Signed-off-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/83052
Reviewed-by: Hudson CI
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
Tested-by: Patrick Tasse <patrick.tasse@gmail.com>
tmf/org.eclipse.tracecompass.tmf.ui.tests/shared/org/eclipse/tracecompass/tmf/ui/tests/shared/ProjectModelTestData.java
tmf/org.eclipse.tracecompass.tmf.ui.tests/src/org/eclipse/tracecompass/tmf/ui/tests/project/model/ProjectModelAnalysisTest.java
tmf/org.eclipse.tracecompass.tmf.ui.tests/src/org/eclipse/tracecompass/tmf/ui/tests/project/model/ProjectModelOutputTest.java
tmf/org.eclipse.tracecompass.tmf.ui.tests/src/org/eclipse/tracecompass/tmf/ui/tests/project/model/ProjectModelTraceTest.java
tmf/org.eclipse.tracecompass.tmf.ui.tests/src/org/eclipse/tracecompass/tmf/ui/tests/project/model/TraceAndExperimentTypeTest.java

index a04f31d549f904631e1cc25c2b4baf72e7e0dfe8..ec61a61ac744811eea1780f802c549e18f881131 100644 (file)
@@ -14,7 +14,6 @@ package org.eclipse.tracecompass.tmf.ui.tests.shared;
 
 import java.io.File;
 import java.lang.reflect.InvocationTargetException;
-import java.util.concurrent.TimeoutException;
 
 import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IProject;
@@ -46,11 +45,6 @@ import org.eclipse.ui.actions.WorkspaceModifyOperation;
  */
 public class ProjectModelTestData {
 
-    /* Maximum number of thread delays the main thread will do before timing out */
-    private static final int DELAY_COUNTER = 1000;
-    /* Default delay time when having the main thread sleep. */
-    private static final long DEFAULT_DELAY = 500;
-
     /** Default test project name */
     public static final String PROJECT_NAME = "Test_Project";
 
@@ -238,28 +232,28 @@ public class ProjectModelTestData {
      * longer delays in those cases, it is preferable to use the
      * {@link ProjectModelTestData#delayThread(long)} instead.
      *
-     * Timeout is DELAY_COUNTER * DEFAULT_DELAY ms
-     *
      * @param projectElement
      *            The trace element we are waiting for. If the element if not of
      *            type TmfTraceElement, the thread is delayed only once.
-     * @throws TimeoutException
+     * @throws WaitTimeoutException
      *             If after the maximum number of delays the trace is still
      *             null, we throw a timeout exception, the trace has not opened.
      */
-    public static void delayUntilTraceOpened(final ITmfProjectModelElement projectElement) throws TimeoutException {
+    public static void delayUntilTraceOpened(final ITmfProjectModelElement projectElement) throws WaitTimeoutException {
         if (projectElement instanceof TmfCommonProjectElement) {
             TmfCommonProjectElement traceElement = (TmfCommonProjectElement) projectElement;
-            final long deadline = System.nanoTime() + (DELAY_COUNTER * DEFAULT_DELAY * 1000000L);
-            do {
-                delayThread(DEFAULT_DELAY);
-                if (traceElement.getTrace() != null) {
-                    return;
+            WaitUtils.waitUntil(new IWaitCondition() {
+                @Override
+                public boolean test() throws Exception {
+                    return traceElement.getTrace() != null;
+                }
+
+                @Override
+                public String getFailureMessage() {
+                    return "Timeout while waiting for " + traceElement;
                 }
-            } while (System.nanoTime() < deadline);
-            throw new TimeoutException("Timeout while waiting for " + traceElement);
+            });
         }
-        delayThread(DEFAULT_DELAY);
-    }
 
+    }
 }
index bb907db14cc1e4221b9f05c70802452842f5ae29..a4fc448c01ab5c105f1b85c57802f40de7b4e83c 100644 (file)
@@ -20,7 +20,6 @@ import static org.junit.Assert.fail;
 
 import java.util.List;
 import java.util.Optional;
-import java.util.concurrent.TimeoutException;
 
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
@@ -30,6 +29,7 @@ import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfViewsElement;
 import org.eclipse.tracecompass.tmf.ui.tests.shared.ProjectModelTestData;
+import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitTimeoutException;
 import org.eclipse.tracecompass.tmf.ui.tests.stubs.analysis.TestAnalysisUi;
 import org.junit.After;
 import org.junit.Before;
@@ -147,7 +147,7 @@ public class ProjectModelAnalysisTest {
 
         try {
             ProjectModelTestData.delayUntilTraceOpened(traceElement);
-        } catch (TimeoutException e) {
+        } catch (WaitTimeoutException e) {
             fail("The analysis parent did not open in a reasonable time");
         }
         ITmfTrace trace = traceElement.getTrace();
index 157b07f3375ba14cd6d3c30ef8330f67cad3ae77..f83c4bcaf6b493043cc3326b6338696a1d5a23be 100644 (file)
@@ -18,7 +18,6 @@ import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.util.List;
-import java.util.concurrent.TimeoutException;
 
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.tracecompass.tmf.ui.project.model.ITmfProjectModelElement;
@@ -26,7 +25,10 @@ import org.eclipse.tracecompass.tmf.ui.project.model.TmfAnalysisElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfAnalysisOutputElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
+import org.eclipse.tracecompass.tmf.ui.tests.shared.IWaitCondition;
 import org.eclipse.tracecompass.tmf.ui.tests.shared.ProjectModelTestData;
+import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitTimeoutException;
+import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitUtils;
 import org.eclipse.tracecompass.tmf.ui.tests.stubs.analysis.TestAnalysisUi;
 import org.eclipse.ui.IViewPart;
 import org.eclipse.ui.IWorkbench;
@@ -108,11 +110,12 @@ public class ProjectModelOutputTest {
         analysis.activateParentTrace();
         try {
             ProjectModelTestData.delayUntilTraceOpened(analysis.getParent());
-        } catch (TimeoutException e) {
+        } catch (WaitTimeoutException e) {
             fail("The analysis parent did not open in a reasonable time");
         }
 
         /* Make sure the output list is not empty */
+        WaitUtils.waitUntil(new ConditionTraceChildrenElements(analysis, 1));
         List<TmfAnalysisOutputElement> outputList = analysis.getAvailableOutputs();
         assertFalse(outputList.isEmpty());
         boolean found = false;
@@ -137,10 +140,11 @@ public class ProjectModelOutputTest {
         analysis.activateParentTrace();
         try {
             ProjectModelTestData.delayUntilTraceOpened(analysis.getParent());
-        } catch (TimeoutException e) {
+        } catch (WaitTimeoutException e) {
             fail("The analysis parent did not open in a reasonable time");
         }
 
+        WaitUtils.waitUntil(new ConditionTraceChildrenElements(analysis, 1));
         List<TmfAnalysisOutputElement> outputList = analysis.getAvailableOutputs();
         assertFalse(outputList.isEmpty());
 
@@ -168,4 +172,26 @@ public class ProjectModelOutputTest {
         view = activePage.findView(TestAnalysisUi.VIEW_ID);
         assertNotNull(view);
     }
+
+    private static final class ConditionTraceChildrenElements implements IWaitCondition {
+        private final ITmfProjectModelElement fProjectElement;
+        private int fCurNumChildren;
+        private int fExpectedChildNum;
+
+        private ConditionTraceChildrenElements(ITmfProjectModelElement projectElement, int childNum) {
+            fProjectElement = projectElement;
+            fExpectedChildNum = childNum;
+        }
+
+        @Override
+        public boolean test() throws Exception {
+            fCurNumChildren = fProjectElement.getChildren().size();
+            return fCurNumChildren == fExpectedChildNum;
+        }
+
+        @Override
+        public String getFailureMessage() {
+            return "Timeout while waiting for " + fProjectElement + " to have number of children. Expected: " + fExpectedChildNum + " Actual: " + fCurNumChildren;
+        }
+    }
 }
index 7365e7e265fdaafce8ed259cb0d7d6e8f7d2ac8a..d046185f685526427cbcd37376764c24280e3d70 100644 (file)
@@ -17,8 +17,6 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
-import java.util.concurrent.TimeoutException;
-
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
@@ -26,6 +24,7 @@ import org.eclipse.tracecompass.tmf.ui.project.model.TmfOpenTraceHelper;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
 import org.eclipse.tracecompass.tmf.ui.tests.shared.ProjectModelTestData;
+import org.eclipse.tracecompass.tmf.ui.tests.shared.WaitTimeoutException;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -79,7 +78,7 @@ public class ProjectModelTraceTest {
         /* Give the trace a chance to open */
         try {
             ProjectModelTestData.delayUntilTraceOpened(traceElement);
-        } catch (TimeoutException e) {
+        } catch (WaitTimeoutException e) {
             fail("The trace did not open in a reasonable delay");
         }
 
@@ -93,7 +92,7 @@ public class ProjectModelTraceTest {
         TmfOpenTraceHelper.openTraceFromElement(traceElement);
         try {
             ProjectModelTestData.delayUntilTraceOpened(traceElement);
-        } catch (TimeoutException e) {
+        } catch (WaitTimeoutException e) {
             fail("The trace did not open in a reasonable delay");
         }
 
index 183879349f3a6d50dd30c793e5fc31244cb3ecca..3e168c2e1e2ddb8b549d63a9183d85dc2bfa8d7e 100644 (file)
@@ -18,8 +18,6 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
-import java.util.concurrent.TimeoutException;
-
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
@@ -106,11 +104,7 @@ public class TraceAndExperimentTypeTest {
         }
 
         TmfOpenTraceHelper.openTraceFromElement(fExperiment);
-        try {
-            ProjectModelTestData.delayUntilTraceOpened(fExperiment);
-        } catch (TimeoutException e1) {
-            fail (e1.getMessage());
-        }
+        ProjectModelTestData.delayUntilTraceOpened(fExperiment);
 
         ITmfTrace trace = fExperiment.getTrace();
         assertTrue(trace instanceof TmfExperimentStub);
@@ -124,11 +118,7 @@ public class TraceAndExperimentTypeTest {
     public void testNoExperimentTypeChildren() {
         TmfOpenTraceHelper.openTraceFromElement(fExperiment);
 
-        try {
-            ProjectModelTestData.delayUntilTraceOpened(fExperiment);
-        } catch (TimeoutException e1) {
-            fail (e1.getMessage());
-        }
+        ProjectModelTestData.delayUntilTraceOpened(fExperiment);
 
         final IWorkbench wb = PlatformUI.getWorkbench();
         final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
This page took 0.029516 seconds and 5 git commands to generate.