1 /*******************************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.statesystem
;
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
;
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
.StateValueTypeException
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfAttributePool
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfAttributePool
.QueueType
;
28 import org
.junit
.After
;
29 import org
.junit
.Test
;
32 * Test the {@link TmfAttributePool} class
34 * @author Geneviève Bastien
37 public class AttributePoolTest
{
39 private static final long START_TIME
= 1000L;
40 private static final String DUMMY_STRING
= "test";
41 private static final ITmfStateValue VALUE
= TmfStateValue
.newValueInt(2);
43 private ITmfStateSystemBuilder fStateSystem
;
46 * Initialize the state system
48 public AttributePoolTest() {
49 IStateHistoryBackend backend
= StateHistoryBackendFactory
.createInMemoryBackend(DUMMY_STRING
, START_TIME
);
50 fStateSystem
= StateSystemFactory
.newStateSystem(backend
);
57 public void tearDown() {
58 fStateSystem
.dispose();
62 * Test the constructor of the attribute pool
65 public void testConstructorGood() {
66 // Create an attribute pool with ROOT_QUARK as base
67 TmfAttributePool pool
= new TmfAttributePool(fStateSystem
, ITmfStateSystem
.ROOT_ATTRIBUTE
);
70 int quark
= fStateSystem
.getQuarkAbsoluteAndAdd(DUMMY_STRING
);
71 pool
= new TmfAttributePool(fStateSystem
, quark
);
75 * Test the constructor with an invalid attribute
77 @Test(expected
= IllegalArgumentException
.class)
78 public void testConstructorBad() {
79 new TmfAttributePool(fStateSystem
, ITmfStateSystem
.INVALID_ATTRIBUTE
);
83 * Test the constructor with an invalid positive attribute
85 @Test(expected
= IllegalArgumentException
.class)
86 public void testConstructorBad2() {
87 new TmfAttributePool(fStateSystem
, 3);
91 * Test attributes using only one level of children
94 public void testSimplePool() {
95 TmfAttributePool pool
= new TmfAttributePool(fStateSystem
, ITmfStateSystem
.ROOT_ATTRIBUTE
);
98 /* Get some attributes */
99 Integer available
= pool
.getAvailable();
100 assertNotNull(available
);
101 Integer available2
= pool
.getAvailable();
102 assertNotNull(available2
);
103 assertNotEquals(available
, available2
);
105 /* Verify the names of the attributes */
106 assertEquals("0", fStateSystem
.getAttributeName(available
));
107 assertEquals("1", fStateSystem
.getAttributeName(available2
));
110 fStateSystem
.modifyAttribute(START_TIME
+ 10, VALUE
, available
);
111 fStateSystem
.modifyAttribute(START_TIME
+ 10, VALUE
, available2
);
113 /* Recycle one and make sure it is set to null */
114 pool
.recycle(available
, START_TIME
+ 20);
115 ITmfStateValue value
= fStateSystem
.queryOngoingState(available
);
116 assertEquals(TmfStateValue
.nullValue(), value
);
118 /* Get a new one and make sure it is reusing the one just recycled */
119 Integer available3
= pool
.getAvailable();
120 assertEquals(available
, available3
);
122 /* Get a new attribute and make sure it is different from both other used attributes */
123 Integer available4
= pool
.getAvailable();
124 assertNotEquals(available3
, available4
);
125 assertNotEquals(available2
, available4
);
127 /* Recycle available attributes, in reverse order and see how they are returned */
128 pool
.recycle(available4
, START_TIME
+ 30);
129 pool
.recycle(available2
, START_TIME
+ 30);
130 pool
.recycle(available3
, START_TIME
+ 30);
132 Integer available5
= pool
.getAvailable();
133 assertEquals(available4
, available5
);
135 Integer available6
= pool
.getAvailable();
136 assertEquals(available2
, available6
);
138 Integer available7
= pool
.getAvailable();
139 assertEquals(available3
, available7
);
143 * Test attributes with sub-trees
146 public void testPoolWithSubTree() {
147 TmfAttributePool pool
= new TmfAttributePool(fStateSystem
, ITmfStateSystem
.ROOT_ATTRIBUTE
);
150 /* Get some attributes */
151 Integer available
= pool
.getAvailable();
152 assertNotNull(available
);
154 /* Add children and set values for them */
156 Integer child1
= fStateSystem
.getQuarkRelativeAndAdd(available
, "child1");
157 Integer child2
= fStateSystem
.getQuarkRelativeAndAdd(available
, "child2");
158 fStateSystem
.modifyAttribute(START_TIME
+ 10, VALUE
, available
);
159 fStateSystem
.modifyAttribute(START_TIME
+ 10, VALUE
, child1
);
160 fStateSystem
.modifyAttribute(START_TIME
+ 10, VALUE
, child2
);
162 pool
.recycle(available
, START_TIME
+ 20);
164 ITmfStateValue value
= fStateSystem
.queryOngoingState(available
);
165 assertEquals(TmfStateValue
.nullValue(), value
);
166 value
= fStateSystem
.queryOngoingState(child1
);
167 assertEquals(TmfStateValue
.nullValue(), value
);
168 value
= fStateSystem
.queryOngoingState(child2
);
169 assertEquals(TmfStateValue
.nullValue(), value
);
170 } catch (StateValueTypeException e
) {
171 fail(e
.getMessage());
176 * Test pool with priority queue
179 public void testPriorityPool() {
180 TmfAttributePool pool
= new TmfAttributePool(fStateSystem
, ITmfStateSystem
.ROOT_ATTRIBUTE
, QueueType
.PRIORITY
);
183 /* Get some attributes */
184 Integer available
= pool
.getAvailable();
185 assertNotNull(available
);
186 Integer available2
= pool
.getAvailable();
187 assertNotNull(available2
);
188 assertNotEquals(available
, available2
);
190 /* Verify the names of the attributes */
191 assertEquals("0", fStateSystem
.getAttributeName(available
));
192 assertEquals("1", fStateSystem
.getAttributeName(available2
));
195 pool
.recycle(available
, START_TIME
+ 20);
197 /* Get a new one and make sure it is reusing the one just recycled */
198 Integer available3
= pool
.getAvailable();
199 assertEquals(available
, available3
);
201 /* Get a new attribute and make sure it is different from both other used attributes */
202 Integer available4
= pool
.getAvailable();
203 assertNotEquals(available3
, available4
);
204 assertNotEquals(available2
, available4
);
206 /* Recycle available attributes, in reverse order and see how they are returned */
207 pool
.recycle(available4
, START_TIME
+ 30);
208 pool
.recycle(available2
, START_TIME
+ 30);
209 pool
.recycle(available3
, START_TIME
+ 30);
211 Integer available5
= pool
.getAvailable();
212 assertEquals(available3
, available5
);
214 Integer available6
= pool
.getAvailable();
215 assertEquals(available2
, available6
);
217 Integer available7
= pool
.getAvailable();
218 assertEquals(available4
, available7
);
222 * Test recycling the root attribute
224 @Test(expected
= IllegalArgumentException
.class)
225 public void testRecycleWrongQuark() {
226 TmfAttributePool pool
= new TmfAttributePool(fStateSystem
, ITmfStateSystem
.ROOT_ATTRIBUTE
);
229 pool
.recycle(ITmfStateSystem
.ROOT_ATTRIBUTE
, START_TIME
+ 10);
233 * Test recycling one of the children
235 @Test(expected
= IllegalArgumentException
.class)
236 public void testRecycleChildQuark() {
237 TmfAttributePool pool
= new TmfAttributePool(fStateSystem
, ITmfStateSystem
.ROOT_ATTRIBUTE
);
240 /* Get some attributes */
241 Integer available
= pool
.getAvailable();
242 assertNotNull(available
);
244 /* Add children and set values for them */
246 Integer child1
= fStateSystem
.getQuarkRelativeAndAdd(available
, "child1");
247 pool
.recycle(child1
, START_TIME
+ 10);
248 } catch (StateValueTypeException e
) {
249 fail(e
.getMessage());