kamaroly
Testing with Ash authentication is extremely slow even after configuring `config :bcrypt_elixir, :log_rounds, 1`
I have been experiencing extremely slow tests for each test behind a protected route with Ash authentication. 54 tests are taking 47 seconds. I tried various tips available online including config :bcrypt_elixir, :log_rounds, 1 but it didn’t help. Meanwhile others are ble to run 376 tests in 7 seconds. How do you speed up your tests?
376 tests in 7 seconds: Anthony Accomazzo on X: "@elixirlang `make signoff` in action: https://t.co/EqDWRGaTsp" / X
My experience:
Marked As Solved
kamaroly
Here is what I did to reduce the testing time.
I created a fake hash provider to skip the bycrypt hashing in while testing. This improved test speed to over 50%.
defmodule MyApp.Accounts.User.HashProviders.TestHashProvider do
@behaviour AshAuthentication.HashProvider
@impl true
def hash(input) when is_binary(input) do
# Fake hashing logic: prefix the input with "fake_hash_"
{:ok, "fake_hash_" <> input}
end
@impl true
def valid?(input, "fake_hash_" <> input), do: true
def valid?(_input, _hash), do: true
@impl true
def simulate do
# For testing purposes, return false to simulate a hash check
false
end
end
Then, I made it configurable in config/test.exs like the following
# Do NOT set this value for production
config :ash_authentication, hash_provider: MyApp.Accounts.User.HashProviders.TestHashProvider
- I told User resource to pick the hash provider from configuration if available like the following
# /lib/my_app/accounts/user.ex
defmodule MyApp.Accounts.User do
#...
authentication do
strategies do
password :password do
identity_field :email
# Tell AshAuthentication to use a faker hash provider to speed up tests
hash_provider Application.compile_env(
:ash_authentication,
:hash_provider,
AshAuthentication.BcryptProvider
)
end
end
#...
end
Also Liked
zachdaniel
@jimsynz may have some thoughts on speeding that process up, but I believe that most people are not actually doing password authentication in the vast majority of cases.
They will simply generate a token for the user and set it into the conn, for example, as opposed to first going through authentication flow.
zachdaniel
There is a setup_all that you can run at the start of each test to ensure your test user is created. It doesn’t happen in the sandbox, so won’t be rolled back on each test. So each test could check first if the user exists and create it otherwise, or do an upsert into the user’s table.
And your config :bcrypt_elixir, log_rounds: 1 is in config/test.exs?
zachdaniel
you can do that for each test using setup_all, or for all tests in your test_helper file IIRC.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










