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
.StateValueTypeException
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.AbstractTmfStateProvider
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfStateProvider
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
30 * Stub test provider for test state system analysis module
32 * @author Geneviève Bastien
34 public class TestStateSystemProvider
extends AbstractTmfStateProvider
{
37 * This interface allows unit tests to provide only the event handling part
38 * of the state provider, without having to extend the analysis and the
42 public static interface TestStateProviderHandler
{
47 * The state system builder
49 * The event to handler
50 * @return <code>true</code> if everything went fine, or <code>false</code> to cancel
52 boolean eventHandle(@NonNull ITmfStateSystemBuilder ss
, ITmfEvent event
);
55 private static final int VERSION
= 1;
56 private static final String fString
= "[]";
57 private static int fCount
= 0;
58 private static final @NonNull TestStateProviderHandler DEFAULT_HANDLER
= (ss
, event
) -> {
59 /* Just need something to fill the state system */
60 if (fString
.equals(event
.getContent().getValue())) {
62 int quarkId
= ss
.getQuarkAbsoluteAndAdd("String");
63 int quark
= ss
.getQuarkRelativeAndAdd(quarkId
, fString
);
64 ss
.modifyAttribute(event
.getTimestamp().getValue(), TmfStateValue
.newValueInt(fCount
++), quark
);
65 } catch (TimeRangeException
| StateValueTypeException e
) {
71 private static @NonNull TestStateProviderHandler sfHandler
= DEFAULT_HANDLER
;
74 * Set the event handler for the state provider
77 * The class containing the event handler for this state provider
79 public static void setEventHandler(TestStateProviderHandler handler
) {
80 if (handler
== null) {
81 sfHandler
= DEFAULT_HANDLER
;
87 private final Lock fLock
= new ReentrantLock();
88 private @Nullable Condition fNextEventSignal
= null;
94 * The LTTng 2.0 kernel trace directory
96 public TestStateSystemProvider(@NonNull ITmfTrace trace
) {
97 super(trace
, "Stub State System");
101 public int getVersion() {
106 public ITmfStateProvider
getNewInstance() {
107 return new TestStateSystemProvider(this.getTrace());
111 protected void eventHandle(ITmfEvent event
) {
112 ITmfStateSystemBuilder ss
= checkNotNull(getStateSystemBuilder());
113 sfHandler
.eventHandle(ss
, event
);
117 public void processEvent(@NonNull ITmfEvent event
) {
120 Condition cond
= fNextEventSignal
;
124 } catch (InterruptedException e
) {
127 super.processEvent(event
);
133 * Set the processing of event to be one event at a time instead of the
134 * default behavior. It will block until the next call to
135 * {@link #signalNextEvent()} method call.
137 * @param throttleEvent
138 * Whether to wait for a signal to process the next event
140 public void setThrottling(boolean throttleEvent
) {
144 Condition cond
= fNextEventSignal
;
145 // If called for the first time, create a condition
147 cond
= fLock
.newCondition();
148 fNextEventSignal
= cond
;
152 Condition cond
= fNextEventSignal
;
154 fNextEventSignal
= null;
165 * Signal for the next event to be processed. Calling this method makes
166 * sense only if {@link #setThrottling(boolean)} has been set to true
168 public void signalNextEvent() {
171 Condition cond
= fNextEventSignal
;