tmf: Rename packages to org.eclipse.tracecompass.*
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ctf.core.tests / src / org / eclipse / tracecompass / tmf / ctf / core / tests / CtfTmfContextTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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 * Patrick Tasse - Updated for removal of context clone
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.tmf.ctf.core.tests;
16
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assume.assumeTrue;
19
20 import java.util.ArrayList;
21
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
24 import org.eclipse.tracecompass.tmf.ctf.core.CtfTmfContext;
25 import org.eclipse.tracecompass.tmf.ctf.core.CtfTmfEvent;
26 import org.eclipse.tracecompass.tmf.ctf.core.CtfTmfTrace;
27 import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
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 */
37 public class CtfTmfContextTest {
38
39 private static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.KERNEL;
40 private static final long begin = 1332170682440133097L; /* Trace start time */
41 private static final long end = 1332170692664579801L; /* Trace end time */
42
43 private CtfTmfTrace trace;
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 {
61 assumeTrue(testTrace.exists());
62 trace = new CtfTmfTrace();
63 String path = testTrace.getPath();
64 trace.initTrace((IResource) null, path, CtfTmfEvent.class);
65 }
66
67 /**
68 * Index all the events in the test trace.
69 */
70 @Test
71 public void testIndexing() {
72 CtfTmfContext context = new CtfTmfContext(trace);
73 context.seek(0);
74
75 int count = 0;
76 while (trace.getNext(context) != null) {
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 *
86 * @throws InterruptedException
87 * Would fail the test
88 */
89 @Test
90 public void testTooManyContexts() throws InterruptedException {
91 final int lwcCount = 101;
92 double increment = (end - begin) / lwcCount;
93 final ArrayList<Long> vals = new ArrayList<>();
94 final ArrayList<Thread> threads = new ArrayList<>();
95 final ArrayList<CtfTmfContext> tooManyContexts = new ArrayList<>();
96
97 for (double i = begin; i < end; i += increment) {
98 SeekerThread thread = new SeekerThread() {
99 @Override
100 public void run() {
101 CtfTmfContext lwc = new CtfTmfContext(trace);
102 lwc.seek(val);
103 trace.getNext(lwc);
104 synchronized(trace){
105 if (lwc.getCurrentEvent() != null) {
106 vals.add(lwc.getCurrentEvent().getTimestamp().getValue());
107 }
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 }
This page took 0.033321 seconds and 5 git commands to generate.