Commit | Line | Data |
---|---|---|
22bae74e GB |
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; | |
22bae74e GB |
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; | |
30 | ||
31 | /** | |
32 | * Test the {@link TmfAttributePool} class | |
33 | * | |
34 | * @author Geneviève Bastien | |
35 | */ | |
36 | @NonNullByDefault | |
37 | public class AttributePoolTest { | |
38 | ||
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); | |
42 | ||
43 | private ITmfStateSystemBuilder fStateSystem; | |
44 | ||
45 | /** | |
46 | * Initialize the state system | |
47 | */ | |
48 | public AttributePoolTest() { | |
49 | IStateHistoryBackend backend = StateHistoryBackendFactory.createInMemoryBackend(DUMMY_STRING, START_TIME); | |
50 | fStateSystem = StateSystemFactory.newStateSystem(backend); | |
51 | } | |
52 | ||
53 | /** | |
54 | * Clean-up | |
55 | */ | |
56 | @After | |
57 | public void tearDown() { | |
58 | fStateSystem.dispose(); | |
59 | } | |
60 | ||
61 | /** | |
62 | * Test the constructor of the attribute pool | |
63 | */ | |
64 | @Test | |
65 | public void testConstructorGood() { | |
66 | // Create an attribute pool with ROOT_QUARK as base | |
67 | TmfAttributePool pool = new TmfAttributePool(fStateSystem, ITmfStateSystem.ROOT_ATTRIBUTE); | |
68 | assertNotNull(pool); | |
69 | ||
70 | int quark = fStateSystem.getQuarkAbsoluteAndAdd(DUMMY_STRING); | |
71 | pool = new TmfAttributePool(fStateSystem, quark); | |
72 | } | |
73 | ||
74 | /** | |
75 | * Test the constructor with an invalid attribute | |
76 | */ | |
77 | @Test(expected = IllegalArgumentException.class) | |
78 | public void testConstructorBad() { | |
79 | new TmfAttributePool(fStateSystem, ITmfStateSystem.INVALID_ATTRIBUTE); | |
80 | } | |
81 | ||
82 | /** | |
83 | * Test the constructor with an invalid positive attribute | |
84 | */ | |
85 | @Test(expected = IllegalArgumentException.class) | |
86 | public void testConstructorBad2() { | |
87 | new TmfAttributePool(fStateSystem, 3); | |
88 | } | |
89 | ||
90 | /** | |
91 | * Test attributes using only one level of children | |
92 | */ | |
93 | @Test | |
94 | public void testSimplePool() { | |
95 | TmfAttributePool pool = new TmfAttributePool(fStateSystem, ITmfStateSystem.ROOT_ATTRIBUTE); | |
96 | assertNotNull(pool); | |
97 | ||
98 | /* Get some attributes */ | |
99 | Integer available = pool.getAvailable(); | |
100 | assertNotNull(available); | |
101 | Integer available2 = pool.getAvailable(); | |
102 | assertNotNull(available2); | |
103 | assertNotEquals(available, available2); | |
104 | ||
105 | /* Verify the names of the attributes */ | |
106 | assertEquals("0", fStateSystem.getAttributeName(available)); | |
107 | assertEquals("1", fStateSystem.getAttributeName(available2)); | |
108 | ||
109 | /* Modify them */ | |
ed48dc75 PT |
110 | fStateSystem.modifyAttribute(START_TIME + 10, VALUE, available); |
111 | fStateSystem.modifyAttribute(START_TIME + 10, VALUE, available2); | |
22bae74e GB |
112 | |
113 | /* Recycle one and make sure it is set to null */ | |
114 | pool.recycle(available, START_TIME + 20); | |
ed48dc75 PT |
115 | ITmfStateValue value = fStateSystem.queryOngoingState(available); |
116 | assertEquals(TmfStateValue.nullValue(), value); | |
22bae74e GB |
117 | |
118 | /* Get a new one and make sure it is reusing the one just recycled */ | |
119 | Integer available3 = pool.getAvailable(); | |
120 | assertEquals(available, available3); | |
121 | ||
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); | |
126 | ||
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); | |
131 | ||
132 | Integer available5 = pool.getAvailable(); | |
133 | assertEquals(available4, available5); | |
134 | ||
135 | Integer available6 = pool.getAvailable(); | |
136 | assertEquals(available2, available6); | |
137 | ||
138 | Integer available7 = pool.getAvailable(); | |
139 | assertEquals(available3, available7); | |
140 | } | |
141 | ||
142 | /** | |
143 | * Test attributes with sub-trees | |
144 | */ | |
145 | @Test | |
146 | public void testPoolWithSubTree() { | |
147 | TmfAttributePool pool = new TmfAttributePool(fStateSystem, ITmfStateSystem.ROOT_ATTRIBUTE); | |
148 | assertNotNull(pool); | |
149 | ||
150 | /* Get some attributes */ | |
151 | Integer available = pool.getAvailable(); | |
152 | assertNotNull(available); | |
153 | ||
154 | /* Add children and set values for them */ | |
155 | try { | |
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); | |
161 | ||
162 | pool.recycle(available, START_TIME + 20); | |
163 | ||
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); | |
ed48dc75 | 170 | } catch (StateValueTypeException e) { |
22bae74e GB |
171 | fail(e.getMessage()); |
172 | } | |
173 | } | |
174 | ||
175 | /** | |
176 | * Test pool with priority queue | |
177 | */ | |
178 | @Test | |
179 | public void testPriorityPool() { | |
180 | TmfAttributePool pool = new TmfAttributePool(fStateSystem, ITmfStateSystem.ROOT_ATTRIBUTE, QueueType.PRIORITY); | |
181 | assertNotNull(pool); | |
182 | ||
183 | /* Get some attributes */ | |
184 | Integer available = pool.getAvailable(); | |
185 | assertNotNull(available); | |
186 | Integer available2 = pool.getAvailable(); | |
187 | assertNotNull(available2); | |
188 | assertNotEquals(available, available2); | |
189 | ||
190 | /* Verify the names of the attributes */ | |
191 | assertEquals("0", fStateSystem.getAttributeName(available)); | |
192 | assertEquals("1", fStateSystem.getAttributeName(available2)); | |
193 | ||
194 | /* Recycle on */ | |
195 | pool.recycle(available, START_TIME + 20); | |
196 | ||
197 | /* Get a new one and make sure it is reusing the one just recycled */ | |
198 | Integer available3 = pool.getAvailable(); | |
199 | assertEquals(available, available3); | |
200 | ||
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); | |
205 | ||
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); | |
210 | ||
211 | Integer available5 = pool.getAvailable(); | |
212 | assertEquals(available3, available5); | |
213 | ||
214 | Integer available6 = pool.getAvailable(); | |
215 | assertEquals(available2, available6); | |
216 | ||
217 | Integer available7 = pool.getAvailable(); | |
218 | assertEquals(available4, available7); | |
219 | } | |
220 | ||
221 | /** | |
222 | * Test recycling the root attribute | |
223 | */ | |
224 | @Test(expected = IllegalArgumentException.class) | |
225 | public void testRecycleWrongQuark() { | |
226 | TmfAttributePool pool = new TmfAttributePool(fStateSystem, ITmfStateSystem.ROOT_ATTRIBUTE); | |
227 | assertNotNull(pool); | |
228 | ||
229 | pool.recycle(ITmfStateSystem.ROOT_ATTRIBUTE, START_TIME + 10); | |
230 | } | |
231 | ||
232 | /** | |
233 | * Test recycling one of the children | |
234 | */ | |
235 | @Test(expected = IllegalArgumentException.class) | |
236 | public void testRecycleChildQuark() { | |
237 | TmfAttributePool pool = new TmfAttributePool(fStateSystem, ITmfStateSystem.ROOT_ATTRIBUTE); | |
238 | assertNotNull(pool); | |
239 | ||
240 | /* Get some attributes */ | |
241 | Integer available = pool.getAvailable(); | |
242 | assertNotNull(available); | |
243 | ||
244 | /* Add children and set values for them */ | |
245 | try { | |
246 | Integer child1 = fStateSystem.getQuarkRelativeAndAdd(available, "child1"); | |
247 | pool.recycle(child1, START_TIME + 10); | |
248 | } catch (StateValueTypeException e) { | |
249 | fail(e.getMessage()); | |
250 | } | |
251 | } | |
252 | } |