Bug 41612

Summary: Loop nested in IF behaves erraticly
Product: JMeter - Now in Github Reporter: Jim Donnellan <jdonnellan>
Component: MainAssignee: JMeter issues mailing list <issues>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 2.2   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Attachments: Suggested patch
Test plan which exposed the bug as described in the bug report

Description Jim Donnellan 2007-02-14 08:35:43 UTC
Fedora Core 6 running java version
java version "1.5.0_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)


I have a single thread group that is running 20 loops. Inside it has one If
Controller with the If conition (Math.random() < .3).  If I simply put a sampler
under the If, things go as expected: the sampler is run roughly 30% of the time
at random intervals. This is working perfectly.

What I want is that if the If condition is met, the sampler will be run 5 times,
then we continue through the thread group, and if there are more loops of the
thread group to run, loop through the thread group again.

I've tried this using Loop Controller, and the results were erratic. I created a
simplified test case with just the controllers and two Java samplers that echo
"Yo" and "Hello" respectively...

THREAD GROUP  L=20
|
|- IF CONTROLLER (Math.random() < .3.)
        |
        | -  "Yo"
        |
        |- LOOP CONTROLLER  = 5
                   |
                   |-- "hello"


This is what I get in terms of Yo's and Hello's  (Y and H):
Y Y H H Y Y Y H Y Y

What I should get is five hello's after each Yo. 


I also tried using a simple controller to group everything under the IF, a la

THREAD GROUP    L=20
|
|- IF CONTROLLER (Math.random() < .3.)
        |
        |-- SIMPLE
                 | -  "Yo"
                 |
                 |- LOOP CONTROLLER = 5
                            |
                            |-- "hello"


And got this in the results tree:
Y H H H H H Y H   then  no more

The first group looks good (5 Hellos for each Yo).  But that second one only has
one Hello, which isn't what I want.
Comment 1 Alf Hogemark 2007-05-11 04:13:16 UTC
Created attachment 20171 [details]
Suggested patch

The reason for the observed behaviour, is that currently, the condition on the
"if controller" is evaluated for each sample / loop iteration INSIDE the if
controller.

I think that is incorrect. I think the "if controller" should only evaluate
its' condition when the "if block" is entered, and if the condition is
evaluated to true, then all the samplers and controllers inside the "if block"
should be executed.

The suggested patch also contains a minor change to ThreadGroupGui, which sets
the name of the LoopController for the ThreadGroup, so that it is easier to
debug which controllers are executed. This change is not needed to fix this
bug, so if you do not like it, then just drop that part.
Comment 2 Alf Hogemark 2007-05-11 04:14:27 UTC
Created attachment 20172 [details]
Test plan which exposed the bug as described in the bug report
Comment 3 Alf Hogemark 2007-05-11 09:10:10 UTC
(In reply to comment #1)
> Created an attachment (id=20171) [edit]
> Suggested patch
> 
> The reason for the observed behaviour, is that currently, the condition on the
> "if controller" is evaluated for each sample / loop iteration INSIDE the if
> controller.
> 
> I think that is incorrect. I think the "if controller" should only evaluate
> its' condition when the "if block" is entered, and if the condition is
> evaluated to true, then all the samplers and controllers inside the "if block"
> should be executed.
> 
> The suggested patch also contains a minor change to ThreadGroupGui, which sets
> the name of the LoopController for the ThreadGroup, so that it is easier to
> debug which controllers are executed. This change is not needed to fix this
> bug, so if you do not like it, then just drop that part.

After thinking about it a bit more, I think I need to test having an IF
controller inside a Loop. I'm not sure that the current suggested patch, with
the "isFirst()" test is correct.
Comment 4 Alf Hogemark 2007-05-13 23:45:10 UTC
(In reply to comment #3)
> 
> After thinking about it a bit more, I think I need to test having an IF
> controller inside a Loop. I'm not sure that the current suggested patch, with
> the "isFirst()" test is correct.

I have tested more, and the suggested patch seems to be correct.
Comment 5 Sebb 2007-06-28 17:35:22 UTC
Patch applied (apart from GUI change) - fixed in SVN r551743.
Comment 6 The ASF infrastructure team 2022-09-24 20:37:38 UTC
This issue has been migrated to GitHub: https://github.com/apache/jmeter/issues/1864