tmf: Add a waitForInitialization() to the state system module
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Fri, 24 Jan 2014 21:06:28 +0000 (16:06 -0500)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Mon, 27 Jan 2014 16:27:53 +0000 (11:27 -0500)
With this, views and other components can be notified when the
underlying state system is instantiated and ready to be queried.

Change-Id: I0802d95b3311b1124803b55b3c5dd617e26afa48
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/21069
Tested-by: Hudson CI
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
IP-Clean: Patrick Tasse <patrick.tasse@gmail.com>
Tested-by: Patrick Tasse <patrick.tasse@gmail.com>
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/statesystem/StateSystemAnalysisModuleTest.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/TmfStateSystemAnalysisModule.java

index cea19e4c9264e7159628a66dae4b9f0aebb3347f..29684b8d45bf44c52795a4285454659415dd7b48 100644 (file)
@@ -44,14 +44,18 @@ public class StateSystemAnalysisModuleTest {
     /** ID of the test state system analysis module */
     public static final String MODULE_SS = "org.eclipse.linuxtools.tmf.core.tests.analysis.sstest";
 
-    private TmfTraceStub fTrace;
+    private TmfStateSystemAnalysisModule module;
 
     /**
      * Setup test trace
      */
     @Before
     public void setupTraces() {
-        fTrace = (TmfTraceStub) TmfTestTrace.A_TEST_10K.getTrace();
+        TmfTraceStub trace = (TmfTraceStub) TmfTestTrace.A_TEST_10K.getTrace();
+        TmfSignalManager.deregister(trace);
+        trace.traceOpened(new TmfTraceOpenedSignal(this, trace, null));
+
+        module = (TmfStateSystemAnalysisModule) trace.getAnalysisModule(MODULE_SS);
     }
 
     /**
@@ -67,12 +71,7 @@ public class StateSystemAnalysisModuleTest {
      */
     @Test
     public void testSsModule() {
-        TmfSignalManager.deregister(fTrace);
-        fTrace.traceOpened(new TmfTraceOpenedSignal(this, fTrace, null));
-
-        TmfStateSystemAnalysisModule module = (TmfStateSystemAnalysisModule) fTrace.getAnalysisModule(MODULE_SS);
-        ITmfStateSystem ss = null;
-        ss = module.getStateSystem();
+        ITmfStateSystem ss = module.getStateSystem();
         assertNull(ss);
         module.schedule();
         if (module.waitForCompletion(new NullProgressMonitor())) {
@@ -83,4 +82,17 @@ public class StateSystemAnalysisModuleTest {
         }
     }
 
+    /**
+     * Make sure that the state system is initialized after callingĀ 
+     * {@link TmfStateSystemAnalysisModule#waitForInitialization()}.
+     */
+    @Test
+    public void testInitialization() {
+        assertNull(module.getStateSystem());
+        module.schedule();
+
+        module.waitForInitialization();
+        assertNotNull(module.getStateSystem());
+    }
+
 }
index f1cdb21399afb833761c611030ec53a2b4e5d941..ec784190a1c83b8cbc5b85e77213ade14280b1e2 100644 (file)
@@ -16,6 +16,7 @@ package org.eclipse.linuxtools.tmf.core.statesystem;
 import java.io.File;
 import java.io.IOException;
 import java.util.Collections;
+import java.util.concurrent.CountDownLatch;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.NullProgressMonitor;
@@ -57,6 +58,8 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
 
     private static final String EXTENSION = ".ht"; //$NON-NLS-1$
 
+    private final CountDownLatch fInitialized = new CountDownLatch(1);
+
     @Nullable private ITmfStateSystemBuilder fStateSystem;
     @Nullable private ITmfStateProvider fStateProvider;
     @Nullable private IStateHistoryBackend fHtBackend;
@@ -113,6 +116,17 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
         return fStateSystem;
     }
 
+    /**
+     * Block the calling thread until the analysis module has been initialized.
+     * After this method returns, {@link #getStateSystem()} should not return
+     * null anymore.
+     */
+    public void waitForInitialization() {
+        try {
+            fInitialized.await();
+        } catch (InterruptedException e) {}
+    }
+
     // ------------------------------------------------------------------------
     // TmfAbstractAnalysisModule
     // ------------------------------------------------------------------------
@@ -183,6 +197,7 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
             try {
                 fHtBackend = new HistoryTreeBackend(htFile, version);
                 fStateSystem = new StateSystem(fHtBackend, false);
+                fInitialized.countDown();
                 return;
             } catch (IOException e) {
                 /*
@@ -338,8 +353,18 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
         fStateProvider = provider;
         fRequest = request;
 
+        /*
+         * The state system object is now created, we can consider this module
+         * "initialized" (components can retrieve it and start doing queries).
+         */
+        fInitialized.countDown();
+
+        /*
+         * Block the executeAnalysis() construction is complete (so that the
+         * progress monitor displays that it is running).
+         */
         try {
-             fRequest.waitForCompletion();
+             request.waitForCompletion();
         } catch (InterruptedException e) {
              e.printStackTrace();
         }
This page took 0.027671 seconds and 5 git commands to generate.