Session variable question

Hi. I’ve been using Gatling for the past week and I’m a bit stuck at what seems to be a simple issue. I’m new to scala.

I basically want to know if a session variable is set or not. The code I’m working on looks like this at the moment:

.exec((s: Session) =>
if (s.getAttribute(“myVariable”) != null)
println(“The variable is set”)
)

What can I change “!= null” with, to make this code print “The variable is set” when it is in fact set, and do nothing when it is not?

For context, this is my scenario: “Call A” precedes “Call B” and “Call B” is dependent on information in the response of “Call A”. I want to stop “Call B” from executing if “Call A” failed (or returned non useable data).

Sincerely
Espen Nersveen

Hi Espen,

As you can see here (https://github.com/excilys/gatling/blob/1.0-maintenance/gatling-core/src/main/scala/com/excilys/ebi/gatling/core/session/Session.scala#L63), the returned result of the getAttribute function if there is nothing found is EMPTY (or “”, the empty string) :slight_smile:

So you should test against “” instead:

if (s.getAttribute(“myVariable”) != “”)

However, I think this will change in 1.1, not sure though :wink:

Hope this helps :slight_smile:

Cheers,
Romain

Hi Espen,

Would it be possible for you to use the 1.1.0-SNAPSHOT (as explained in the thread I just opened)?

In 1.1.0, here’s what your function should look like :

.exec((s: Session) =>
if (s.isAttributeDefined(“myVariable”))
println(“The variable is set”)
s
)

Cheers,

Steph

2012/3/12 Espen Nersveen <espen.nersveen@gmail.com>

Thank you for a quick answer.

As for running 1.1.0-SNAPSHOT. I have a parallell task trying to get it to run on that version, but so far not completed. Some issues still remain on cookie and session handling. I will redirect my efforts on converting to 1.1.0-SNAPSHOT.

Thank you
Espen Nersveen

Thanks for the quick answer.

I have tried to check for an empty String and this is the result:
15:35:38.704 [akka:event-driven:dispatcher:global-11][WARN ] c.e.e.g.c.s.Session - No Matching Attribute for key: ‘myVariable’ in session

1.1 has another version, but I’m not able to run on 1.1 just yet. Some issues still remain.

Thank you
Espen Nersveen

Hi Espen,

The warning is not harmful and will not prevent you from running your simulations. The getAttribute method was intended for what it is named: getting the value of an attribute.

This is why an isAttributeDefined method has been created in 1.1 :slight_smile:

As for your concerns about migrating to 1.1, what issues are you talking about?

Cheers,
Romain

I have to delve a bit deeper into the debug-logged script to find out
what is wrong. I'll try to get it up and running on 1.1.0-SNAPSHOT now
and get back to you when I have more information. I will leave the
1.0.3 version for now.

Thanks for the help
espen