callstack: Fix the FlameGraphTest

Some concurrent build threads would be run in any order and result may
have been to display the callgraph of another test. This fixes the
concurrency issues by resetting all values at the end of the test to
make sure there's no pollution between tests.

Change-Id: Ie7030966e157c6a2b984f490470c63f3e4d2a967
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/156103
Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org>
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
diff --git a/callstack/org.eclipse.tracecompass.incubator.callstack.core.tests/src/org/eclipse/tracecompass/incubator/callstack/core/tests/callgraph/instrumented/AggregationTreeTest.java b/callstack/org.eclipse.tracecompass.incubator.callstack.core.tests/src/org/eclipse/tracecompass/incubator/callstack/core/tests/callgraph/instrumented/AggregationTreeTest.java
index 390e8de..60e6996 100644
--- a/callstack/org.eclipse.tracecompass.incubator.callstack.core.tests/src/org/eclipse/tracecompass/incubator/callstack/core/tests/callgraph/instrumented/AggregationTreeTest.java
+++ b/callstack/org.eclipse.tracecompass.incubator.callstack.core.tests/src/org/eclipse/tracecompass/incubator/callstack/core/tests/callgraph/instrumented/AggregationTreeTest.java
@@ -37,6 +37,7 @@
 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
 import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
+import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal;
 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
@@ -128,6 +129,7 @@
         }
         ITmfTrace trace = fTrace;
         if (trace != null) {
+            TmfTraceManager.getInstance().traceClosed(new TmfTraceClosedSignal(this, trace));
             trace.dispose();
         }
     }
diff --git a/callstack/org.eclipse.tracecompass.incubator.callstack.core/src/org/eclipse/tracecompass/incubator/internal/callstack/core/flamegraph/FlameGraphDataProviderFactory.java b/callstack/org.eclipse.tracecompass.incubator.callstack.core/src/org/eclipse/tracecompass/incubator/internal/callstack/core/flamegraph/FlameGraphDataProviderFactory.java
index 401da11..bec14b1 100644
--- a/callstack/org.eclipse.tracecompass.incubator.callstack.core/src/org/eclipse/tracecompass/incubator/internal/callstack/core/flamegraph/FlameGraphDataProviderFactory.java
+++ b/callstack/org.eclipse.tracecompass.incubator.callstack.core/src/org/eclipse/tracecompass/incubator/internal/callstack/core/flamegraph/FlameGraphDataProviderFactory.java
@@ -93,12 +93,17 @@
      * fixtures of data are used without a trace
      *
      * @param id
-     *            ID of the data provider
+     *            ID of the data provider. A <code>null</code> value will remove
+     *            the data provider from the instance list
      * @param dataProvider
      *            The data provider
      */
     @VisibleForTesting
-    public static void registerDataProviderWithId(String id, FlameGraphDataProvider<?, ?, ?> dataProvider) {
+    public static void registerDataProviderWithId(String id, @Nullable FlameGraphDataProvider<?, ?, ?> dataProvider) {
+        if (dataProvider == null) {
+            INSTANCES.remove(id);
+            return;
+        }
         INSTANCES.put(id, dataProvider);
     }
 
diff --git a/callstack/org.eclipse.tracecompass.incubator.callstack.ui.swtbot.tests/src/org/eclipse/tracecompass/incubator/callstack/core/ui/swtbot/tests/flamegraph/FlameGraphTest.java b/callstack/org.eclipse.tracecompass.incubator.callstack.ui.swtbot.tests/src/org/eclipse/tracecompass/incubator/callstack/core/ui/swtbot/tests/flamegraph/FlameGraphTest.java
index ba7d2de..7f07194 100644
--- a/callstack/org.eclipse.tracecompass.incubator.callstack.ui.swtbot.tests/src/org/eclipse/tracecompass/incubator/callstack/core/ui/swtbot/tests/flamegraph/FlameGraphTest.java
+++ b/callstack/org.eclipse.tracecompass.incubator.callstack.ui.swtbot.tests/src/org/eclipse/tracecompass/incubator/callstack/core/ui/swtbot/tests/flamegraph/FlameGraphTest.java
@@ -25,7 +25,6 @@
 import org.apache.log4j.Logger;
 import org.apache.log4j.SimpleLayout;
 import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.swt.widgets.Display;
 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
@@ -37,7 +36,6 @@
 import org.eclipse.tracecompass.incubator.internal.callstack.core.flamegraph.FlameGraphDataProvider;
 import org.eclipse.tracecompass.incubator.internal.callstack.core.flamegraph.FlameGraphDataProviderFactory;
 import org.eclipse.tracecompass.incubator.internal.callstack.ui.flamegraph.FlameGraphView;
-import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers.SWTBotTestCondition;
@@ -132,7 +130,10 @@
         if (fg != null) {
             SWTBotUtils.maximize(fg);
         }
-        // Setting the input to null so the view can be emptied, to avoid race conditions with subsequent tests
+        // Reset the data provider so old data is not used to build view
+        FlameGraphDataProviderFactory.registerDataProviderWithId(SECONDARY_ID, null);
+        // Setting the input to null so the view can be emptied, to avoid race
+        // conditions with subsequent tests
         TimeGraphViewer tg = fTimeGraphViewer;
         if (tg != null) {
             UIThreadRunnable.syncExec(() -> {
@@ -158,7 +159,8 @@
     private void loadFlameGraph() {
         CallGraphAnalysisStub cga = Objects.requireNonNull(getCga());
         ITmfTrace trace = getTrace();
-        Display.getDefault().syncExec(() -> fFg.traceSelected(new TmfTraceSelectedSignal(this, trace)));
+        fFg.setTrace(trace);
+        // Add the new provider and rebuild the view
         FlameGraphDataProvider<?, ?, ?> dp = new FlameGraphDataProvider<>(trace, cga, FlameGraphDataProvider.ID + ':' + SECONDARY_ID);
         FlameGraphDataProviderFactory.registerDataProviderWithId(SECONDARY_ID, dp);
         UIThreadRunnable.syncExec(() -> fFg.buildFlameGraph(trace, null, null));
diff --git a/callstack/org.eclipse.tracecompass.incubator.callstack.ui/src/org/eclipse/tracecompass/incubator/internal/callstack/ui/flamegraph/FlameGraphView.java b/callstack/org.eclipse.tracecompass.incubator.callstack.ui/src/org/eclipse/tracecompass/incubator/internal/callstack/ui/flamegraph/FlameGraphView.java
index 8597032..b1fac22 100644
--- a/callstack/org.eclipse.tracecompass.incubator.callstack.ui/src/org/eclipse/tracecompass/incubator/internal/callstack/ui/flamegraph/FlameGraphView.java
+++ b/callstack/org.eclipse.tracecompass.incubator.callstack.ui/src/org/eclipse/tracecompass/incubator/internal/callstack/ui/flamegraph/FlameGraphView.java
@@ -1196,6 +1196,19 @@
     }
 
     /**
+     * Set the current trace of this view. This should be called only for
+     * testing purposes, otherwise, the normal
+     * {@link #traceSelected(TmfTraceSelectedSignal)} should be used.
+     *
+     * @param trace
+     *            The trace to set
+     */
+    @VisibleForTesting
+    public void setTrace(ITmfTrace trace) {
+        fTrace = trace;
+    }
+
+    /**
      * Trace is closed: clear the data structures and the view
      *
      * @param signal