# Problem with testing bulk inserts with Task.async\_stream

**URL:** <https://forum.elixirforum.com/t/problem-with-testing-bulk-inserts-with-task-async-stream/21693>\
**Category:** Questions\
**Tags:** ecto, testing, asynchronous\
**Created:** [April 17, 2019, 3:33am UTC](https://forum.elixirforum.com/t/problem-with-testing-bulk-inserts-with-task-async-stream/21693 "2019-04-17T03:33:44Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![benwilson512](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/32/1457_2.png) [@benwilson512](https://forum.elixirforum.com/u/benwilson512)\
**Post date:** [April 17, 2019, 2:00pm UTC](https://forum.elixirforum.com/t/problem-with-testing-bulk-inserts-with-task-async-stream/21693/6 "2019-04-17T14:00:03Z")

</div>

Hey @ug0 this is because of how the ecto sandbox works during tests. Each test process is provided its own database connection. The `Task.async_stream` call spawns new processes, and ecto doesn’t know which connection to use. You can solve this by giving Ecto a little more information:

```elixir
stream = build_attrs_stream()

parent = self()

Repo.transaction(fn ->
  stream
  |> Stream.chunk_every(5000)
  |> Task.async_stream(fn batch ->
    {n, _} = Repo.insert_all(Assignment, batch, caller: parent)
    n
  end, ordered: false)
  |> Enum.reduce(0, fn {:ok, n}, acc -> acc + n end)
end)

```

The change here is the addition of `caller: parent` as options to the Repo call.

---

_[View the full topic](https://forum.elixirforum.com/t/problem-with-testing-bulk-inserts-with-task-async-stream/21693)._
