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