1 /*******************************************************************************
2 * Copyright (c) 2013, 2016 École Polytechnique de Montréal
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.analysis
;
12 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
14 import java
.util
.concurrent
.locks
.Condition
;
15 import java
.util
.concurrent
.locks
.Lock
;
16 import java
.util
.concurrent
.locks
.ReentrantLock
;
18 import org
.eclipse
.jdt
.annotation
.NonNull
;
19 import org
.eclipse
.jdt
.annotation
.Nullable
;
20 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.AbstractTmfStateProvider
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfStateProvider
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
31 * Stub test provider for test state system analysis module
33 * @author Geneviève Bastien
35 public class TestStateSystemProvider
extends AbstractTmfStateProvider
{
38 * This interface allows unit tests to provide only the event handling part
39 * of the state provider, without having to extend the analysis and the
43 public static interface TestStateProviderHandler
{
48 * The state system builder
50 * The event to handler
51 * @return <code>true</code> if everything went fine, or <code>false</code> to cancel
53 boolean eventHandle(@NonNull ITmfStateSystemBuilder ss
, ITmfEvent event
);
56 private static final int VERSION
= 1;
57 private static final String fString
= "[]";
58 private static int fCount
= 0;
59 private static final @NonNull TestStateProviderHandler DEFAULT_HANDLER
= (ss
, event
) -> {
60 /* Just need something to fill the state system */
61 if (fString
.equals(event
.getContent().getValue())) {
63 int quarkId
= ss
.getQuarkAbsoluteAndAdd("String");
64 int quark
= ss
.getQuarkRelativeAndAdd(quarkId
, fString
);
65 ss
.modifyAttribute(event
.getTimestamp().getValue(), TmfStateValue
.newValueInt(fCount
++), quark
);
66 } catch (TimeRangeException
| AttributeNotFoundException
| StateValueTypeException e
) {
72 private static @NonNull TestStateProviderHandler sfHandler
= DEFAULT_HANDLER
;
75 * Set the event handler for the state provider
78 * The class containing the event handler for this state provider
80 public static void setEventHandler(TestStateProviderHandler handler
) {
81 if (handler
== null) {
82 sfHandler
= DEFAULT_HANDLER
;
88 private final Lock fLock
= new ReentrantLock();
89 private @Nullable Condition fNextEventSignal
= null;
95 * The LTTng 2.0 kernel trace directory
97 public TestStateSystemProvider(@NonNull ITmfTrace trace
) {
98 super(trace
, "Stub State System");
102 public int getVersion() {
107 public ITmfStateProvider
getNewInstance() {
108 return new TestStateSystemProvider(this.getTrace());
112 protected void eventHandle(ITmfEvent event
) {
113 ITmfStateSystemBuilder ss
= checkNotNull(getStateSystemBuilder());
114 sfHandler
.eventHandle(ss
, event
);
120 public void processEvent(@NonNull ITmfEvent event
) {
123 Condition cond
= fNextEventSignal
;
127 } catch (InterruptedException e
) {
130 super.processEvent(event
);
136 * Set the processing of event to be one event at a time instead of the
137 * default behavior. It will block until the next call to
138 * {@link #signalNextEvent()} method call.
140 * @param throttleEvent
141 * Whether to wait for a signal to process the next event
143 public void setThrottling(boolean throttleEvent
) {
147 Condition cond
= fNextEventSignal
;
148 // If called for the first time, create a condition
150 cond
= fLock
.newCondition();
151 fNextEventSignal
= cond
;
155 Condition cond
= fNextEventSignal
;
157 fNextEventSignal
= null;
168 * Signal for the next event to be processed. Calling this method makes
169 * sense only if {@link #setThrottling(boolean)} has been set to true
171 public void signalNextEvent() {
174 Condition cond
= fNextEventSignal
;