Search in sources :

Example 16 with Sampler

use of org.apache.jmeter.samplers.Sampler in project jmeter by apache.

the class TestIfController method testEvaluateAllChildrenWithoutSubController.

/**
     * Test false return on sample3 (sample4 doesn't execute)
     * 
     * @throws Exception
     *             if something fails
     */
@Test
public void testEvaluateAllChildrenWithoutSubController() throws Exception {
    LoopController controller = new LoopController();
    controller.setLoops(2);
    controller.addTestElement(new TestSampler("Sample1"));
    IfController ifCont = new IfController("true==true");
    ifCont.setEvaluateAll(true);
    controller.addTestElement(ifCont);
    ifCont.addTestElement(new TestSampler("Sample2"));
    TestSampler sample3 = new TestSampler("Sample3");
    ifCont.addTestElement(sample3);
    TestSampler sample4 = new TestSampler("Sample4");
    ifCont.addTestElement(sample4);
    String[] order = new String[] { "Sample1", "Sample2", "Sample3", "Sample1", "Sample2", "Sample3" };
    int counter = 0;
    controller.initialize();
    controller.setRunningVersion(true);
    ifCont.setRunningVersion(true);
    Sampler sampler = null;
    while ((sampler = controller.next()) != null) {
        sampler.sample(null);
        if (sampler.getName().equals("Sample3")) {
            ifCont.setCondition("true==false");
        }
        assertEquals(order[counter], sampler.getName());
        counter++;
    }
    assertEquals(counter, 6);
}
Also used : DebugSampler(org.apache.jmeter.sampler.DebugSampler) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Sampler(org.apache.jmeter.samplers.Sampler) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Test(org.junit.Test)

Example 17 with Sampler

use of org.apache.jmeter.samplers.Sampler in project jmeter by apache.

the class TestIfController method testStackOverflow.

/**
     * See Bug 56160
     * 
     * @throws Exception
     *             if something fails
     */
@Test
public void testStackOverflow() throws Exception {
    LoopController controller = new LoopController();
    controller.setLoops(1);
    controller.setContinueForever(false);
    IfController ifCont = new IfController("true==false");
    ifCont.setUseExpression(false);
    ifCont.setEvaluateAll(false);
    WhileController whileController = new WhileController();
    whileController.setCondition("${__javaScript(\"true\" != \"false\")}");
    whileController.addTestElement(new TestSampler("Sample1"));
    controller.addTestElement(ifCont);
    ifCont.addTestElement(whileController);
    Sampler sampler = null;
    int counter = 0;
    controller.initialize();
    controller.setRunningVersion(true);
    ifCont.setRunningVersion(true);
    whileController.setRunningVersion(true);
    try {
        while ((sampler = controller.next()) != null) {
            sampler.sample(null);
            counter++;
        }
        assertEquals(0, counter);
    } catch (StackOverflowError e) {
        fail("Stackoverflow occurred in testStackOverflow");
    }
}
Also used : DebugSampler(org.apache.jmeter.sampler.DebugSampler) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Sampler(org.apache.jmeter.samplers.Sampler) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Test(org.junit.Test)

Example 18 with Sampler

use of org.apache.jmeter.samplers.Sampler in project jmeter by apache.

the class TestIfController method testProcessingTrue.

@Test
public void testProcessingTrue() throws Exception {
    LoopController controller = new LoopController();
    controller.setLoops(2);
    controller.addTestElement(new TestSampler("Sample1"));
    IfController ifCont = new IfController("true==true");
    ifCont.setEvaluateAll(true);
    ifCont.addTestElement(new TestSampler("Sample2"));
    TestSampler sample3 = new TestSampler("Sample3");
    ifCont.addTestElement(sample3);
    controller.addTestElement(ifCont);
    String[] order = new String[] { "Sample1", "Sample2", "Sample3", "Sample1", "Sample2", "Sample3" };
    int counter = 0;
    controller.initialize();
    controller.setRunningVersion(true);
    ifCont.setRunningVersion(true);
    Sampler sampler = null;
    while ((sampler = controller.next()) != null) {
        sampler.sample(null);
        assertEquals(order[counter], sampler.getName());
        counter++;
    }
    assertEquals(counter, 6);
}
Also used : DebugSampler(org.apache.jmeter.sampler.DebugSampler) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Sampler(org.apache.jmeter.samplers.Sampler) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Test(org.junit.Test)

Example 19 with Sampler

use of org.apache.jmeter.samplers.Sampler in project jmeter by apache.

