ss: Extract an history tree interface
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core.tests / src / org / eclipse / tracecompass / statesystem / core / tests / backend / historytree / HistoryTreeWithBackendTest.java
1 /*******************************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9
10 package org.eclipse.tracecompass.statesystem.core.tests.backend.historytree;
11
12 import static org.junit.Assert.fail;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.util.Arrays;
17
18 import org.eclipse.tracecompass.common.core.NonNullUtils;
19 import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HTInterval;
20 import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HTNode;
21 import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.HistoryTreeBackend;
22 import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.IHistoryTree;
23 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
24 import org.eclipse.tracecompass.statesystem.core.tests.stubs.backend.HistoryTreeBackendStub;
25 import org.eclipse.tracecompass.statesystem.core.tests.stubs.backend.HistoryTreeBackendStub.HistoryTreeType;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.Parameterized;
29 import org.junit.runners.Parameterized.Parameters;
30
31 /**
32 * Test the {@link HistoryTreeBackend}-specific behavior and its interactions
33 * with the {@link IHistoryTree} classes.
34 *
35 * @author Geneviève Bastien
36 */
37 @RunWith(Parameterized.class)
38 public class HistoryTreeWithBackendTest {
39
40 /** State system ID */
41 private static final String SSID = "test";
42 /** Provider version */
43 private static final int PROVIDER_VERSION = 0;
44
45 /** Default maximum number of children nodes */
46 private static final int MAX_CHILDREN = 3;
47 /** Default block size */
48 private static final int BLOCK_SIZE = 4096;
49
50 private final HistoryTreeType fHtType;
51
52 /**
53 * @return The arrays of parameters
54 */
55 @Parameters(name = "{0}")
56 public static Iterable<Object[]> getParameters() {
57 return Arrays.asList(new Object[][] {
58 { HistoryTreeType.CLASSIC }
59 });
60 }
61
62 /**
63 * Constructor
64 *
65 * @param htType
66 * The type of history tree to use
67 */
68 public HistoryTreeWithBackendTest(HistoryTreeType htType) {
69 fHtType = htType;
70 }
71
72 /**
73 * Test the behavior of the history tree after at least a depth of 3
74 */
75 @Test
76 public void testFillNodes() {
77 try {
78 // Test case parameters
79 final int nbAttr = 5;
80 final int depthToRead = 3;
81
82 long startTime = 1;
83 File historyTreeFile = NonNullUtils.checkNotNull(File.createTempFile("HistoryTreeBackendTest", ".ht"));
84 HistoryTreeBackendStub.setTreeType(fHtType);
85 HistoryTreeBackendStub backend = new HistoryTreeBackendStub(SSID, historyTreeFile, PROVIDER_VERSION, startTime, BLOCK_SIZE, MAX_CHILDREN);
86
87 int duration = nbAttr;
88 int quarkTest = nbAttr;
89 long time = startTime + duration;
90
91 HTInterval interval = new HTInterval(startTime, time, quarkTest, TmfStateValue.newValueLong(time));
92 // Insert a first interval for the test attribute
93 backend.insertPastState(interval.getStartTime(), interval.getEndTime(), interval.getAttribute(), interval.getStateValue());
94
95 /*
96 * insert cascading intervals to fill 2 levels of history tree, so
97 * that we start another branch
98 */
99 while (backend.getHistoryTree().getDepth() < depthToRead) {
100 backend.insertPastState(
101 Math.max(startTime, time - duration),
102 time - 1,
103 (int) time % nbAttr,
104 TmfStateValue.newValueLong(time));
105 time++;
106 }
107
108 // entirely fill the latest leaf with cascading intervals
109 HTNode latestLeaf = backend.getHistoryTree().getLatestLeaf();
110 /*
111 * Add an interval while there is still room for it or make sure the
112 * node does not get written on disk in the meantime.
113 */
114 while (interval.getSizeOnDisk() <= latestLeaf.getNodeFreeSpace() || latestLeaf.isOnDisk()) {
115 backend.insertPastState(
116 Math.max(startTime, time - duration),
117 time - 1,
118 (int) time % nbAttr,
119 TmfStateValue.newValueLong(time));
120 time++;
121 }
122
123 // Add an interval that does not fit in latest leaf, but starts
124 // before the current branch
125 backend.insertPastState(interval.getEndTime() + 1, time, quarkTest, TmfStateValue.newValueLong(time));
126
127 backend.getHistoryTree().assertIntegrity();
128
129 } catch (IOException e) {
130 fail(e.getMessage());
131 }
132 }
133
134 }
This page took 0.034406 seconds and 5 git commands to generate.