tmf: Add an attribute pool for state system analyses
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / statesystem / AttributePoolTest.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.tmf.core.tests.statesystem;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.fail;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
19 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
20 import org.eclipse.tracecompass.statesystem.core.StateSystemFactory;
21 import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend;
22 import org.eclipse.tracecompass.statesystem.core.backend.StateHistoryBackendFactory;
23 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
24 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
25 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
26 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
27 import org.eclipse.tracecompass.tmf.core.statesystem.TmfAttributePool;
28 import org.eclipse.tracecompass.tmf.core.statesystem.TmfAttributePool.QueueType;
29 import org.junit.After;
30 import org.junit.Test;
31
32 /**
33 * Test the {@link TmfAttributePool} class
34 *
35 * @author Geneviève Bastien
36 */
37 @NonNullByDefault
38 public class AttributePoolTest {
39
40 private static final long START_TIME = 1000L;
41 private static final String DUMMY_STRING = "test";
42 private static final ITmfStateValue VALUE = TmfStateValue.newValueInt(2);
43
44 private ITmfStateSystemBuilder fStateSystem;
45
46 /**
47 * Initialize the state system
48 */
49 public AttributePoolTest() {
50 IStateHistoryBackend backend = StateHistoryBackendFactory.createInMemoryBackend(DUMMY_STRING, START_TIME);
51 fStateSystem = StateSystemFactory.newStateSystem(backend);
52 }
53
54 /**
55 * Clean-up
56 */
57 @After
58 public void tearDown() {
59 fStateSystem.dispose();
60 }
61
62 /**
63 * Test the constructor of the attribute pool
64 */
65 @Test
66 public void testConstructorGood() {
67 // Create an attribute pool with ROOT_QUARK as base
68 TmfAttributePool pool = new TmfAttributePool(fStateSystem, ITmfStateSystem.ROOT_ATTRIBUTE);
69 assertNotNull(pool);
70
71 int quark = fStateSystem.getQuarkAbsoluteAndAdd(DUMMY_STRING);
72 pool = new TmfAttributePool(fStateSystem, quark);
73 }
74
75 /**
76 * Test the constructor with an invalid attribute
77 */
78 @Test(expected = IllegalArgumentException.class)
79 public void testConstructorBad() {
80 new TmfAttributePool(fStateSystem, ITmfStateSystem.INVALID_ATTRIBUTE);
81 }
82
83 /**
84 * Test the constructor with an invalid positive attribute
85 */
86 @Test(expected = IllegalArgumentException.class)
87 public void testConstructorBad2() {
88 new TmfAttributePool(fStateSystem, 3);
89 }
90
91 /**
92 * Test attributes using only one level of children
93 */
94 @Test
95 public void testSimplePool() {
96 TmfAttributePool pool = new TmfAttributePool(fStateSystem, ITmfStateSystem.ROOT_ATTRIBUTE);
97 assertNotNull(pool);
98
99 /* Get some attributes */
100 Integer available = pool.getAvailable();
101 assertNotNull(available);
102 Integer available2 = pool.getAvailable();
103 assertNotNull(available2);
104 assertNotEquals(available, available2);
105
106 /* Verify the names of the attributes */
107 assertEquals("0", fStateSystem.getAttributeName(available));
108 assertEquals("1", fStateSystem.getAttributeName(available2));
109
110 /* Modify them */
111 try {
112 fStateSystem.modifyAttribute(START_TIME + 10, VALUE, available);
113 fStateSystem.modifyAttribute(START_TIME + 10, VALUE, available2);
114 } catch (StateValueTypeException | AttributeNotFoundException e) {
115 fail(e.getMessage());
116 }
117
118 /* Recycle one and make sure it is set to null */
119 pool.recycle(available, START_TIME + 20);
120 try {
121 ITmfStateValue value = fStateSystem.queryOngoingState(available);
122 assertEquals(TmfStateValue.nullValue(), value);
123 } catch (AttributeNotFoundException e) {
124 fail(e.getMessage());
125 }
126
127 /* Get a new one and make sure it is reusing the one just recycled */
128 Integer available3 = pool.getAvailable();
129 assertEquals(available, available3);
130
131 /* Get a new attribute and make sure it is different from both other used attributes */
132 Integer available4 = pool.getAvailable();
133 assertNotEquals(available3, available4);
134 assertNotEquals(available2, available4);
135
136 /* Recycle available attributes, in reverse order and see how they are returned */
137 pool.recycle(available4, START_TIME + 30);
138 pool.recycle(available2, START_TIME + 30);
139 pool.recycle(available3, START_TIME + 30);
140
141 Integer available5 = pool.getAvailable();
142 assertEquals(available4, available5);
143
144 Integer available6 = pool.getAvailable();
145 assertEquals(available2, available6);
146
147 Integer available7 = pool.getAvailable();
148 assertEquals(available3, available7);
149 }
150
151 /**
152 * Test attributes with sub-trees
153 */
154 @Test
155 public void testPoolWithSubTree() {
156 TmfAttributePool pool = new TmfAttributePool(fStateSystem, ITmfStateSystem.ROOT_ATTRIBUTE);
157 assertNotNull(pool);
158
159 /* Get some attributes */
160 Integer available = pool.getAvailable();
161 assertNotNull(available);
162
163 /* Add children and set values for them */
164 try {
165 Integer child1 = fStateSystem.getQuarkRelativeAndAdd(available, "child1");
166 Integer child2 = fStateSystem.getQuarkRelativeAndAdd(available, "child2");
167 fStateSystem.modifyAttribute(START_TIME + 10, VALUE, available);
168 fStateSystem.modifyAttribute(START_TIME + 10, VALUE, child1);
169 fStateSystem.modifyAttribute(START_TIME + 10, VALUE, child2);
170
171 pool.recycle(available, START_TIME + 20);
172
173 ITmfStateValue value = fStateSystem.queryOngoingState(available);
174 assertEquals(TmfStateValue.nullValue(), value);
175 value = fStateSystem.queryOngoingState(child1);
176 assertEquals(TmfStateValue.nullValue(), value);
177 value = fStateSystem.queryOngoingState(child2);
178 assertEquals(TmfStateValue.nullValue(), value);
179 } catch (StateValueTypeException | AttributeNotFoundException e) {
180 fail(e.getMessage());
181 }
182 }
183
184 /**
185 * Test pool with priority queue
186 */
187 @Test
188 public void testPriorityPool() {
189 TmfAttributePool pool = new TmfAttributePool(fStateSystem, ITmfStateSystem.ROOT_ATTRIBUTE, QueueType.PRIORITY);
190 assertNotNull(pool);
191
192 /* Get some attributes */
193 Integer available = pool.getAvailable();
194 assertNotNull(available);
195 Integer available2 = pool.getAvailable();
196 assertNotNull(available2);
197 assertNotEquals(available, available2);
198
199 /* Verify the names of the attributes */
200 assertEquals("0", fStateSystem.getAttributeName(available));
201 assertEquals("1", fStateSystem.getAttributeName(available2));
202
203 /* Recycle on */
204 pool.recycle(available, START_TIME + 20);
205
206 /* Get a new one and make sure it is reusing the one just recycled */
207 Integer available3 = pool.getAvailable();
208 assertEquals(available, available3);
209
210 /* Get a new attribute and make sure it is different from both other used attributes */
211 Integer available4 = pool.getAvailable();
212 assertNotEquals(available3, available4);
213 assertNotEquals(available2, available4);
214
215 /* Recycle available attributes, in reverse order and see how they are returned */
216 pool.recycle(available4, START_TIME + 30);
217 pool.recycle(available2, START_TIME + 30);
218 pool.recycle(available3, START_TIME + 30);
219
220 Integer available5 = pool.getAvailable();
221 assertEquals(available3, available5);
222
223 Integer available6 = pool.getAvailable();
224 assertEquals(available2, available6);
225
226 Integer available7 = pool.getAvailable();
227 assertEquals(available4, available7);
228 }
229
230 /**
231 * Test recycling the root attribute
232 */
233 @Test(expected = IllegalArgumentException.class)
234 public void testRecycleWrongQuark() {
235 TmfAttributePool pool = new TmfAttributePool(fStateSystem, ITmfStateSystem.ROOT_ATTRIBUTE);
236 assertNotNull(pool);
237
238 pool.recycle(ITmfStateSystem.ROOT_ATTRIBUTE, START_TIME + 10);
239 }
240
241 /**
242 * Test recycling one of the children
243 */
244 @Test(expected = IllegalArgumentException.class)
245 public void testRecycleChildQuark() {
246 TmfAttributePool pool = new TmfAttributePool(fStateSystem, ITmfStateSystem.ROOT_ATTRIBUTE);
247 assertNotNull(pool);
248
249 /* Get some attributes */
250 Integer available = pool.getAvailable();
251 assertNotNull(available);
252
253 /* Add children and set values for them */
254 try {
255 Integer child1 = fStateSystem.getQuarkRelativeAndAdd(available, "child1");
256 pool.recycle(child1, START_TIME + 10);
257 } catch (StateValueTypeException e) {
258 fail(e.getMessage());
259 }
260 }
261 }
This page took 0.053713 seconds and 6 git commands to generate.