fireproofsocks

fireproofsocks

Streams in Tests: strange fails

I am building a Phoenix app with session-based authentication. Sessions are stored in the database (for reporting/visibility/persistence) AND in ETS via Cachex. I’m working through the code that needs to run on startup that queries the database for all active sessions and loads that data into cache. I thought this would be a good opportunity to explore using streams (and tasks + async).

I came up with the following code that works in the iel shell:

import Ecto.Query, warn: false
alias Auth.Schemas.Session
alias Auth.Contexts.SessionContext
alias Auth.Repo

now = Timex.now()
query =
from s in Session,
     where: s.expires_at > ^now,
     select: s
stream = Repo.stream(query)

Repo.transaction(fn() ->
  Enum.to_list(stream)
  |> Enum.reduce(fn s, _acc ->
              SessionContext.cache_session(s)
              IO.puts("Session caching #{s.id}")
              end)
end)

That works! And I can query Cachex on the various session IDs and it returns what I would expect, i.e. SessionContext.get_from_cache("some-session-id-here") returns an :ok with my session data.

However… when I try to test that in my ExUnit tests, I get a failure. I suspect it has something to do with threads and processes and however those get spawned when running mix test, but honestly, that’s only a guess. I’ve noticed that sometimes when a function writes data to both PostGres AND to Cachex, sometimes the test can only see one of them (?!?).

Can someone shed light on why this doesn’t work in the test environment? Why does SessionContext.get_from_cache("some-session-id-here") return an :error with no session data found?
Do I have to update my mix.exs or something to make ExUnit be able to “see” what’s going on in the cache?
How can I incorporate an asynchronous task to make sure that this completes the process as quickly as possible?

Thanks for any insights!

Most Liked

fireproofsocks

fireproofsocks

I figured out a solution that satisfies my tests:

    Repo.transaction(
      fn () ->
        Enum.map(stream, &cache_session(&1))
      end
    )

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement