Fl4m3Ph03n1x

Fl4m3Ph03n1x

Background

I have an OTP application that reads data from json files. Depending on what these files have, the application may or may not modify them with new data.

Problem

The challenge here arrives when I am creating integration/e2e tests. I want to test the entire flow.

Normally I have my fixtures in tests/fixtures/my_file.json or something amongst those lines. I want my tests to run with async: true, so this creates a problem:

  • because my fixture file is static, different processes cannot read/write to the fixture file without causing race condition issues and messing up other tests
  • i also have to forceably reset the fixture file every time I run a test
  • becasue I have to use async: false this will cause my tests to be slow

Possible solution

I could in theory, create a fixture file per test running. I would have to spice the file name with a seed (like a random number) and then have the process know in advance which file it has to use.

This sounds to me like overly compex and would force me to have to rewrite a good chunk of the application, since I am only using the normal config/test.exs to tell where the files are located, and as far as I know this cannot be dynamic (at least not to the level I require):

config :store,
  products: Path.expand("#{__DIR__}/../apps/store/test/fixtures/products.json") |> Path.split(),
  setup: Path.expand("#{__DIR__}/../apps/store/test/fixtures/setup.json") |> Path.split(),

Questions

Given that the only solution I can think of seems rather complex, I would like to know if:

  • is there a standard way in Elixir to achieve dynamic fixtures for each test process?
  • are there any libraries that can help?
  • can you recommend any material (blogs, videos, etc) that deal with problems similar to this?

Ideally I am looking for something like bypass, which allows to async: true as well.

Showing Posts 1 to 6

LostKobrakai

LostKobrakai

Are you really testing at the “application” level? Because you cannot have multiple instances of an application running at the same time on the beam, so the requirement to be able to use async true would be moot.

If you’re not testing this at the application level, but at the process level I’d suggest passing the paths to the files in question as part of their startup, not having them depend on global state like the application environment.

eahanson

eahanson

For tests that will be modifying a fixture file, maybe you could make a copy of the fixture file in a temp dir and have the test use that (ExUnit has a @tmp_dir tag).

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

Are you really testing at the “application” level? Because you cannot have multiple instances of an application running at the same time on the beam, so the requirement to be able to use async true would be moot.

To be more precise, I launch a Supervisor wich then launches other processes. Think of it like a small GenServer application (with a supervisor) that you can use with iex -S mix.

It does not use phoenix, nor any major frameworks.

If you’re not testing this at the application level, but at the process level I’d suggest passing the paths to the files in question as part of their startup, not having them depend on global state like the application environment.

For tests that will be modifying a fixture file, maybe you could make a copy of the fixture file in a temp dir and have the test use that (ExUnit has a @tmp_dir tag).

To my knowledge, I could combine these two approaches. This would effectively result in the proposed solution I described, I would pass the files as arguments, and because I am doing it this way I can use the ExUnit setup callback to automatically generate temporary files and use them for each process.

As mentioned however, the (very heavy) downside of this approach, would be that it requires me to re-write a considerable portion of the application.

D4no0

D4no0

That’s the correct approach, the only thing you need to personally ensure is that this is idempotent, hence why using a temporary folder/files is the way to go.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

That’s the correct approach, the only thing you need to personally ensure is that this is idempotent, hence why using a temporary folder/files is the way to go.

I understand that Ecto has a system under the hood where it creates mini-databases for each test running.

I am unsure on the details, but if it is true, Isn’t there a library that they use to this effect that could be applied to this scenario?

LostKobrakai

LostKobrakai

Yes, but with many processes involved you need to deal with Ecto.Adapters.SQL.Sandbox — Ecto SQL v3.12.1 as well. There’s a whole lot of machinery involved to make this injection of state happening (especially if it’s implicit). Caller lookups are the backbone of most mocking libraries in elixir as well.

Things will be way simpler if you don’t need the implicit part and explicitly provide configuration by passing it down the process tree / into processes. You can still do the following in your Application.start/2 callback to use config to provide those values when processes are started there.

children = [
   {Child, Application.get_env(…)}
]
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews