tmf: Make IAnalysisModule AutoCloseable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.core.tests / src / org / eclipse / linuxtools / lttng2 / kernel / core / tests / stateprovider / StateSystemFullHistoryTest.java
index 8830f809ebad94effac0d80e3fa994d9a69c92af..17863e7c744928684b4a11e77d8951a4d607f679 100644 (file)
 /*******************************************************************************
- * Copyright (c) 2012 Ericsson
- * Copyright (c) 2010, 2011 École Polytechnique de Montréal
- * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
- * 
+ * Copyright (c) 2012, 2014 Ericsson
+ *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
  * accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
- * 
- *******************************************************************************/
+ *
+ * Contributors:
+ *   Alexandre Montplaisir - Initial API and implementation
+ *   Bernd Hufmann - Use state system analysis module instead of factory
+ ******************************************************************************/
 
 package org.eclipse.linuxtools.lttng2.kernel.core.tests.stateprovider;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
 
 import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.List;
 
-import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
-import org.eclipse.linuxtools.tmf.core.statesystem.AttributeNotFoundException;
-import org.eclipse.linuxtools.tmf.core.statesystem.StateHistorySystem;
-import org.eclipse.linuxtools.tmf.core.statesystem.TimeRangeException;
-import org.eclipse.linuxtools.tmf.core.statesystem.backend.historytree.HistoryTreeBackend;
-import org.eclipse.linuxtools.tmf.core.statesystem.helpers.HistoryBuilder;
-import org.eclipse.linuxtools.tmf.core.statesystem.helpers.IStateChangeInput;
-import org.eclipse.linuxtools.tmf.core.statesystem.helpers.IStateHistoryBackend;
-import org.eclipse.linuxtools.tmf.core.statevalue.StateValueTypeException;
-import org.eclipse.linuxtools.internal.lttng2.kernel.core.stateprovider.CTFKernelStateInput;
-import org.junit.*;
+import org.eclipse.linuxtools.internal.lttng2.kernel.core.stateprovider.LttngKernelStateProvider;
+import org.eclipse.linuxtools.statesystem.core.ITmfStateSystem;
+import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException;
+import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateProvider;
+import org.eclipse.linuxtools.tmf.core.statesystem.TmfStateSystemAnalysisModule;
+import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
+import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager;
+import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
 
 /**
- * Unit tests for the StateHistorySystem, which uses a full (non-partial)
- * history and the non-threaded CTF kernel handler.
- * 
- * @author alexmont
- * 
+ * State system tests using a full history back-end and the LTTng kernel state
+ * input.
+ *
+ * @author Alexandre Montplaisir
  */
