Conversion from scala DSL to java DSL: different behaviour with findAll

257 views
Skip to first unread message

e3mg...@gmail.com

unread,
Dec 21, 2021, 2:55:28 PM12/21/21
to Gatling User Group
Hi gatling experts,
I've been using gatling for a while and now with 3.7 version I'd like to give a try to the new java DSL since I've always been struggling a bit with scala syntax.
So I've taken a project I did with gatling and I've tried to convert to use java DSL. I'm still in the process and testing if everything works as expected. Up to now I found a major difference in the checks using findAll function. Here's an example of such a check in Scala:

def selectProductCode(): HttpCheck = {
css("input[name='productCodePost']", "value").findAll.transform(random).optional.saveAs("productCode")
}

and the custom random function was this one:

val random = (list: Seq[String]) => {
list(Random.nextInt(list.size))
}  

and here how I translated them in java DSL

public static final CheckBuilder.Final selectProductCode() {
return css("input[name='productCodePost']", "value").findAll().transform(random).optional().saveAs("productCode");
}

and the random function became:

private static Function<List<String>, String> random = (final List<String> list) -> list.get(ThreadLocalRandom.current().nextInt(list.size()));

Now, I've noticed that when the css selector find some occurrences in the html the translated java version works fine as the scala one, but when the css selector find no occurrences of the search then on Scala everything is fine and the only effect is the "productCode" attribute not being stored in the session (as expected), while on java the check breaks with a NPE:

19:44:00.243 [DEBUG] i.g.h.e.r.DefaultStatsProcessor - Request 'product detail page - pdpTypeAJAX' failed for user 1: css((input[name='productCodePost'],Some(value))).findAll.transform.transformOption.noop extraction crashed: j.l.NullPointerException

I'm not sure if the NPE is happenng in my custom "random" function because I have no check on the list of elements or if the function should not be called at all because there's no list of elements and in fact the optional call after the transform had the purpose to optionally save the attribute if some value was retrieved.

Has anyone faced something similar with new java DSL?

Thanks
Michele

Stéphane LANDELLE

unread,
Dec 21, 2021, 6:26:29 PM12/21/21
to gat...@googlegroups.com
Please provide a reproducer we can run on our side so we can investigate.

--

Stéphane Landelle

Chief Technical Officer

   

slan...@gatling.io
gatling.io
   
facebook twitter linkedin 


--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gatling/b04776a9-f339-416e-900e-31ce6954bd6fn%40googlegroups.com.

Stéphane LANDELLE

unread,
Dec 22, 2021, 9:44:14 AM12/22/21
to gat...@googlegroups.com
Here's what I've done to try to reproduce your issue, to no avail:
public class Foo extends Simulation {

private static Function<List<String>, String> random = list -> list.get(ThreadLocalRandom.current().nextInt(list.size()));

ScenarioBuilder scn = scenario("scn")
.exec(http("Search")
.get("https://computer-database.gatling.io/computers?f=MacBook")
.check(css("a", "href").findAll().transform(random).optional().saveAs("url"))
).exec(session -> {
System.out.println(session.getString("url"));
return session;
});

{
setUp(scn.injectOpen(atOnceUsers(1)));
}
}
We can't investigate any further without you providing a reproducer.

--

Stéphane Landelle

Chief Technical Officer

   

slan...@gatling.io
gatling.io
   
facebook twitter linkedin 

e3mg...@gmail.com

unread,
Dec 22, 2021, 11:13:06 AM12/22/21
to Gatling User Group
Hi Stephane,
today preparing the SSCCE i realized that the problem happens also with the Scala version ( I wrongly assumed it was introduced with java DSL)...in fact the problem I think is just that my random function assume that the list that is passed is always populated but when the findAll of the css selector doesn't match anything the function is called  passing a null list and then the NPE happens when trying to call list.get. Solution was to check if list was not null, so the check didn't brake.
I guess you can reproduce it in your example with css("a","notexistingAttribute") and you should see the NPE.

Thanks

Stéphane LANDELLE

unread,
Dec 22, 2021, 12:11:56 PM12/22/21
to gat...@googlegroups.com
> i realized that the problem happens also with the Scala version

I don't think that's true (I've checked).
  • the Scala DSL has transform and transformOption. The former is only invoked when the check could capture something. The latter is always invoked.
  • the Java DSL only has transform that is always invoked, but the function can be invoked with a null parameter if the check could not capture anything.


--

Stéphane Landelle

Chief Technical Officer

   

slan...@gatling.io
gatling.io
   
facebook twitter linkedin 


Michele Gallina

unread,
Dec 22, 2021, 12:44:18 PM12/22/21
to gat...@googlegroups.com
I guess you're again right...I've messed up something with the execution before...I've attached the 2 reproducers (Scala and Java) and the Scala one is working fine even without the null check on random function. Instead the java one is failing with NPE and to fix it I had to perform the null check on random function.

You received this message because you are subscribed to a topic in the Google Groups "Gatling User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gatling/_sfCtE2ua2U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gatling+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gatling/CAJw%3DiaQpnXM64HZEts%2BVyc_LAQp9pekcydN8dVsrH0V%3DUYKA1A%40mail.gmail.com.
SSCCE.zip
Reply all
Reply to author
Forward
0 new messages