Commit | Line | Data |
---|---|---|
53b235e1 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2012, 2014 Ericsson |
53b235e1 MK |
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 | |
ea271da6 | 12 | * Patrick Tasse - Updated for removal of context clone |
53b235e1 MK |
13 | *******************************************************************************/ |
14 | ||
9722e5d7 | 15 | package org.eclipse.tracecompass.tmf.ctf.core.tests.context; |
53b235e1 | 16 | |
5b341dc8 | 17 | import static org.junit.Assert.assertEquals; |
53b235e1 MK |
18 | import static org.junit.Assert.assertTrue; |
19 | ||
20 | import java.util.ArrayList; | |
21 | ||
c4d57ac1 AM |
22 | import org.eclipse.jdt.annotation.NonNull; |
23 | import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace; | |
9722e5d7 | 24 | import org.eclipse.tracecompass.tmf.ctf.core.context.CtfTmfContext; |
c4d57ac1 | 25 | import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils; |
9722e5d7 | 26 | import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace; |
9c0cef56 | 27 | import org.junit.After; |
53b235e1 MK |
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 | */ | |
81a2d02e | 37 | public class CtfTmfContextTest { |
53b235e1 | 38 | |
c4d57ac1 | 39 | private static final @NonNull CtfTestTrace testTrace = CtfTestTrace.KERNEL; |
5b341dc8 MK |
40 | private static final long begin = 1332170682440133097L; /* |
41 | * Trace start time | |
42 | */ | |
53b235e1 MK |
43 | private static final long end = 1332170692664579801L; /* Trace end time */ |
44 | ||
81a2d02e | 45 | private CtfTmfTrace trace; |
53b235e1 MK |
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 | |
53b235e1 MK |
57 | */ |
58 | @Before | |
c4d57ac1 AM |
59 | public void setUp() { |
60 | trace = CtfTmfTestTraceUtils.getTrace(testTrace); | |
53b235e1 MK |
61 | } |
62 | ||
9c0cef56 PT |
63 | /** |
64 | * Post-test clean-up. | |
65 | */ | |
66 | @After | |
67 | public void tearDown() { | |
68 | if (trace != null) { | |
69 | trace.dispose(); | |
70 | } | |
71 | } | |
72 | ||
53b235e1 MK |
73 | /** |
74 | * Index all the events in the test trace. | |
75 | */ | |
76 | @Test | |
77 | public void testIndexing() { | |
81a2d02e | 78 | CtfTmfContext context = new CtfTmfContext(trace); |
53b235e1 MK |
79 | context.seek(0); |
80 | ||
81 | int count = 0; | |
81a2d02e | 82 | while (trace.getNext(context) != null) { |
53b235e1 MK |
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 | * | |
53b235e1 MK |
92 | * @throws InterruptedException |
93 | * Would fail the test | |
94 | */ | |
95 | @Test | |
54a7a54c | 96 | public void testTooManyContexts() throws InterruptedException { |
53b235e1 MK |
97 | final int lwcCount = 101; |
98 | double increment = (end - begin) / lwcCount; | |
ccf2bbb4 AM |
99 | final ArrayList<Long> vals = new ArrayList<>(); |
100 | final ArrayList<Thread> threads = new ArrayList<>(); | |
53b235e1 | 101 | |
9c0cef56 PT |
102 | double time = begin; |
103 | for (int i = 0; i < lwcCount; i++) { | |
53b235e1 MK |
104 | SeekerThread thread = new SeekerThread() { |
105 | @Override | |
106 | public void run() { | |
81a2d02e | 107 | CtfTmfContext lwc = new CtfTmfContext(trace); |
53b235e1 | 108 | lwc.seek(val); |
81a2d02e | 109 | trace.getNext(lwc); |
5b341dc8 | 110 | synchronized (trace) { |
132a02b0 | 111 | if (lwc.getCurrentEvent() != null) { |
58f3bc52 | 112 | vals.add(lwc.getCurrentEvent().getTimestamp().getValue()); |
132a02b0 | 113 | } |
53b235e1 MK |
114 | } |
115 | } | |
116 | }; | |
9c0cef56 | 117 | thread.setVal((long) time); |
53b235e1 MK |
118 | threads.add(thread); |
119 | thread.start(); | |
9c0cef56 | 120 | time += increment; |
53b235e1 MK |
121 | } |
122 | ||
5b341dc8 | 123 | for (Thread t : threads) { |
53b235e1 MK |
124 | t.join(); |
125 | } | |
9c0cef56 | 126 | assertEquals("seeks done ", lwcCount, vals.size()); |
5b341dc8 MK |
127 | for (long val : vals) { |
128 | assertTrue("val >= begin, " + val + " " + begin, val >= begin); | |
129 | assertTrue("val >= end, " + val + " " + end, val <= end); | |
53b235e1 MK |
130 | } |
131 | } | |
132 | } |