ctf: Depend on the tracecompass-test-traces project
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core.tests / src / org / eclipse / tracecompass / tmf / ctf / core / tests / context / 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.context;
16
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertTrue;
19
20 import java.util.ArrayList;
21
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
24 import org.eclipse.tracecompass.tmf.ctf.core.context.CtfTmfContext;
25 import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils;
26 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
27 import org.junit.After;
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 @NonNull CtfTestTrace testTrace = CtfTestTrace.KERNEL;
40 private static final long begin = 1332170682440133097L; /*
41 * Trace start time
42 */
43 private static final long end = 1332170692664579801L; /* Trace end time */
44
45 private CtfTmfTrace trace;
46
47 private class SeekerThread extends Thread {
48 long val;
49
50 public void setVal(long val) {
51 this.val = val;
52 }
53 }
54
55 /**
56 * Pre-test initialization
57 */
58 @Before
59 public void setUp() {
60 trace = CtfTmfTestTraceUtils.getTrace(testTrace);
61 }
62
63 /**
64 * Post-test clean-up.
65 */
66 @After
67 public void tearDown() {
68 if (trace != null) {
69 trace.dispose();
70 }
71 }
72
73 /**
74 * Index all the events in the test trace.
75 */
76 @Test
77 public void testIndexing() {
78 CtfTmfContext context = new CtfTmfContext(trace);
79 context.seek(0);
80
81 int count = 0;
82 while (trace.getNext(context) != null) {
83 count++;
84 }
85 assertTrue(count > 0);
86 }
87
88 /**
89 * Context fuzzer. Use an amount of contexts greater than the size of the
90 * iterator cache and have them access the trace in parallel.
91 *
92 * @throws InterruptedException
93 * Would fail the test
94 */
95 @Test
96 public void testTooManyContexts() throws InterruptedException {
97 final int lwcCount = 101;
98 double increment = (end - begin) / lwcCount;
99 final ArrayList<Long> vals = new ArrayList<>();
100 final ArrayList<Thread> threads = new ArrayList<>();
101
102 double time = begin;
103 for (int i = 0; i < lwcCount; i++) {
104 SeekerThread thread = new SeekerThread() {
105 @Override
106 public void run() {
107 CtfTmfContext lwc = new CtfTmfContext(trace);
108 lwc.seek(val);
109 trace.getNext(lwc);
110 synchronized (trace) {
111 if (lwc.getCurrentEvent() != null) {
112 vals.add(lwc.getCurrentEvent().getTimestamp().getValue());
113 }
114 }
115 }
116 };
117 thread.setVal((long) time);
118 threads.add(thread);
119 thread.start();
120 time += increment;
121 }
122
123 for (Thread t : threads) {
124 t.join();
125 }
126 assertEquals("seeks done ", lwcCount, vals.size());
127 for (long val : vals) {
128 assertTrue("val >= begin, " + val + " " + begin, val >= begin);
129 assertTrue("val >= end, " + val + " " + end, val <= end);
130 }
131 }
132 }
This page took 0.039081 seconds and 5 git commands to generate.