AtomicInteger is 10?

When I create new instance of:
val deleteCounter = new AtomicInteger()

and then check its value, it is 10, but not 0. What could be the problem?

Can you post a minimal working example that reproduces the problem?

val deleteCounter = new AtomicInteger()
def incNumber = deleteCounter.getAndIncrement

def incNumber = { //Function takes deleteCounter and increments it for each session
exec((session: Session) => {
deleteCounter.getAndIncrement
session
})}

.exec(http(“HomePage”).get("/").check(regex("""<a wicket:id=“delete” href=".*(\W\d-\d.declarations-\d["""
+incNumber+ “”"]-delete)">Delete.
""").saveAs(“myUrl1”)).pause(3)

So this script goes to my home page and from the response fetches the page to go next. There is a list of options and I need every user to go on a different page. The regex is ok, I controlled it using regex tester. The page URL looks like:
…delete-1-dec…
…delete-2-dec… and so on.

So I put a function that increments this number for each session. The problem is that instead of “delete-1-dec” I get “delete-10-dec”.
And the second problem that it doesn’t increment at all.

Where you have this: “”“declarations-\d[”""+incNumber+ “”"]-delete""" your regex is matching a number with \d, then your AtomicInteger’s value. Assuming you’ve got a list that counts up from 0, the smallest value that will match this pattern when deleteCounter is 0 is 10. If you want to just match deleteCounter, get rid of the \d and it should be fine.

NB In the future, if you want to check the value of something, you can use something like this (using deleteCounter as an example, here)

.exec(session => {
println(deleteCounter)
session
})

That way you know for sure you’re seeing the real value and there isn’t a problem with some other code.

Thank you! That fixed it. But what about the incrementation. How can I define incNumber so it would increment for each session. At the moment every user clicks the same url (with the same counter)

val deleteCounter = new AtomicInteger()

.exec(http(“HomePage”).get("/").check(regex("""<a wicket:id=“delete” href=".*(\W\d-\d.declarations-\d["""
+session => deleteCounter.getAndIncrement + “”"]-delete)">Delete.
""").saveAs(“myUrl1”)).pause(3)

Should execute every time

This method looks a bit more complicated. I have these functions:
def getIt(requestTitle: String,
requestURL: io.gatling.core.session.Expression[String],
pause: Int,
pattern: String,
urlName: String) = {
exec(http(requestTitle).get(requestURL).check(regex(pattern).saveAs(urlName))).pause(pause)
}

def goHome (pattern: String, saveUrl: String) = {
getIt(“HomePage”, “/”, 2, pattern, saveUrl)
}

def goHomeDelete = {
goHome(
“”"<a wicket:id=“delete” href=".*(\W\d-\d.*declarations-"""

  • session => deleteCounter.getAndIncrement + “”"-delete)">Delete.*""",
    “deleteUrl1”
    )
    }

It doesn’t compile:
error: not a legal formal parameter

I set up the counter as a session attribute manually and it worked. However I also tried it and it didn’t work earlier. Could you please tell me what is the difference between:
.exec(session => session.set(“myCounter”, deleteCounter.getAndIncrement))

and
.exec((session: Session) => {
session.set(“myCounter”, deleteCounter.getAndIncrement)
session
})

Session is immutable, so in your second exec, when you call session.set(), it returns a new Session that you don’t do anything with, and then you’re returning the original Session.