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 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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
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
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
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
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
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
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
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











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.