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
.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
;
33 * Test the {@link TmfAttributePool} class
35 * @author Geneviève Bastien
38 public class AttributePoolTest
{
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);
44 private ITmfStateSystemBuilder fStateSystem
;
47 * Initialize the state system
49 public AttributePoolTest() {
50 IStateHistoryBackend backend
= StateHistoryBackendFactory
.createInMemoryBackend(DUMMY_STRING
, START_TIME
);
51 fStateSystem
= StateSystemFactory
.newStateSystem(backend
);
58 public void tearDown() {
59 fStateSystem
.dispose();
63 * Test the constructor of the attribute pool
66 public void testConstructorGood() {
67 // Create an attribute pool with ROOT_QUARK as base
68 TmfAttributePool pool
= new TmfAttributePool(fStateSystem
, ITmfStateSystem
.ROOT_ATTRIBUTE
);
71 int quark
= fStateSystem
.getQuarkAbsoluteAndAdd(DUMMY_STRING
);
72 pool
= new TmfAttributePool(fStateSystem
, quark
);
76 * Test the constructor with an invalid attribute
78 @Test(expected
= IllegalArgumentException
.class)
79 public void testConstructorBad() {
80 new TmfAttributePool(fStateSystem
, ITmfStateSystem
.INVALID_ATTRIBUTE
);
84 * Test the constructor with an invalid positive attribute
86 @Test(expected
= IllegalArgumentException
.class)
87 public void testConstructorBad2() {
88 new TmfAttributePool(fStateSystem
, 3);
92 * Test attributes using only one level of children
95 public void testSimplePool() {
96 TmfAttributePool pool
= new TmfAttributePool(fStateSystem
, ITmfStateSystem
.ROOT_ATTRIBUTE
);
99 /* Get some attributes */
100 Integer available
= pool
.getAvailable();
101 assertNotNull(available
);
102 Integer available2
= pool
.getAvailable();
103 assertNotNull(available2
);
104 assertNotEquals(available
, available2
);
106 /* Verify the names of the attributes */
107 assertEquals("0", fStateSystem
.getAttributeName(available
));
108 assertEquals("1", fStateSystem
.getAttributeName(available2
));
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());
118 /* Recycle one and make sure it is set to null */
119 pool
.recycle(available
, START_TIME
+ 20);
121 ITmfStateValue value
= fStateSystem
.queryOngoingState(available
);
122 assertEquals(TmfStateValue
.nullValue(), value
);
123 } catch (AttributeNotFoundException e
) {
124 fail(e
.getMessage());
127 /* Get a new one and make sure it is reusing the one just recycled */
128 Integer available3
= pool
.getAvailable();
129 assertEquals(available
, available3
);
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
);
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);
141 Integer available5
= pool
.getAvailable();
142 assertEquals(available4
, available5
);
144 Integer available6
= pool
.getAvailable();
145 assertEquals(available2
, available6
);
147 Integer available7
= pool
.getAvailable();
148 assertEquals(available3
, available7
);
152 * Test attributes with sub-trees
155 public void testPoolWithSubTree() {
156 TmfAttributePool pool
= new TmfAttributePool(fStateSystem
, ITmfStateSystem
.ROOT_ATTRIBUTE
);
159 /* Get some attributes */
160 Integer available
= pool
.getAvailable();
161 assertNotNull(available
);
163 /* Add children and set values for them */
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
);
171 pool
.recycle(available
, START_TIME
+ 20);
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());
185 * Test pool with priority queue
188 public void testPriorityPool() {
189 TmfAttributePool pool
= new TmfAttributePool(fStateSystem
, ITmfStateSystem
.ROOT_ATTRIBUTE
, QueueType
.PRIORITY
);
192 /* Get some attributes */
193 Integer available
= pool
.getAvailable();
194 assertNotNull(available
);
195 Integer available2
= pool
.getAvailable();
196 assertNotNull(available2
);
197 assertNotEquals(available
, available2
);
199 /* Verify the names of the attributes */
200 assertEquals("0", fStateSystem
.getAttributeName(available
));
201 assertEquals("1", fStateSystem
.getAttributeName(available2
));
204 pool
.recycle(available
, START_TIME
+ 20);
206 /* Get a new one and make sure it is reusing the one just recycled */
207 Integer available3
= pool
.getAvailable();
208 assertEquals(available
, available3
);
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
);
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);
220 Integer available5
= pool
.getAvailable();
221 assertEquals(available3
, available5
);
223 Integer available6
= pool
.getAvailable();
224 assertEquals(available2
, available6
);
226 Integer available7
= pool
.getAvailable();
227 assertEquals(available4
, available7
);
231 * Test recycling the root attribute
233 @Test(expected
= IllegalArgumentException
.class)
234 public void testRecycleWrongQuark() {
235 TmfAttributePool pool
= new TmfAttributePool(fStateSystem
, ITmfStateSystem
.ROOT_ATTRIBUTE
);
238 pool
.recycle(ITmfStateSystem
.ROOT_ATTRIBUTE
, START_TIME
+ 10);
242 * Test recycling one of the children
244 @Test(expected
= IllegalArgumentException
.class)
245 public void testRecycleChildQuark() {
246 TmfAttributePool pool
= new TmfAttributePool(fStateSystem
, ITmfStateSystem
.ROOT_ATTRIBUTE
);
249 /* Get some attributes */
250 Integer available
= pool
.getAvailable();
251 assertNotNull(available
);
253 /* Add children and set values for them */
255 Integer child1
= fStateSystem
.getQuarkRelativeAndAdd(available
, "child1");
256 pool
.recycle(child1
, START_TIME
+ 10);
257 } catch (StateValueTypeException e
) {
258 fail(e
.getMessage());