igalic
Hi folks,
I’m trying to transform our tests for AuthToken (a wrapper around JOSE) to ExUnitProperties. But I’m kinda failing at the most basic task of transforming a simple function into a generator. What I’ve done is this:
defp stream_authtoken_keys(_seed, _range) do
Stream.repeatedly(&AuthToken.generate_key/0)
end
defp gen_authtoken_key() do
%StreamData{generator: &stream_authtoken_keys/2}
end
However, when trying to use it…
describe "keys" do
property "generate_key/0 returns a valid AES128 key" do
check all authtoken_key <- gen_authtoken_key() do
{:ok, key} = authtoken_key
assert byte_size(key) == 16
end
end
end
I get the following error:
1) property keys generate_key/0 returns a valid AES128 key (AuthTokenTest)
test/authtoken_test.exs:23
** (MatchError) no match of right hand side value: #Function<51.91433161/2 in Stream.repeatedly/1>
code: check all authtoken_key <- gen_authtoken_key() do
stacktrace:
(stream_data) lib/stream_data.ex:203: StreamData.bind_filter/5
(stream_data) lib/stream_data.ex:346: anonymous fn/5 in StreamData.bind_filter/3
(stream_data) lib/stream_data.ex:203: StreamData.check_all/6
test/authtoken_test.exs:24: (test)
Trending in Questions
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
DanCouper
Just as simple as
authtoken_keyrather thankey?igalic
After posting this in elixir (on irc & discord) I got a very decent reply that works out pretty fine, so I’m gonna close this topic for now with it!
First off, trying to generate
%StreamData{}myself, is kinda bad form, since it’s basically a private struct…We looked for alternatives, and found
StreamData.constant/0. However, on its own, it always generates the same value.Which makes a certain amount of sense when you think about how it’s called:
The clue came from @michalmuskala:
and the code we now have from @ericmj
what we’re doing here is using
StreamData.constant/1as a sort of clock generator, that drives the call ofAuthToken.generate_key/0… and it’s nice start, but we’re needlessly generating alist_ofwhich we thenmapSo let’s update this with all responses below!
This is much cleaner! Since we use
bind/2, which is essentially made for this… better see what’s going on, we can use pipes to extract the flow:We feed a stream of
:unusedintogen_tickerlambda, just so it will be constantly called. Since our call toAuthToken.generate_key/0is wrapped in said lambda, it will be executed on every call, rather than onge_ticker’s declaration. Finally, we pass the so generated%StreamData{}intounshrinkable/0(because it is) before returning it.igalic
i think that was just an awkward copy/paste error… and i should fix this in the original post
igalic
was missing in my paste (not the error causing code)
ericmj
I found a (maybe) cleaner way of doing it that doesn’t involve generating a list:
I haven’t tested this so I can’t verify that it works.
EDIT: @whatyouhide has a more concise solution below: How to create a custom StreamData Generator - #12 by whatyouhide
LostKobrakai
It might also be good to make the generator unshrinkable, because shrinking doesn’t seem to make sense in that context.
igalic
I really couldn’t figure this out from the documentation…
maybe we should add something to the docs that helps people figure out how to create custom generators
@michalmuskala suggested to have
StreamData.repeatedly/1. I was thinking maybe a better way would be to have a way of converting aStreamto aStreamData.LostKobrakai
I found it quite easy to build more complex generators out of the basic generators already in the streamdata module, but I’ve to say that your usecase really doesn’t seem very well supported by the current functions.
anthonator
@igalic’s approach only seems to work if you use the generator in a
check. If you try to use it in agenyou run into a the issue of the value being considered a constant so it’s the same on each run through. Any way around this?anthonator
I was able to get this working. Turned out
checkwas swallowing another error.