Search code examples
groovyjmeterperformance-testingjsr223post-processor

JSR223 Postprocessor is not working under tearDown thread group


I have created a test plan and added setup thread group where few pre-requisites are verified using different HTTP requests and respective JSR223 assertions. Failed assertions related data i.e. sampler name and expected value is placed in a property, name starts with failedSampler_.Below is the code.

import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.assertions.AssertionResult;

// JSR223 Assertion (Groovy)
String expectedValue = "test"; // Replace with your expected value
String jsonString = prev.getResponseDataAsString();


def jsonSlurper = new groovy.json.JsonSlurper()
def jsonObject = jsonSlurper.parseText(jsonString)
log.info(jsonObject.toString());

boolean matchFound = false;

// Iterate over the hits array and get the ab_name from each _src document
    jsonObject.data.record.hits.each { hit ->
        String actualValue = hit._name;
    log.info("Actual value is: " + actualValue);

    if (expectedValue.equals(actualValue)) {
        matchFound = true;
    }
}

if (!matchFound) {
    String sampleName = SampleResult.getSampleLabel();
    String failureMessage = "Assertion failed for sample: " + sampleName +
        "\nExpected value: " + expectedValue +
        "\nNo matching actual value found";
    props.put("failedSampler_" + sampleName, expectedValue);
    log.error(failureMessage);

        // Create a new AssertionResult
    AssertionResult result = new AssertionResult("${expectedValue} Not Found");
    result.setFailure(true);
    result.setFailureMessage(failureMessage);
    prev.addAssertionResult(result);
    prev.setSuccessful(false);
}

To verify the clean up part there is a tearDown thread group where JSR223 post processor is configured to remove the props data. But it is not clearing the prop data and on next run prop is showing the data from previous run as well. Code for JSR223 postprocessor under tearDown thread group is as below.

import org.apache.jmeter.reporters.ResultCollector
import org.apache.jorphan.collections.SearchByClass


def engine = engine = ctx.getEngine()
def test = engine.getClass().getDeclaredField('test')

test.setAccessible(true)

def testPlanTree = test.get(engine)

SearchByClass<ResultCollector> listenerSearch = new SearchByClass<>(ResultCollector.class)
testPlanTree.traverse(listenerSearch)
Collection<ResultCollector> listeners = listenerSearch.getSearchResults()

listeners.each { listener ->
    def files = listener.files
    files.each { file ->
        file.value.pw.close()
    }
}

log.info('Before clear:' +props.size()) 
props.clear()
log.info('After clear:' +props.size()) 

Note: No normal thread group is defined between setup and tearDown thread group currently. The purpose that needs to be achieve is that all failed assertion's expected value should be saved in prop with sampler name and can be access in normal thread group to perform some action on missing data that is verified in setup thread group.

Test plan image below

JMeter setup & tearDown Configurations


Solution

  • JSR223 PostProcessor is only executed in context of a Sampler. If there is no Sampler in the Thread Group - it won't be run.

    You either need to convert it to JSR223 Sampler or add another Sampler to the tearDown Thread Group like Debug Sampler or Dummy Sampler or whatever