apache / jmeter

Apache JMeter open-source load testing tool for analyzing and measuring the performance of a variety of services
https://jmeter.apache.org/
Apache License 2.0
8.19k stars 2.07k forks source link

If controller doesn't evaluate all RegEx booleans #5435

Closed asfimport closed 3 years ago

asfimport commented 3 years ago

eR@SeR (Bug 64924): Hello,

I had a case where I wanted to execute child elements in If controller using RegEx, to avoid mentioning several times vars.get("variable") method including logical operator "||". Simply I put ${__groovy(vars.get("variable") =~ /[1235]/,)} in If controller, but it doesn't enter inside if the variable is matched. Is this a bug? Note that groovy regex works just fine in JSR223 elements - If/else branch.

However, following examples are evaluated properly in If controller:

  1. ${__groovy(vars.get("variable") ==~ /[1235]/,)} (matches entire string - this works though)
  2. ${__groovy(boolean foundMatch = vars.get("variable") =~ /[1235]/,)} (matches part of string)
  3. ${__groovy(boolean foundMatch = vars.get("variable") ==~ /[1235]/,)} (matches entire string)
  4. ${__groovy(vars.get("variable").matches(/[1235]/),)} (matches entire string)

You can run example jmx and see the results.

Created attachment regexInIfController.jmx: regexInIfController.jmx

Severity: normal OS: All Resolution: WORKSFORME

asfimport commented 3 years ago

@FSchumacher (migrated from Bugzilla): Thanks for the interesting note on the behaviour of the regex matching in Groovy.

The =~ operator in Groovy returns either an instance of java.util.regex.Matcher or null. All your other examples are returning boolean values. The If Controller expects a boolean result and therefore does not work on the =~ operator (but on all the other ways you showed).

You could use Groovy to coerce the Matcher into a boolean value by using

__groovy(!!(vars.get("variable") =~ /[1-5]/))

I am not sure, we should do something here, as the Include Controller states, that it expects a boolean result and the Groovy documentation shows =~ returns a Matcher.

If we want to change the behaviour, we would have to think about the thruthiness of things, which most likely means something different to everyone. The discussion should probably be started on the dev mailing list.

asfimport commented 3 years ago

eR@SeR (migrated from Bugzilla): Hello,

You're welcome.

Thank you for consideration of this issue and showing a workaround to problematic boolean evaluation. It would be nice if it can be fixed. Thanks.

asfimport commented 3 years ago

@pmouawad (migrated from Bugzilla): Hello, This is the documented behaviour of IfController and cannot be changed:

https://jmeter.apache.org/usermanual/component_reference.html#If_Controller

It's not a bug.

So I am closing the ticket.

Thanks for report and feedback, always appreciated Regards

asfimport commented 3 years ago

eR@SeR (migrated from Bugzilla): (In reply to Philippe Mouawad from comment 3)

Hello, This is the documented behaviour of IfController and cannot be changed:

https://jmeter.apache.org/usermanual/component_reference.html#If_Controller

It's not a bug.

So I am closing the ticket.

Thanks for report and feedback, always appreciated Regards

OK, thank you. Probably I was misunderstood. When I said "It would be nice if it can be fixed." I meant that "returns a Matcher" would be changed/fixed to return a boolean, not to change If controller behavior to allow "Matcher".

If this cannot be fixed, then it is totally fine for me, just to be sure that everything is clear :)

Regards