Gatling 3.0 - How to break out of foreach loop?

Hello,

I’ve already done my research and it seems there’s no way to break out of a Gatling foreach() loop. I’m looking for direction on how to implement this appropriately in Gatling EL.

My objective
I’d like to run a foreach() loop over a Map of Clients (json results), and break out of the foreach loop once it finds a client who has Documents, and iterate over the “allClients” array until it does find a client with documents.

The problem:
The foreach(“allClients”) loop keeps on running, and moving onto the next client, and the asLongAs() condition is never checked. I need to stop the foreach(“allClients”) once it hits a client with documents, and thus asLongAs() condition fails and stops.

Pseudocode
The code below is conceptually how my code works.

asLongAs(session.contains(“clientFoundWithDocuments”) == false) {

foreach(“allClients”, “clientId”) {

var allDocuments = get("/get/documents/{$clientId}")

if(allDocuments.size > 0) {

session.set(“clientFoundWithDocuments”, “true”) ←

foreach(“allDocuments”, as “documentId”) {
get("/get/document/${documentId}")
}
}
}
}

Actual code:
https://gist.github.com/dragoonis/9f89a6f842a07b9799ac480c2a992c84

Many thanks,
Paul

Hi Stephane,

I still need support on knowing how to tweak my Gatling EL so that I can break out of my foreach(clients as client) loop, once I find a client with documents.

Can you help, please? This is blocking me delivering this work.

Many thanks,
Paul

I think you could use some “doIf” condition inside foreach loop. When you find a client with documents, you set some variable value that will not match your condition in doIf, and all the next iterations will not do anything.
foreach(…){
doIf(…){
//fire request
//if you’ve found what you need, e.g. use exec {} block to set some variable value, so that doIf condition will not be met in the next loops of foreach
}
}