Search in sources :

Example 26 with TestSampler

use of org.apache.jmeter.junit.stubs.TestSampler in project jmeter by apache.

the class TestRandomOrderController method testRandomOrder.

@Test
public void testRandomOrder() {
    testLog.debug("Testing RandomOrderController");
    RandomOrderController roc = new RandomOrderController();
    roc.addTestElement(new TestSampler("zero"));
    roc.addTestElement(new TestSampler("one"));
    roc.addTestElement(new TestSampler("two"));
    roc.addTestElement(new TestSampler("three"));
    TestElement sampler = null;
    List<String> usedSamplers = new ArrayList<>();
    roc.initialize();
    while ((sampler = roc.next()) != null) {
        String samplerName = sampler.getName();
        if (usedSamplers.contains(samplerName)) {
            assertTrue("Duplicate sampler returned from next()", false);
        }
        usedSamplers.add(samplerName);
    }
    assertEquals("All samplers were returned", 4, usedSamplers.size());
}
Also used : ArrayList(java.util.ArrayList) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) TestElement(org.apache.jmeter.testelement.TestElement) Test(org.junit.Test)

Example 27 with TestSampler

use of org.apache.jmeter.junit.stubs.TestSampler in project jmeter by apache.

the class TestRunTime method testProcessing.

@Test
public void testProcessing() throws Exception {
    RunTime controller = new RunTime();
    controller.setRuntime(10);
    TestSampler samp1 = new TestSampler("Sample 1", 500);
    TestSampler samp2 = new TestSampler("Sample 2", 480);
    LoopController sub1 = new LoopController();
    sub1.setLoops(2);
    sub1.setContinueForever(false);
    sub1.addTestElement(samp1);
    LoopController sub2 = new LoopController();
    sub2.setLoops(40);
    sub2.setContinueForever(false);
    sub2.addTestElement(samp2);
    controller.addTestElement(sub1);
    controller.addTestElement(sub2);
    controller.setRunningVersion(true);
    sub1.setRunningVersion(true);
    sub2.setRunningVersion(true);
    controller.initialize();
    Sampler sampler = null;
    int loops = 0;
    long now = System.currentTimeMillis();
    while ((sampler = controller.next()) != null) {
        loops++;
        sampler.sample(null);
    }
    long elapsed = System.currentTimeMillis() - now;
    assertTrue("Should be at least 20 loops " + loops, loops >= 20);
    assertTrue("Should be fewer than 30 loops " + loops, loops < 30);
    assertTrue("Should take at least 10 seconds " + elapsed, elapsed >= 10000);
    assertTrue("Should take less than 12 seconds " + elapsed, elapsed <= 12000);
    assertEquals("Sampler 1 should run 2 times", 2, samp1.getSamples());
    assertTrue("Sampler 2 should run >= 18 times", samp2.getSamples() >= 18);
}
Also used : 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 28 with TestSampler

use of org.apache.jmeter.junit.stubs.TestSampler in project jmeter by apache.

the class TestSwitchController method runTest2.

/*
         * Test: 
         * Before 
         * Selection Controller
         *  - zero (default)
         *  - simple controller 1
         *  - - one
         *  - - two
         *  - simple controller 2
         *  - - three
         *  - - four
         *  - five
         *  - six
         * After
         * 
         * cond  = Switch condition 
         * exp[] = expected results
         */
private void runTest2(String cond, String[] exp) throws Exception {
    int loops = 3;
    LoopController controller = new LoopController();
    controller.setLoops(loops);
    controller.setContinueForever(false);
    GenericController sub_1 = new GenericController();
    GenericController sub_2 = new GenericController();
    SwitchController switch_cont = new SwitchController();
    switch_cont.setSelection(cond);
    switch_cont.addTestElement(new TestSampler("zero"));
    switch_cont.addTestElement(sub_1);
    sub_1.addTestElement(new TestSampler("one"));
    sub_1.addTestElement(new TestSampler("two"));
    switch_cont.addTestElement(sub_2);
    sub_2.addTestElement(new TestSampler("three"));
    sub_2.addTestElement(new TestSampler("four"));
    switch_cont.addTestElement(new TestSampler("five"));
    switch_cont.addTestElement(new TestSampler("six"));
    controller.addTestElement(new TestSampler("before"));
    controller.addTestElement(switch_cont);
    controller.addTestElement(new TestSampler("after"));
    controller.setRunningVersion(true);
    sub_1.setRunningVersion(true);
    sub_2.setRunningVersion(true);
    switch_cont.setRunningVersion(true);
    controller.initialize();
    for (int i = 1; i <= 3; i++) {
        assertEquals("Loop:" + i, "before", nextName(controller));
        for (String anExp : exp) {
            assertEquals("Loop:" + i, anExp, nextName(controller));
        }
        assertEquals("Loop:" + i, "after", nextName(controller));
    }
    assertNull("Loops:" + loops, nextName(controller));
}
Also used : TestSampler(org.apache.jmeter.junit.stubs.TestSampler)

