Skip to content

Commit

Permalink
Merge pull request #266 from anasoid/265-for-java-executor-add-before…
Browse files Browse the repository at this point in the history
…-and-after-test-end-for-executor-java

for-java-executor-add-before-and-after-test-end-for-executor-java
  • Loading branch information
anasoid authored Oct 31, 2022
2 parents bce0af7 + 959a861 commit b2e8cba
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Properties;
import org.anasoid.jmc.plugins.utils.ExecutorUtils;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.engine.util.NoThreadClone;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.testelement.AbstractTestElement;
Expand All @@ -37,10 +38,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Base class for Java TestElement. */
/**
* Base class for Java TestElement.
*/
@SuppressWarnings("PMD.JUnit4TestShouldUseTestAnnotation")
public abstract class AbstractJavaTestElement<T extends JavaTestElementExecutor>
extends AbstractTestElement implements TestStateListener {
extends AbstractTestElement implements TestStateListener, NoThreadClone {

public static final String PARAMETERS = "parameters";

Expand Down Expand Up @@ -111,7 +114,7 @@ protected Properties getJMeterProperties() {
}

protected Logger getExecutorLogger() {
return LoggerFactory.getLogger(getExecutor().getClass().getName() + "." + getName());
return LoggerFactory.getLogger(getExecutorClass() + "." + getName());
}

protected Sampler getCurrentSampler() {
Expand Down Expand Up @@ -140,16 +143,22 @@ protected Map<String, String> getParameters() {
@Override
public void testStarted() {
executor = null; // NOPMD - Initialize executor
getExecutor().onStartTest();
}

@Override
public void testStarted(String host) {}
public void testStarted(String host) {
testStarted();
}

@Override
public void testEnded() {
getExecutor().onEndTest();
executor = null; // NOPMD - Initialize executor
}

@Override
public void testEnded(String host) {}
public void testEnded(String host) {
testEnded();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ public interface JavaTestElementExecutor extends Serializable {
* @return list of valid parameters, null to not check validity.
*/
List<String> getParametersKey();

/**
* Executed on Start Test.
*/
default void onStartTest() {}

/**
* Executed on End Test.
*/
default void onEndTest() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.anasoid.jmc.plugins.utils.ExecutorUtils;
import org.anasoid.jmc.plugins.wrapper.java.AbstractJmcPluginJavaTest;
import org.anasoid.jmc.plugins.wrapper.java.sampler.executor.TestJavaPostProcessorCheckWrapper;
import org.anasoid.jmc.plugins.wrapper.java.sampler.executor.TestJavaSamplerOnStartEndWrapper;
import org.anasoid.jmc.plugins.wrapper.java.sampler.executor.TestJavaSamplerWrapper;
import org.anasoid.jmc.plugins.wrapper.java.sampler.executor.TestJavaSamplerWrapperWithField;
import org.anasoid.jmc.test.log.LogMonitor;
Expand Down Expand Up @@ -37,6 +38,7 @@
*/

class AbstractJavaSamplerWrapperTest extends AbstractJmcPluginJavaTest {

private static final Logger LOG = LoggerFactory.getLogger(AbstractJavaSamplerWrapperTest.class);

// @Test
Expand Down Expand Up @@ -225,4 +227,58 @@ void testCheckContent() throws IOException {
.isPresent(),
"Errors : " + LogMonitor.getLogs().toString());
}

@Test
void testOnStartOnEnd() throws IOException {
TestJavaSamplerOnStartEndWrapper.initialize();
TestPlanWrapper testPlanWrapper =
TestPlanWrapper.builder()
.addThread(
ThreadGroupWrapper.builder()
.withLoops(2)
.withDuration(1)
.withNumThreads(2)
.addSampler(
TestJavaSamplerOnStartEndWrapper.builder()
.withName("testOnStartOnEnd")
.build())
.build())
.build();
ApplicationTest applicationTest = toApplicationTest(testPlanWrapper, "testOnStartOnEnd");
applicationTest.run();
Assertions.assertTrue(
TestJavaSamplerOnStartEndWrapper.ON_END);
Assertions.assertTrue(
TestJavaSamplerOnStartEndWrapper.ON_START);
}

@Test
void testOnStartOnEndMulti() throws IOException {
TestJavaSamplerOnStartEndWrapper.initialize();
TestPlanWrapper testPlanWrapper =
TestPlanWrapper.builder()
.addThread(
ThreadGroupWrapper.builder()
.withLoops(2)
.withDuration(1)
.withNumThreads(2)
.addSampler(
TestJavaSamplerOnStartEndWrapper.builder()
.withName("testOnStartOnEnd1")
.withNumberInstance(2)
.build())
.addSampler(
TestJavaSamplerOnStartEndWrapper.builder()
.withName("testOnStartOnEnd2")
.withNumberInstance(2)
.build())
.build())
.build();
ApplicationTest applicationTest = toApplicationTest(testPlanWrapper, "testOnStartOnEnd");
applicationTest.run();
Assertions.assertTrue(
TestJavaSamplerOnStartEndWrapper.ON_END);
Assertions.assertTrue(
TestJavaSamplerOnStartEndWrapper.ON_START);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.anasoid.jmc.plugins.wrapper.java.sampler.executor;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import lombok.Builder.Default;
import lombok.experimental.SuperBuilder;
import org.anasoid.jmc.plugins.wrapper.java.sampler.AbstractJavaSamplerWrapper;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterVariables;
import org.junit.jupiter.api.Assertions;
import org.slf4j.Logger;

/**
* test class.
*/
@SuperBuilder(setterPrefix = "with", toBuilder = true)
public class TestJavaSamplerOnStartEndWrapper extends AbstractJavaSamplerWrapper {

public static boolean ON_START;
public static boolean ON_END;

private static int COUNTER_START;
private static int COUNTER_END;
private int counterStart;
private int counterEnd;
@Default
private int numberInstance = 1;

/**
* init variable, should be executed before each test.
*/
public static void initialize() {
ON_START = false;
ON_END = false;
COUNTER_START = 0;
COUNTER_END = 0;
}

@Override
public String execute(
String label,
JMeterContext ctx,
JMeterVariables vars,
Properties props,
Map<String, String> parameters,
Logger log,
Sampler sampler,
SampleResult sampleResult) {

log.info("TestJavaSamplerOnStartEndWrapper : ######################ME#####################");
Assertions.assertNotNull(label);
Assertions.assertNotNull(ctx);
Assertions.assertNotNull(vars);
Assertions.assertNotNull(props);
Assertions.assertNotNull(parameters);
Assertions.assertNotNull(log);
Assertions.assertNotNull(sampler);
Assertions.assertNotNull(sampleResult);
return "content";
}

@Override
public List<String> getParametersKey() {
return null;
}


@Override
public void onStartTest() {
ON_START = true;

COUNTER_START++;
counterStart++;

if (COUNTER_START > numberInstance || COUNTER_START <= 0) {
throw new IllegalStateException("COUNTER_START already started : " + COUNTER_START);
}
if (counterStart != 1) {
throw new IllegalStateException("counterStart already started : " + counterStart);
}
}

@Override
public void onEndTest() {
ON_END = true;
COUNTER_END--;
counterEnd--;

if (COUNTER_END >= numberInstance || COUNTER_END < 0) {
throw new IllegalStateException("COUNTER_START already started : " + COUNTER_END);
}
if (counterEnd != 0) {
throw new IllegalStateException("Counter already Ended : " + counterEnd);
}
}
}

0 comments on commit b2e8cba

Please sign in to comment.