-@SuppressWarnings("nls")
-public class StateSystemFullHistoryTest {
+public class StateSystemFullHistoryTest extends StateSystemTest {
 
-    protected static File stateFile;
-    protected static File stateFileBenchmark;
+    private static final String TEST_FILE_NAME = "test.ht";
+    private static final String BENCHMARK_FILE_NAME = "test.benchmark.ht";
 
-    protected static HistoryBuilder builder;
-    protected static IStateChangeInput input;
-    protected static IStateHistoryBackend hp;
-    protected static StateHistorySystem shs;
+    private static File stateFile;
+    private static File stateFileBenchmark;
+    private static TestLttngKernelAnalysisModule module;
 
-    protected static String getTestFileName() {
-        return "/tmp/statefile.ht"; //$NON-NLS-1$
-    }
 
+    /**
+     * Initialize the test cases (build the history file once for all tests).
+     */
     @BeforeClass
     public static void initialize() {
-        stateFile = new File(getTestFileName());
-        stateFileBenchmark = new File(getTestFileName() + ".benchmark"); //$NON-NLS-1$
+        assumeTrue(testTrace.exists());
+        stateFile = createStateFile(TEST_FILE_NAME);
+        stateFileBenchmark = createStateFile(BENCHMARK_FILE_NAME);
+
+        module = new TestLttngKernelAnalysisModule(TEST_FILE_NAME);
         try {
-            input = new CTFKernelStateInput(CTFTestFiles.getTestTrace());
-            hp = new HistoryTreeBackend(stateFile, input.getStartTime());
-            builder = new HistoryBuilder(input, hp);
-        } catch (Exception e) {
-            e.printStackTrace();
+            module.setTrace(testTrace.getTrace());
+        } catch (TmfAnalysisException e) {
+            fail();
         }
-        builder.run();
-        shs = (StateHistorySystem) builder.getSS();
+        module.schedule();
+        assertTrue(module.waitForCompletion());
+        ssq = module.getStateSystem();
+
+        assertNotNull(ssq);
     }
 
+    /**
+     * Clean-up
+     */
     @AfterClass
-    public static void cleanup() {
+    public static void tearDownClass() {
+        module.close();
         stateFile.delete();
         stateFileBenchmark.delete();
     }
 
+    // ------------------------------------------------------------------------
+    // Tests specific to a full-history
+    // ------------------------------------------------------------------------
+
     /**
      * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
      * us to @Test the @BeforeClass...
      */
     @Test
     public void testBuild() {
-        HistoryBuilder zebuilder;
-        IStateChangeInput zeinput;
-        IStateHistoryBackend zehp;
-
-        try {
-            zeinput = new CTFKernelStateInput(CTFTestFiles.getTestTrace());
-            zehp = new HistoryTreeBackend(stateFileBenchmark,
-                    zeinput.getStartTime());
-            zebuilder = new HistoryBuilder(zeinput, zehp);
-            zebuilder.run();
-        } catch (Exception e) {
-            e.printStackTrace();
+        try (TestLttngKernelAnalysisModule module2 =
+                new TestLttngKernelAnalysisModule(BENCHMARK_FILE_NAME);) {
+            try {
+                module2.setTrace(testTrace.getTrace());
+            } catch (TmfAnalysisException e) {
+                fail();
+            }
+            module2.schedule();
+            assertTrue(module2.waitForCompletion());
+            ITmfStateSystem ssb2 = module2.getStateSystem();
+
+            assertNotNull(ssb2);
+            assertEquals(startTime, ssb2.getStartTime());
+            assertEquals(endTime, ssb2.getCurrentEndTime());
         }
     }
 
+    /**
+     * Test re-opening the existing file.
+     */
     @Test
     public void testOpenExistingStateFile() {
-        IStateHistoryBackend hp2 = null;
-        StateHistorySystem shs2 = null;
-        try {
-            /* 'newStateFile' should have already been created */
-            hp2 = new HistoryTreeBackend(stateFile);
-            shs2 = new StateHistorySystem(hp2, false);
-        } catch (IOException e) {
-            e.printStackTrace();
+        /* 'newStateFile' should have already been created */
+        try (TestLttngKernelAnalysisModule module2 = new TestLttngKernelAnalysisModule(TEST_FILE_NAME);) {
+            try {
+                module2.setTrace(testTrace.getTrace());
+            } catch (TmfAnalysisException e) {
+                fail();
+            }
+            module2.schedule();
+            assertTrue(module2.waitForCompletion());
+            ITmfStateSystem ssb2 = module2.getStateSystem();
+
+            assertNotNull(ssb2);
+            assertEquals(startTime, ssb2.getStartTime());
+            assertEquals(endTime, ssb2.getCurrentEndTime());
         }
-        assertTrue(shs2 != null);
-    }
-
-    @Test
-    public void testFullQuery1() throws StateValueTypeException,
-            AttributeNotFoundException, TimeRangeException {
-
-        ITmfStateInterval interval;
-        int quark, valueInt;
-        String valueStr;
-
-        shs.loadStateAtTime(17622841472359L);
-
-        quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        interval = shs.queryState(quark);
-        valueInt = interval.getStateValue().unboxInt();
-        assertTrue(valueInt == 1929);
-
-        quark = shs.getQuarkAbsolute("Threads", "1197", "Exec_name");
-        interval = shs.queryState(quark);
-        valueStr = interval.getStateValue().unboxStr();
-        assertTrue(valueStr.equals("apache2"));
-
-        // FIXME fails at the moment (attribute type is int, and = 3129??), I'll
-        // figure it out later
-        // quark = shs.getQuarkAbsolute("Threads", "3109", "Exec_mode_stack");
-        // interval = shs.getState(quark);
-        // valueStr = interval.getStateValue().unboxStr();
-        // assertTrue( valueStr.equals("bloup") );
-    }
-
-    @Test
-    public void testFullQuery2() {
-        //
-    }
-
-    @Test
-    public void testFullQuery3() {
-        //
-    }
-
-    @Test
-    public void testSingleQuery1() throws AttributeNotFoundException,
-            TimeRangeException, StateValueTypeException {
-
-        long timestamp = 17622841472359L;
-        int quark;
-        ITmfStateInterval interval;
-        String valueStr;
-
-        quark = shs.getQuarkAbsolute("Threads", "1197", "Exec_name");
-        interval = shs.querySingleState(timestamp, quark);
-        valueStr = interval.getStateValue().unboxStr();
-        assertTrue(valueStr.equals("apache2"));
-    }
-
-    @Test
-    public void testSingleQuery2() {
-        //
-    }
-
-    @Test
-    public void testSingleQuery3() {
-        //
     }
 
-    @Test
-    public void testRangeQuery1() throws AttributeNotFoundException,
-            TimeRangeException, StateValueTypeException {
+    private static class TestLttngKernelAnalysisModule extends TmfStateSystemAnalysisModule {
 
-        long time1 = 17622841472359L;
-        long time2 = time1 + 1L * CTFTestFiles.NANOSECS_PER_SEC;
-        int quark;
-        List<ITmfStateInterval> intervals;
-
-        quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        intervals = shs.queryHistoryRange(quark, time1, time2);
-        assertTrue(intervals.size() == 1018); /* Number of context switches! */
-        assertTrue(intervals.get(100).getStateValue().unboxInt() == 2974);
-        assertTrue(intervals.get(205).getEndTime() == 17622977386059L);
-    }
-
-    /**
-     * Ask for a time range outside of the trace's range
-     * 
-     * @throws TimeRangeException
-     */
-    @Test(expected = TimeRangeException.class)
-    public void testFullQueryInvalidTime1() throws TimeRangeException {
-        shs.loadStateAtTime(CTFTestFiles.startTime1 + 20L
-                * CTFTestFiles.NANOSECS_PER_SEC);
+        private final String htFileName;
 
-    }
-
-    @Test(expected = TimeRangeException.class)
-    public void testFullQueryInvalidTime2() throws TimeRangeException {
-        shs.loadStateAtTime(CTFTestFiles.startTime1 - 20L
-                * CTFTestFiles.NANOSECS_PER_SEC);
-
-    }
-
-    @Test(expected = TimeRangeException.class)
-    public void testSingleQueryInvalidTime1()
-            throws AttributeNotFoundException, TimeRangeException {
-
-        int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        long time = CTFTestFiles.startTime1 + 20L
-                * CTFTestFiles.NANOSECS_PER_SEC;
-        shs.querySingleState(time, quark);
-    }
-
-    @Test(expected = TimeRangeException.class)
-    public void testSingleQueryInvalidTime2()
-            throws AttributeNotFoundException, TimeRangeException {
-
-        int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        long time = CTFTestFiles.startTime1 - 20L
-                * CTFTestFiles.NANOSECS_PER_SEC;
-        shs.querySingleState(time, quark);
-    }
-
-    @Test(expected = TimeRangeException.class)
-    public void testRangeQueryInvalidTime1() throws AttributeNotFoundException,
-            TimeRangeException {
-
-        int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        long time1 = CTFTestFiles.startTime1 - 20L
-                * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
-        long time2 = CTFTestFiles.startTime1 + 1L
-                * CTFTestFiles.NANOSECS_PER_SEC; /* valid */
-
-        shs.queryHistoryRange(quark, time1, time2);
-    }
-
-    @Test(expected = TimeRangeException.class)
-    public void testRangeQueryInvalidTime2() throws TimeRangeException,
-            AttributeNotFoundException {
-
-        int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        long time1 = CTFTestFiles.startTime1 + 1L
-                * CTFTestFiles.NANOSECS_PER_SEC; /* valid */
-        long time2 = CTFTestFiles.startTime1 + 20L
-                * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
-
-        shs.queryHistoryRange(quark, time1, time2);
-    }
-
-    @Test(expected = TimeRangeException.class)
-    public void testRangeQueryInvalidTime3() throws TimeRangeException,
-            AttributeNotFoundException {
-
-        int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        long time1 = CTFTestFiles.startTime1 - 1L
-                * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
-        long time2 = CTFTestFiles.startTime1 + 20L
-                * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
-
-        shs.queryHistoryRange(quark, time1, time2);
-    }
-
-    /**
-     * Ask for a non-existing attribute
-     * 
-     * @throws AttributeNotFoundException
-     */
-    @Test(expected = AttributeNotFoundException.class)
-    public void testQueryInvalidAttribute() throws AttributeNotFoundException {
-
-        shs.getQuarkAbsolute("There", "is", "no", "cow", "level");
-    }
-
-    /**
-     * Query but with the wrong State Value type
-     * 
-     * @throws StateValueTypeException
-     * @throws AttributeNotFoundException
-     * @throws TimeRangeException
-     */
-    @Test(expected = StateValueTypeException.class)
-    public void testQueryInvalidValuetype1() throws StateValueTypeException,
-            AttributeNotFoundException, TimeRangeException {
-
-        ITmfStateInterval interval;
-        int quark;
-
-        shs.loadStateAtTime(17622841472359L);
-
-        quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        interval = shs.queryState(quark);
-        interval.getStateValue().unboxStr(); /*
-                                              * This is supposed to be a int
-                                              * value
-                                              */
-    }
+        /**
+         * Constructor adding the views to the analysis
+         * @param htFileName
+         *      The History File Name
+         */
+        public TestLttngKernelAnalysisModule(String htFileName) {
+            super();
+            this.htFileName = htFileName;
+        }
 
-    @Test(expected = StateValueTypeException.class)
-    public void testQueryInvalidValuetype2() throws StateValueTypeException,
-            AttributeNotFoundException, TimeRangeException {
+        @Override
+        public void setTrace(ITmfTrace trace) throws TmfAnalysisException {
+            if (!(trace instanceof CtfTmfTrace)) {
+                throw new IllegalStateException("TestLttngKernelAnalysisModule: trace should be of type CtfTmfTrace"); //$NON-NLS-1$
+            }
+            super.setTrace(trace);
+        }
 
-        ITmfStateInterval interval;
-        int quark;
+        @Override
+        protected ITmfStateProvider createStateProvider() {
+            return new LttngKernelStateProvider(getTrace());
+        }
 
-        shs.loadStateAtTime(17622841472359L);
+        @Override
+        protected StateSystemBackendType getBackendType() {
+            return StateSystemBackendType.FULL;
+        }
 
-        quark = shs.getQuarkAbsolute("Threads", "1197", "Exec_name");
-        interval = shs.queryState(quark);
-        interval.getStateValue().unboxInt(); /*
-                                              * This is supposed to be a String
-                                              * value
-                                              */
+        @Override
+        protected String getSsFileName() {
+            return htFileName;
+        }
     }
 
-    @Test
-    public void testFullAttributeName() throws AttributeNotFoundException {
-        int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
-        String name = shs.getFullAttributePath(quark);
-        assertTrue(name.equals("CPUs/0/Current_thread"));
+    private static File createStateFile(String name) {
+        File file = new File(TmfTraceManager.getSupplementaryFileDir(testTrace.getTrace()) + name);
+        if (file.exists()) {
+            file.delete();
+        }
+        return file;
     }
 
-    @Test
-    public void testDebugPrinting() throws FileNotFoundException {
-        shs.debugPrint(new PrintWriter(new File("/dev/null")));
-    }
 }
This page took 0.030248 seconds and 5 git commands to generate.