--- src/core/org/apache/jmeter/threads/JMeterContext.java (revision 1166194) +++ src/core/org/apache/jmeter/threads/JMeterContext.java (working copy) @@ -65,6 +65,7 @@ samplingStarted = false; threadNum = 0; readBuffer = null; + thread = null; } /** --- src/core/org/apache/jmeter/threads/JMeterThread.java (revision 1166194) +++ src/core/org/apache/jmeter/threads/JMeterThread.java (working copy) @@ -20,9 +20,12 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.ListIterator; +import java.util.Map; +import java.util.Set; import org.apache.jmeter.assertions.Assertion; import org.apache.jmeter.assertions.AssertionResult; @@ -123,6 +126,11 @@ private volatile Sampler currentSampler; + /** + * Variables backup state at start of run + */ + private JMeterVariables threadVarsBkp; + public JMeterThread(HashTree test, JMeterThreadMonitor monitor, ListenerNotifier note) { this.monitor = monitor; threadVars = new JMeterVariables(); @@ -302,6 +310,28 @@ threadFinished(); monitor.threadFinished(this); // Tell the engine we are done JMeterContextService.removeContext(); // Remove the ThreadLocal entry + clearThreadVars(); + } + } + + /** + * Clean all variables that have been added by samplers by coming back to initial set + */ + private void clearThreadVars() { + Set keysToKeepSet = new HashSet(); + for (Iterator> iterator = threadVarsBkp.getIterator(); iterator.hasNext();) { + keysToKeepSet.add(iterator.next().getKey()); + } + Set keysToRemoveSet = new HashSet(); + for (Iterator> iterator = threadVars.getIterator(); iterator.hasNext();) { + Map.Entry entry = iterator.next(); + if(!keysToKeepSet.contains(entry.getKey())) + { + keysToRemoveSet.add(entry.getKey()); + } + } + for (String key : keysToRemoveSet) { + threadVars.remove(key); } } @@ -473,6 +503,8 @@ * */ private void initRun(JMeterContext threadContext) { + threadVarsBkp = new JMeterVariables(); + threadVarsBkp.putAll(threadVars); threadContext.setVariables(threadVars); threadContext.setThreadNum(getThreadNum()); threadContext.getVariables().put(LAST_SAMPLE_OK, TRUE);