| 1 | |
package kg.apc.jmeter.functions; |
| 2 | |
|
| 3 | |
import kg.apc.jmeter.modifiers.FifoMap; |
| 4 | |
import org.apache.jmeter.engine.util.CompoundVariable; |
| 5 | |
import org.apache.jmeter.functions.AbstractFunction; |
| 6 | |
import org.apache.jmeter.functions.InvalidVariableException; |
| 7 | |
import org.apache.jmeter.samplers.SampleResult; |
| 8 | |
import org.apache.jmeter.samplers.Sampler; |
| 9 | |
import org.apache.jmeter.threads.JMeterVariables; |
| 10 | |
import org.apache.jmeter.util.JMeterUtils; |
| 11 | |
import org.apache.jorphan.logging.LoggingManager; |
| 12 | |
import org.apache.log.Logger; |
| 13 | |
|
| 14 | |
import java.util.Collection; |
| 15 | |
import java.util.LinkedList; |
| 16 | |
import java.util.List; |
| 17 | |
|
| 18 | |
public class FifoPop extends AbstractFunction { |
| 19 | 1 | private static final Logger log = LoggingManager.getLoggerForClass(); |
| 20 | |
|
| 21 | 1 | private static final List<String> desc = new LinkedList<String>(); |
| 22 | |
private static final String KEY = "__fifoPop"; |
| 23 | |
private long timeout; |
| 24 | |
|
| 25 | |
static { |
| 26 | 1 | desc.add("FIFO queue name to pop value"); |
| 27 | 1 | desc.add("Name of variable in which to store the result (optional)"); |
| 28 | 1 | } |
| 29 | |
|
| 30 | |
private Object[] values; |
| 31 | |
|
| 32 | 4 | public FifoPop() { |
| 33 | 4 | timeout = JMeterUtils.getPropDefault(FifoMap.TIMEOUT_PROP, Long.MAX_VALUE); |
| 34 | 4 | } |
| 35 | |
|
| 36 | |
@Override |
| 37 | |
public synchronized String execute(SampleResult previousResult, Sampler currentSampler) |
| 38 | |
throws InvalidVariableException { |
| 39 | 1 | String fifoName = ((CompoundVariable) values[0]).execute(); |
| 40 | |
|
| 41 | 1 | String value = null; |
| 42 | |
try { |
| 43 | 1 | Object valueObj = FifoMap.getInstance().pop(fifoName, timeout); |
| 44 | 1 | if (valueObj != null) { |
| 45 | 1 | value = valueObj.toString(); |
| 46 | |
} |
| 47 | 0 | } catch (InterruptedException ex) { |
| 48 | 0 | log.warn("Interrupted pop from queue " + fifoName); |
| 49 | 0 | value = "INTERRUPTED"; |
| 50 | 1 | } |
| 51 | |
|
| 52 | 1 | JMeterVariables vars = getVariables(); |
| 53 | 1 | if (vars != null && values.length > 1) { |
| 54 | 1 | String varName = ((CompoundVariable) values[1]).execute().trim(); |
| 55 | 1 | vars.put(varName, value); |
| 56 | |
} |
| 57 | |
|
| 58 | 1 | return value; |
| 59 | |
} |
| 60 | |
|
| 61 | |
@Override |
| 62 | |
public synchronized void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException { |
| 63 | 2 | checkMinParameterCount(parameters, 1); |
| 64 | 2 | values = parameters.toArray(); |
| 65 | 2 | } |
| 66 | |
|
| 67 | |
@Override |
| 68 | |
public String getReferenceKey() { |
| 69 | 1 | return KEY; |
| 70 | |
} |
| 71 | |
|
| 72 | |
@Override |
| 73 | |
public List<String> getArgumentDesc() { |
| 74 | 1 | return desc; |
| 75 | |
} |
| 76 | |
} |