Foreach suggestion

Hello there,
I’m trying to use foreach for the first time, so I’ll try to explain the situation:

I have precondition that will create a number of simple users in json file. In the scenario I have the exec method that will register those number of users on the platform, and after the registration some simple codes are written by another exec method in the CSV file. I have another exec command that will try to buy a product on the platform, but I want to try it with each user from the CSV file.

So I found out that I can use foreach in this way:

.foreach(List.of("", "", ""), "code").on(
                                            .tryMax(2).on(                                                exec(buyProduct("users.csv)).pause(1, 10)).exitHereIfFailed()

So, it works fine, but only when I pass the exact number of empty strings in the List.of();

So I don’t know each time how many users I’ll have in the csv file, so I want to have some dynamic thing in this foreach in order to make it work anytime.

Maybe I’m doing something wrong, but this is the first time I’m using foreach :slight_smile:

P.S. I know that I have 3 users in the csv file so I provided 3 empty Strings in the foreach.

Thanks a lot in advance!


some simple codes are written by another exec method in the CSV file. I have another exec command … with each user from the CSV file.

This is a recurring topic.
This is a very bad technique.

Don’t use a file as a buffer between scenarios: use a thread-safe in-memory structure: ConcurrentLinkedQueue, ConcurrentHashMap, etc.

Don’t use a file as a means to pass data between simulations. Simulations should be independent units. Use multiple scenarios in the same simulation. And if you want to reuse scenarios in different simulations, move them out of the Simulation class.

Can’t say what your problem is without you providing a proper reproducer.
foreach traverses the full collection. Then, as you’re passing a value an not a function, what’s passed to foreach is the collection at the time of the Simulation instantiation. Depending on what you’re actually doing, this might differ from the collection at the time a virtual users enters the loop.

Thanks for a quick response.
I know that this is recurring topic, but initially, it was implemented in this way and now it costs a lot to reimplement entire logic.

Maybe in the future we will do it, but now I have to work with files.
I will try to fix it somehow.

Thanks again, cheers!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.