| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013, 2016 Ericsson |
| 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 | * Contributors: |
| 10 | * Matthew Khouzam - Initial API and implementation |
| 11 | ******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.tracecompass.statesystem.core.tests.backend; |
| 14 | |
| 15 | import static org.junit.Assert.assertArrayEquals; |
| 16 | import static org.junit.Assert.assertEquals; |
| 17 | import static org.junit.Assert.assertNotNull; |
| 18 | import static org.junit.Assert.assertNull; |
| 19 | import static org.junit.Assert.fail; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.List; |
| 23 | |
| 24 | import org.eclipse.jdt.annotation.Nullable; |
| 25 | import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend; |
| 26 | import org.eclipse.tracecompass.statesystem.core.backend.StateHistoryBackendFactory; |
| 27 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException; |
| 28 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; |
| 29 | import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException; |
| 30 | import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval; |
| 31 | import org.eclipse.tracecompass.statesystem.core.interval.TmfStateInterval; |
| 32 | import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue; |
| 33 | import org.junit.BeforeClass; |
| 34 | import org.junit.Test; |
| 35 | |
| 36 | /** |
| 37 | * Test cases for the in-memory backend |
| 38 | * |
| 39 | * @author Matthew Khouzam |
| 40 | */ |
| 41 | public class InMemoryBackendTest extends StateHistoryBackendTestBase { |
| 42 | |
| 43 | private static final String SSID = "test-ss"; |
| 44 | private static final int NUMBER_OF_ATTRIBUTES = 10; |
| 45 | |
| 46 | private static @Nullable IStateHistoryBackend fixture; |
| 47 | |
| 48 | /** |
| 49 | * Test setup. make a state system that is moderately large |
| 50 | */ |
| 51 | @BeforeClass |
| 52 | public static void init() { |
| 53 | IStateHistoryBackend backend = StateHistoryBackendFactory.createInMemoryBackend(SSID, 0); |
| 54 | for (int attribute = 0; attribute < NUMBER_OF_ATTRIBUTES; attribute++) { |
| 55 | for (int timeStart = 0; timeStart < 1000; timeStart++) { |
| 56 | try { |
| 57 | final int stateEndTime = (timeStart * 100) + 90 + attribute; |
| 58 | final int stateStartTime = timeStart * 100 + attribute; |
| 59 | backend.insertPastState(stateStartTime, stateEndTime, attribute, TmfStateValue.newValueInt(timeStart % 100)); |
| 60 | if (timeStart != 999) { |
| 61 | backend.insertPastState(stateEndTime + 1, stateEndTime + 9, attribute, TmfStateValue.nullValue()); |
| 62 | } |
| 63 | } catch (TimeRangeException e) { |
| 64 | /* Should not happen here */ |
| 65 | throw new IllegalStateException(); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | fixture = backend; |
| 70 | } |
| 71 | |
| 72 | private static void testInterval(@Nullable ITmfStateInterval interval, int startTime, |
| 73 | int endTime, int value) { |
| 74 | assertNotNull(interval); |
| 75 | assertEquals(startTime, interval.getStartTime()); |
| 76 | assertEquals(endTime, interval.getEndTime()); |
| 77 | try { |
| 78 | assertEquals(value, interval.getStateValue().unboxInt()); |
| 79 | } catch (StateValueTypeException e) { |
| 80 | fail(e.getMessage()); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected IStateHistoryBackend getBackendForBuilding(long startTime) { |
| 86 | return StateHistoryBackendFactory.createInMemoryBackend(SSID, startTime); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Test at start time |
| 91 | */ |
| 92 | @Test |
| 93 | public void testStartTime() { |
| 94 | IStateHistoryBackend backend = fixture; |
| 95 | assertNotNull(backend); |
| 96 | assertEquals(0, backend.getStartTime()); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Test at end time |
| 101 | */ |
| 102 | @Test |
| 103 | public void testEndTime() { |
| 104 | IStateHistoryBackend backend = fixture; |
| 105 | assertNotNull(backend); |
| 106 | assertEquals(99999, backend.getEndTime()); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Query the state system |
| 111 | */ |
| 112 | @Test |
| 113 | public void testDoQuery() { |
| 114 | List<@Nullable ITmfStateInterval> interval = new ArrayList<>(NUMBER_OF_ATTRIBUTES); |
| 115 | for (int i = 0; i < NUMBER_OF_ATTRIBUTES; i++) { |
| 116 | interval.add(null); |
| 117 | } |
| 118 | IStateHistoryBackend backend = fixture; |
| 119 | assertNotNull(backend); |
| 120 | try { |
| 121 | backend.doQuery(interval, 950); |
| 122 | } catch (TimeRangeException | StateSystemDisposedException e) { |
| 123 | fail(e.getMessage()); |
| 124 | } |
| 125 | |
| 126 | assertEquals(NUMBER_OF_ATTRIBUTES, interval.size()); |
| 127 | testInterval(interval.get(0), 900, 990, 9); |
| 128 | testInterval(interval.get(1), 901, 991, 9); |
| 129 | testInterval(interval.get(2), 902, 992, 9); |
| 130 | testInterval(interval.get(3), 903, 993, 9); |
| 131 | testInterval(interval.get(4), 904, 994, 9); |
| 132 | testInterval(interval.get(5), 905, 995, 9); |
| 133 | testInterval(interval.get(6), 906, 996, 9); |
| 134 | testInterval(interval.get(7), 907, 997, 9); |
| 135 | testInterval(interval.get(8), 908, 998, 9); |
| 136 | testInterval(interval.get(9), 909, 999, 9); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | /** |
| 141 | * Test single attribute then compare it to a full query |
| 142 | */ |
| 143 | @Test |
| 144 | public void testQueryAttribute() { |
| 145 | try { |
| 146 | IStateHistoryBackend backend = fixture; |
| 147 | assertNotNull(backend); |
| 148 | ITmfStateInterval interval[] = new TmfStateInterval[10]; |
| 149 | for (int i = 0; i < 10; i++) { |
| 150 | interval[i] = backend.doSingularQuery(950, i); |
| 151 | } |
| 152 | |
| 153 | testInterval(interval[0], 900, 990, 9); |
| 154 | testInterval(interval[1], 901, 991, 9); |
| 155 | testInterval(interval[2], 902, 992, 9); |
| 156 | testInterval(interval[3], 903, 993, 9); |
| 157 | testInterval(interval[4], 904, 994, 9); |
| 158 | testInterval(interval[5], 905, 995, 9); |
| 159 | testInterval(interval[6], 906, 996, 9); |
| 160 | testInterval(interval[7], 907, 997, 9); |
| 161 | testInterval(interval[8], 908, 998, 9); |
| 162 | testInterval(interval[9], 909, 999, 9); |
| 163 | |
| 164 | List<@Nullable ITmfStateInterval> intervalQuery = new ArrayList<>(NUMBER_OF_ATTRIBUTES); |
| 165 | for (int i = 0; i < NUMBER_OF_ATTRIBUTES; i++) { |
| 166 | intervalQuery.add(null); |
| 167 | } |
| 168 | |
| 169 | backend.doQuery(intervalQuery, 950); |
| 170 | ITmfStateInterval ref[] = intervalQuery.toArray(new ITmfStateInterval[0]); |
| 171 | assertArrayEquals(ref, interval); |
| 172 | |
| 173 | } catch (TimeRangeException | StateSystemDisposedException e) { |
| 174 | fail(e.getMessage()); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Test single attribute that should not exist |
| 180 | */ |
| 181 | @Test |
| 182 | public void testQueryAttributeEmpty() { |
| 183 | try { |
| 184 | IStateHistoryBackend backend = fixture; |
| 185 | assertNotNull(backend); |
| 186 | ITmfStateInterval interval = backend.doSingularQuery(999, 0); |
| 187 | assertEquals(TmfStateValue.nullValue(), interval.getStateValue()); |
| 188 | |
| 189 | } catch (TimeRangeException | StateSystemDisposedException e) { |
| 190 | fail(e.getMessage()); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Test first element in ss |
| 196 | */ |
| 197 | @Test |
| 198 | public void testBegin() { |
| 199 | try { |
| 200 | IStateHistoryBackend backend = fixture; |
| 201 | assertNotNull(backend); |
| 202 | ITmfStateInterval interval = backend.doSingularQuery(0, 0); |
| 203 | assertEquals(0, interval.getStartTime()); |
| 204 | assertEquals(90, interval.getEndTime()); |
| 205 | assertEquals(0, interval.getStateValue().unboxInt()); |
| 206 | |
| 207 | } catch (TimeRangeException | StateSystemDisposedException e) { |
| 208 | fail(e.getMessage()); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Test last element in ss |
| 214 | */ |
| 215 | @Test |
| 216 | public void testEnd() { |
| 217 | try { |
| 218 | IStateHistoryBackend backend = fixture; |
| 219 | assertNotNull(backend); |
| 220 | ITmfStateInterval interval = backend.doSingularQuery(99998, 9); |
| 221 | testInterval(interval, 99909, 99999, 99); |
| 222 | |
| 223 | } catch (TimeRangeException | StateSystemDisposedException e) { |
| 224 | fail(e.getMessage()); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Test out of range query |
| 230 | * |
| 231 | * @throws TimeRangeException |
| 232 | * Expected |
| 233 | */ |
| 234 | @Test(expected = TimeRangeException.class) |
| 235 | public void testOutOfRange_1() throws TimeRangeException { |
| 236 | try { |
| 237 | IStateHistoryBackend backend = fixture; |
| 238 | assertNotNull(backend); |
| 239 | ITmfStateInterval interval = backend.doSingularQuery(-1, 0); |
| 240 | assertNull(interval); |
| 241 | |
| 242 | } catch (StateSystemDisposedException e) { |
| 243 | fail(e.getMessage()); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Test out of range query |
| 249 | * |
| 250 | * @throws TimeRangeException |
| 251 | * Expected |
| 252 | */ |
| 253 | @Test(expected = TimeRangeException.class) |
| 254 | public void testOutOfRange_2() throws TimeRangeException { |
| 255 | try { |
| 256 | IStateHistoryBackend backend = fixture; |
| 257 | assertNotNull(backend); |
| 258 | ITmfStateInterval interval = backend.doSingularQuery(100000, 0); |
| 259 | assertNull(interval); |
| 260 | |
| 261 | } catch (StateSystemDisposedException e) { |
| 262 | fail(e.getMessage()); |
| 263 | } |
| 264 | } |
| 265 | } |