the class TimeShift method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    String dateString;
    LocalDateTime localDateTimeToShift = LocalDateTime.now(systemDefaultZoneID);
    DateTimeFormatter formatter = null;
    if (!StringUtils.isEmpty(format)) {
        try {
            formatter = dateTimeFormatterCache.get(format, key -> createFormatter((String) key));
        } catch (IllegalArgumentException ex) {
            // $NON-NLS-1$
            log.error("Format date pattern '{}' is invalid (see https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html)", format, ex);
            return "";
        }
    }
    if (!dateToShift.isEmpty()) {
        try {
            if (formatter != null) {
                localDateTimeToShift = LocalDateTime.parse(dateToShift, formatter);
            } else {
                localDateTimeToShift = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(dateToShift)), ZoneId.systemDefault());
            }
        } catch (DateTimeParseException | NumberFormatException ex) {
            // $NON-NLS-1$
            log.error("Failed to parse the date '{}' to shift", dateToShift, ex);
        }
    }
    // Check amount value to shift
    if (!StringUtils.isEmpty(amountToShift)) {
        try {
            Duration duration = Duration.parse(amountToShift);
            localDateTimeToShift = localDateTimeToShift.plus(duration);
        } catch (DateTimeParseException ex) {
            // $NON-NLS-1$
            log.error("Failed to parse the amount duration '{}' to shift (see https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#parse-java.lang.CharSequence-) ", amountToShift, ex);
        }
    }
    if (formatter != null) {
        dateString = localDateTimeToShift.format(formatter);
    } else {
        ZoneOffset offset = ZoneOffset.systemDefault().getRules().getOffset(localDateTimeToShift);
        dateString = String.valueOf(localDateTimeToShift.toInstant(offset).toEpochMilli());
    }
    if (!StringUtils.isEmpty(variableName)) {
        JMeterVariables vars = getVariables();
        if (vars != null) {
            // vars will be null on TestPlan
            vars.put(variableName, dateString);
        }
    }
    return dateString;
}
Also used : LocalDateTime(java.time.LocalDateTime) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) ChronoField(java.time.temporal.ChronoField) JMeterUtils(org.apache.jmeter.util.JMeterUtils) Arrays(java.util.Arrays) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) Logger(org.slf4j.Logger) Collection(java.util.Collection) LocalDateTime(java.time.LocalDateTime) LoggerFactory(org.slf4j.LoggerFactory) SampleResult(org.apache.jmeter.samplers.SampleResult) Instant(java.time.Instant) Cache(com.github.benmanes.caffeine.cache.Cache) StringUtils(org.apache.commons.lang3.StringUtils) ZoneId(java.time.ZoneId) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) Year(java.time.Year) Duration(java.time.Duration) DateTimeFormatter(java.time.format.DateTimeFormatter) ZoneOffset(java.time.ZoneOffset) Sampler(org.apache.jmeter.samplers.Sampler) DateTimeParseException(java.time.format.DateTimeParseException) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) Duration(java.time.Duration) DateTimeFormatter(java.time.format.DateTimeFormatter) ZoneOffset(java.time.ZoneOffset)

Example 20 with Sampler

use of org.apache.jmeter.samplers.Sampler in project jmeter by apache.

the class TestURLRewritingModifier method testNonHTTPSampler.

@Test
public void testNonHTTPSampler() throws Exception {
    Sampler sampler = new NullSampler();
    response = new SampleResult();
    context.setCurrentSampler(sampler);
    context.setPreviousResult(response);
    mod.process();
}
Also used : HTTPNullSampler(org.apache.jmeter.protocol.http.sampler.HTTPNullSampler) NullSampler(org.apache.jmeter.samplers.NullSampler) Sampler(org.apache.jmeter.samplers.Sampler) SampleResult(org.apache.jmeter.samplers.SampleResult) HTTPNullSampler(org.apache.jmeter.protocol.http.sampler.HTTPNullSampler) NullSampler(org.apache.jmeter.samplers.NullSampler) Test(org.junit.Test)

Aggregations

Sampler (org.apache.jmeter.samplers.Sampler)33 SampleResult (org.apache.jmeter.samplers.SampleResult)11 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)9 JMeterContext (org.apache.jmeter.threads.JMeterContext)9 Test (org.junit.Test)9 TransactionSampler (org.apache.jmeter.control.TransactionSampler)6 DebugSampler (org.apache.jmeter.sampler.DebugSampler)6 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)5 Controller (org.apache.jmeter.control.Controller)3 HTTPSamplerBase (org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase)3 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)3 JMeterStopTestException (org.apache.jorphan.util.JMeterStopTestException)3 JMeterStopTestNowException (org.apache.jorphan.util.JMeterStopTestNowException)3 JMeterStopThreadException (org.apache.jorphan.util.JMeterStopThreadException)3 Logger (org.slf4j.Logger)3 Properties (java.util.Properties)2 Argument (org.apache.jmeter.config.Argument)2 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)2 TestElement (org.apache.jmeter.testelement.TestElement)2 Cache (com.github.benmanes.caffeine.cache.Cache)1