Commit | Line | Data |
---|---|---|
53b235e1 | 1 | /******************************************************************************* |
ea271da6 | 2 | * Copyright (c) 2012, 2013 Ericsson |
53b235e1 MK |
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 implementation | |
11 | * Alexandre Montplaisir | |
ea271da6 | 12 | * Patrick Tasse - Updated for removal of context clone |
53b235e1 MK |
13 | *******************************************************************************/ |
14 | ||
15 | package org.eclipse.linuxtools.tmf.core.tests.ctfadaptor; | |
16 | ||
17 | import static org.junit.Assert.assertTrue; | |
5dd1fa65 | 18 | import static org.junit.Assume.assumeTrue; |
53b235e1 MK |
19 | |
20 | import java.util.ArrayList; | |
21 | ||
22 | import org.eclipse.core.resources.IResource; | |
81a2d02e | 23 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfContext; |
5dd1fa65 | 24 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent; |
53b235e1 MK |
25 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace; |
26 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; | |
5dd1fa65 | 27 | import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTraces; |
53b235e1 MK |
28 | import org.junit.Before; |
29 | import org.junit.Test; | |
30 | ||
31 | /** | |
32 | * Tests for the CtfTmfLightweightContext class | |
33 | * | |
34 | * @author Matthew Khouzam | |
35 | * @version 1.1 | |
36 | */ | |
81a2d02e | 37 | public class CtfTmfContextTest { |
53b235e1 | 38 | |
5dd1fa65 | 39 | private static final int TRACE_INDEX = 0; |
53b235e1 MK |
40 | private static final long begin = 1332170682440133097L; /* Trace start time */ |
41 | private static final long end = 1332170692664579801L; /* Trace end time */ | |
42 | ||
81a2d02e | 43 | private CtfTmfTrace trace; |
53b235e1 MK |
44 | |
45 | private class SeekerThread extends Thread { | |
46 | long val; | |
47 | ||
48 | public void setVal(long val) { | |
49 | this.val = val; | |
50 | } | |
51 | } | |
52 | ||
53 | /** | |
54 | * Pre-test initialization | |
55 | * | |
56 | * @throws TmfTraceException | |
57 | * If the trace couldn't be init'ed, which shouldn't happen. | |
58 | */ | |
59 | @Before | |
60 | public void setUp() throws TmfTraceException { | |
5dd1fa65 | 61 | assumeTrue(CtfTmfTestTraces.tracesExist()); |
81a2d02e | 62 | trace = new CtfTmfTrace(); |
5dd1fa65 AM |
63 | String path = CtfTmfTestTraces.getTestTracePath(TRACE_INDEX); |
64 | trace.initTrace((IResource) null, path, CtfTmfEvent.class); | |
53b235e1 MK |
65 | } |
66 | ||
67 | /** | |
68 | * Index all the events in the test trace. | |
69 | */ | |
70 | @Test | |
71 | public void testIndexing() { | |
81a2d02e | 72 | CtfTmfContext context = new CtfTmfContext(trace); |
53b235e1 MK |
73 | context.seek(0); |
74 | ||
75 | int count = 0; | |
81a2d02e | 76 | while (trace.getNext(context) != null) { |
53b235e1 MK |
77 | count++; |
78 | } | |
79 | assertTrue(count > 0); | |
80 | } | |
81 | ||
82 | /** | |
83 | * Context fuzzer. Use an amount of contexts greater than the size of the | |
84 | * iterator cache and have them access the trace in parallel. | |
85 | * | |
53b235e1 MK |
86 | * @throws InterruptedException |
87 | * Would fail the test | |
88 | */ | |
89 | @Test | |
54a7a54c | 90 | public void testTooManyContexts() throws InterruptedException { |
53b235e1 MK |
91 | final int lwcCount = 101; |
92 | double increment = (end - begin) / lwcCount; | |
93 | final ArrayList<Long> vals = new ArrayList<Long>(); | |
94 | final ArrayList<Thread> threads = new ArrayList<Thread>(); | |
81a2d02e | 95 | final ArrayList<CtfTmfContext> tooManyContexts = new ArrayList<CtfTmfContext>(); |
53b235e1 MK |
96 | |
97 | for (double i = begin; i < end; i += increment) { | |
98 | SeekerThread thread = new SeekerThread() { | |
99 | @Override | |
100 | public void run() { | |
81a2d02e | 101 | CtfTmfContext lwc = new CtfTmfContext(trace); |
53b235e1 | 102 | lwc.seek(val); |
81a2d02e AM |
103 | trace.getNext(lwc); |
104 | synchronized(trace){ | |
132a02b0 | 105 | if (lwc.getCurrentEvent() != null) { |
58f3bc52 | 106 | vals.add(lwc.getCurrentEvent().getTimestamp().getValue()); |
132a02b0 | 107 | } |
53b235e1 MK |
108 | tooManyContexts.add(lwc); |
109 | } | |
110 | } | |
111 | }; | |
112 | thread.setVal((long)i); | |
113 | threads.add(thread); | |
114 | thread.start(); | |
115 | } | |
116 | ||
117 | for( Thread t: threads){ | |
118 | t.join(); | |
119 | } | |
120 | ||
121 | for( Long val : vals){ | |
122 | assertTrue(val >= begin); | |
123 | assertTrue(val <= end); | |
124 | } | |
125 | } | |
126 | } |