# Feeder reading multiple lines and foreach

**URL:** <https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947>\
**Category:** Gatling (Open-Source)\
**Created:** [May 15, 2023, 3:47pm UTC](https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947 "2023-05-15T15:47:48Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![oddfrost](https://avatars.discourse-cdn.com/v4/letter/o/5fc32e/32.png) [@oddfrost](https://community.gatling.io/u/oddfrost)\
**Post date:** [May 15, 2023, 3:47pm UTC](https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947/1 "2023-05-15T15:47:48Z")

</div>

Hello  
I’m trying to run foreach on multiple records of feeder but foreach is not executing and no errors are shown

```scala
  scenario("my scenario")
    .feed(myFeeder, 5) //read 5 lines as array?
    .foreach("#{columnName}", "attrName") { session => //iterate over array
       println(session("attrName").as[String])
       session
  }

```

But if i change code to repeat, everything works (so file could be read)

```scala
scenario("my scenario")
  .repeat(5) { // read 5 lines one by one
    feed(myFeeder)
      .exec { session =>
         println(session("columnName").as[String])
         session
       }
  }

```

With gatling v 3.7.6 i can see keys in session like “columnName1”, “columnName2”, etc…  
With gatling 3.9.1 or higher i can see key “columnName” but i don’t know its type so i cant do anything with it.

So what is the problem with combination like:

```scala
.feed(myFeeder, 5)
.foreach("#{columnName}", "attrName")

```

Different types, so foreach can’t iterate? or problem with feeder?  
csv file has enough lines to read.

---

<div class="post-metadata">

**Author:** ![GeMi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/gemi/32/939_2.png) [@GeMi](https://community.gatling.io/u/GeMi)\
**Post date:** [May 15, 2023, 5:01pm UTC](https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947/2 "2023-05-15T17:01:32Z")

</div>

Here you have a little hints: 🙂

- [https://gatling.io/docs/gatling/guides/grouping\_feeder/](https://gatling.io/docs/gatling/guides/grouping_feeder/)

- [Case0015UUIDfeederTwoRecordsAtTheSameTimeSimulation](https://github.com/gemiusz/gatling-examples-maven-java/blob/master/src/test/java/pl/gemiusz/Case0015UUIDfeederTwoRecordsAtTheSameTimeSimulation.java)

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [May 15, 2023, 5:03pm UTC](https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947/3 "2023-05-15T17:03:18Z")

</div>

> [@oddfrost](#):
>
> `.foreach("#{columnName}", "attrName") { session => //iterate over array`

This is not correct and has never been.  
The second parameter list of `foreach` takes a chain, not a function.

```scala
.foreach("#{columnName}", "attrName") {
  exec { session =>
    println(session("attrName").as[String])
    session
  }
}

```

---

<div class="post-metadata">

**Author:** ![oddfrost](https://avatars.discourse-cdn.com/v4/letter/o/5fc32e/32.png) [@oddfrost](https://community.gatling.io/u/oddfrost)\
**Post date:** [May 15, 2023, 5:36pm UTC](https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947/4 "2023-05-15T17:36:05Z")

</div>

Thank you for your reply, i’ve missed exec block when copied an example. My project compiles and runs but foreach don’t iterate over records of csv file.

---

<div class="post-metadata">

**Author:** ![oddfrost](https://avatars.discourse-cdn.com/v4/letter/o/5fc32e/32.png) [@oddfrost](https://community.gatling.io/u/oddfrost)\
**Post date:** [May 15, 2023, 5:42pm UTC](https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947/5 "2023-05-15T17:42:03Z")

</div>

Thank you. Example with UUID looks like what i want but with combination of gatling foreach.

Its not critical for me, i’ve used repeat instead.

I just want to know if i can use foreach for feeder records which were stored in array like [this](https://github.com/gemiusz/gatling-examples-maven-java/blob/35f6e77b6c85881647247b606c7981183e790740/src/test/java/pl/gemiusz/Case0015UUIDfeederTwoRecordsAtTheSameTimeSimulation.java#L32)

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [May 15, 2023, 6:22pm UTC](https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947/6 "2023-05-15T18:22:00Z")

</div>

Make sure to upgrade to the latest version (3.9.5 atm).  
If you still experience an issue, please build a simple yet complete reproducer someone else can run on his side.

---

<div class="post-metadata">

**Author:** ![oddfrost](https://avatars.discourse-cdn.com/v4/letter/o/5fc32e/32.png) [@oddfrost](https://community.gatling.io/u/oddfrost)\
**Post date:** [May 15, 2023, 7:25pm UTC](https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947/7 "2023-05-15T19:25:41Z")

</div>

Here is an [example](https://github.com/oddfrost/gatling-feeder-and-foreach/blob/main/src/test/scala/computerdatabase/ForEachSimulation.scala)

Found error in log:

> Condition evaluation crashed with message ‘Can’t cast ‘[Ljava.lang.Object;@7cafc344’ of type class [Ljava.lang.Object; into interface scala.collection.immutable.Seq’, exiting loop

---

<div class="post-metadata">

**Author:** ![GeMi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/gemi/32/939_2.png) [@GeMi](https://community.gatling.io/u/GeMi)\
**Post date:** [May 15, 2023, 8:37pm UTC](https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947/8 "2023-05-15T20:37:57Z")

</div>

@oddfrost  
below works for me without error 🙂

I wondering how to cast in better way `Object[]` to `ArrayList<String>` - any suggestions @slandelle ? 😏

PR with Solution: [Case0023foreachFromUUIDfeederFiveRecordsAtTheSameTimeSimulation by gemiusz · Pull Request #18 · gemiusz/gatling-examples-maven-java · GitHub](https://github.com/gemiusz/gatling-examples-maven-java/pull/18)  
Any comments and sugestions are welcome ♥

Direct Link to Simulation code:

> <https://github.com/gemiusz/gatling-examples-maven-java/blob/Case0023foreachFromUUIDfeederFiveRecordsAtTheSameTimeSimulation/src/test/java/pl/gemiusz/Case0023foreachFromUUIDfeederFiveRecordsAtTheSameTimeSimulation.java>

---

<div class="post-metadata">

**Author:** ![oddfrost](https://avatars.discourse-cdn.com/v4/letter/o/5fc32e/32.png) [@oddfrost](https://community.gatling.io/u/oddfrost)\
**Post date:** [May 15, 2023, 8:56pm UTC](https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947/9 "2023-05-15T20:56:39Z")

</div>

Yes, but putting objects from object array to ArrayList looks like a workaround. When working with api (gatling) expecting things to work without this step.  
Like reading lines and storing them directly in Seq or ArrayList not in Object

---

<div class="post-metadata">

**Author:** ![GeMi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/gemi/32/939_2.png) [@GeMi](https://community.gatling.io/u/GeMi)\
**Post date:** [May 15, 2023, 9:19pm UTC](https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947/10 "2023-05-15T21:19:33Z")

</div>

As I see in Gatling source code ([gatling/ForEach.java at main · gatling/gatling · GitHub](https://github.com/gatling/gatling/blob/main/gatling-core-java/src/main/java/io/gatling/javaapi/core/loop/ForEach.java)) there is `foreach` in almost all methods so there must be `List` 🙂

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [May 16, 2023, 11:03am UTC](https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947/11 "2023-05-16T11:03:54Z")

</div>

OK, this is actually a bug. Indeed, `foreach` doesn’t support arrays, only `Seq` and `List`. Investigating.

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [May 16, 2023, 3:48pm UTC](https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947/12 "2023-05-16T15:48:25Z")

</div>

Fixed: [Core: Make foreach support arrays · Issue #4420 · gatling/gatling · GitHub](https://github.com/gatling/gatling/issues/4420)

Should be released in 3.9.6 (no ETA at the moment).

---

<div class="post-metadata">

**Author:** ![GeMi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/gemi/32/939_2.png) [@GeMi](https://community.gatling.io/u/GeMi)\
**Post date:** [May 16, 2023, 4:18pm UTC](https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947/13 "2023-05-16T16:18:00Z")

</div>

Hi @slandelle,  
I have looked in Commit [Make foreach support arrays, close #4420 · gatling/gatling@1ae49cb · GitHub](https://github.com/gatling/gatling/commit/1ae49cb820e4f04e5ff006e453cd0818443e5ead)  
and I’m wondering why there aren’t any changes in JAVA code [gatling/ForEach.java at main · gatling/gatling · GitHub](https://github.com/gatling/gatling/blob/main/gatling-core-java/src/main/java/io/gatling/javaapi/core/loop/ForEach.java)  
How your changes will appile to Java code if there are only `List<?>`, `String`, `Function<Session, List<?>>` types that can be used ?

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [May 22, 2023, 4:05pm UTC](https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947/14 "2023-05-22T16:05:48Z")

</div>

Actually, I’m not satisfied with this solution.  
I’m going with [Core: Change multiple feed to generate either java.util.List or Scala Seq instead of Array (BREAKING CHANGE) · Issue #4423 · gatling/gatling · GitHub](https://github.com/gatling/gatling/issues/4423) instead.

Cheers

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/b/b148e0742de35c6280726cabf3ecd3de2d320f63.png) [@system](https://community.gatling.io/u/system)\
**Post date:** [June 21, 2023, 4:06pm UTC](https://community.gatling.io/t/feeder-reading-multiple-lines-and-foreach/7947/15 "2023-06-21T16:06:30Z")

</div>

Closed on June 21, 2023, 4:06pm UTC

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