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