# AtomicInteger is 10?

**URL:** <https://community.gatling.io/t/atomicinteger-is-10/1199>\
**Category:** Gatling (Open-Source)\
**Created:** [July 22, 2014, 9:21am UTC](https://community.gatling.io/t/atomicinteger-is-10/1199 "2014-07-22T09:21:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrei\_Proskurin](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@Andrei\_Proskurin](https://community.gatling.io/u/Andrei_Proskurin)\
**Post date:** [July 22, 2014, 9:21am UTC](https://community.gatling.io/t/atomicinteger-is-10/1199/1 "2014-07-22T09:21:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Michelle\_Burrows](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@Michelle\_Burrows](https://community.gatling.io/u/Michelle_Burrows)\
**Post date:** [July 22, 2014, 12:14pm UTC](https://community.gatling.io/t/atomicinteger-is-10/1199/2 "2014-07-22T12:14:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Andrei\_Proskurin](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@Andrei\_Proskurin](https://community.gatling.io/u/Andrei_Proskurin)\
**Post date:** [July 22, 2014, 12:37pm UTC](https://community.gatling.io/t/atomicinteger-is-10/1199/3 "2014-07-22T12:37:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michelle\_Burrows](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@Michelle\_Burrows](https://community.gatling.io/u/Michelle_Burrows)\
**Post date:** [July 22, 2014, 12:46pm UTC](https://community.gatling.io/t/atomicinteger-is-10/1199/4 "2014-07-22T12:46:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michelle\_Burrows](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@Michelle\_Burrows](https://community.gatling.io/u/Michelle_Burrows)\
**Post date:** [July 22, 2014, 12:49pm UTC](https://community.gatling.io/t/atomicinteger-is-10/1199/5 "2014-07-22T12:49:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Andrei\_Proskurin](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@Andrei\_Proskurin](https://community.gatling.io/u/Andrei_Proskurin)\
**Post date:** [July 22, 2014, 1:00pm UTC](https://community.gatling.io/t/atomicinteger-is-10/1199/6 "2014-07-22T13:00:00Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Michelle\_Burrows](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@Michelle\_Burrows](https://community.gatling.io/u/Michelle_Burrows)\
**Post date:** [July 22, 2014, 1:30pm UTC](https://community.gatling.io/t/atomicinteger-is-10/1199/7 "2014-07-22T13:30:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Andrei\_Proskurin](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@Andrei\_Proskurin](https://community.gatling.io/u/Andrei_Proskurin)\
**Post date:** [July 22, 2014, 1:37pm UTC](https://community.gatling.io/t/atomicinteger-is-10/1199/8 "2014-07-22T13:37:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Andrei\_Proskurin](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@Andrei\_Proskurin](https://community.gatling.io/u/Andrei_Proskurin)\
**Post date:** [July 22, 2014, 2:07pm UTC](https://community.gatling.io/t/atomicinteger-is-10/1199/9 "2014-07-22T14:07:55Z")

</div>

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  
})

---

<div class="post-metadata">

**Author:** ![Michelle\_Burrows](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@Michelle\_Burrows](https://community.gatling.io/u/Michelle_Burrows)\
**Post date:** [July 22, 2014, 5:16pm UTC](https://community.gatling.io/t/atomicinteger-is-10/1199/10 "2014-07-22T17:16:02Z")

</div>

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.