Example 29 with TestSampler

use of org.apache.jmeter.junit.stubs.TestSampler in project jmeter by apache.

the class TestSwitchController method runSimpleTest2.

// Selection controller with two sub-controllers, but each has only 1
// child
/*
         * Controller
         * + Before
         * + Switch (cond)
         * + + zero
         * + + Controller sub_1
         * + + + one
         * + + two
         * + + Controller sub_2
         * + + + three
         * + After
         */
private void runSimpleTest2(String cond, String exp, String sub1Name) throws Exception {
    GenericController controller = new GenericController();
    GenericController sub_1 = new GenericController();
    GenericController sub_2 = new GenericController();
    SwitchController switch_cont = new SwitchController();
    switch_cont.setSelection(cond);
    switch_cont.addTestElement(new TestSampler("zero"));
    switch_cont.addTestElement(sub_1);
    sub_1.addTestElement(new TestSampler("one"));
    sub_1.setName(sub1Name);
    switch_cont.addTestElement(new TestSampler("two"));
    switch_cont.addTestElement(sub_2);
    sub_2.addTestElement(new TestSampler("three"));
    sub_2.setName("three");
    controller.addTestElement(new TestSampler("before"));
    controller.addTestElement(switch_cont);
    controller.addTestElement(new TestSampler("after"));
    controller.initialize();
    for (int i = 1; i <= 3; i++) {
        assertEquals("Loop=" + i, "before", nextName(controller));
        if (exp != null) {
            assertEquals("Loop=" + i, exp, nextName(controller));
        }
        assertEquals("Loop=" + i, "after", nextName(controller));
        assertNull("Loop=" + i, nextName(controller));
    }
}
Also used : TestSampler(org.apache.jmeter.junit.stubs.TestSampler)

Example 30 with TestSampler

use of org.apache.jmeter.junit.stubs.TestSampler in project jmeter by apache.

the class TestSwitchController method testFunction.

/*
         * N.B. Requires ApacheJMeter_functions.jar to be on the classpath,
         * otherwise the function cannot be resolved.
        */
@Test
public void testFunction() throws Exception {
    JMeterContext jmctx = JMeterContextService.getContext();
    Map<String, String> variables = new HashMap<>();
    ReplaceStringWithFunctions transformer = new ReplaceStringWithFunctions(new CompoundVariable(), variables);
    jmctx.setVariables(new JMeterVariables());
    JMeterVariables jmvars = jmctx.getVariables();
    jmvars.put("VAR", "100");
    StringProperty prop = new StringProperty(SwitchController.SWITCH_VALUE, "${__counter(TRUE,VAR)}");
    JMeterProperty newProp = transformer.transformValue(prop);
    newProp.setRunningVersion(true);
    GenericController controller = new GenericController();
    SwitchController switch_cont = new SwitchController();
    switch_cont.setProperty(newProp);
    controller.addTestElement(new TestSampler("before"));
    controller.addTestElement(switch_cont);
    switch_cont.addTestElement(new TestSampler("0"));
    switch_cont.addTestElement(new TestSampler("1"));
    switch_cont.addTestElement(new TestSampler("2"));
    switch_cont.addTestElement(new TestSampler("3"));
    controller.addTestElement(new TestSampler("after"));
    controller.initialize();
    assertEquals("100", jmvars.get("VAR"));
    for (int i = 1; i <= 3; i++) {
        assertEquals("Loop " + i, "before", nextName(controller));
        assertEquals("Loop " + i, "" + i, nextName(controller));
        assertEquals("Loop " + i, "" + i, jmvars.get("VAR"));
        assertEquals("Loop " + i, "after", nextName(controller));
        assertNull(nextName(controller));
    }
    int i = 4;
    assertEquals("Loop " + i, "before", nextName(controller));
    assertEquals("Loop " + i, "0", nextName(controller));
    assertEquals("Loop " + i, "" + i, jmvars.get("VAR"));
    assertEquals("Loop " + i, "after", nextName(controller));
    assertNull(nextName(controller));
    assertEquals("4", jmvars.get("VAR"));
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) ReplaceStringWithFunctions(org.apache.jmeter.engine.util.ReplaceStringWithFunctions) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) HashMap(java.util.HashMap) StringProperty(org.apache.jmeter.testelement.property.StringProperty) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Test(org.junit.Test)

Aggregations

TestSampler (org.apache.jmeter.junit.stubs.TestSampler)43 Test (org.junit.Test)36 TestElement (org.apache.jmeter.testelement.TestElement)21 Sampler (org.apache.jmeter.samplers.Sampler)7 DebugSampler (org.apache.jmeter.sampler.DebugSampler)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)2 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)2 ReplaceStringWithFunctions (org.apache.jmeter.engine.util.ReplaceStringWithFunctions)2 ValueReplacer (org.apache.jmeter.engine.util.ValueReplacer)2 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)2 StringProperty (org.apache.jmeter.testelement.property.StringProperty)2 JMeterContext (org.apache.jmeter.threads.JMeterContext)2 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)2