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