GenStage, BroadcastDispatcher and Testing

Good Evening All,

I’m trying to figure out a way to create a simple inline event broadcaster adapter for a GenStage system that uses a BroadcastDispatcher.

In my test suite, I’d really love to be able to test against something as simple as:

defmodule InlineAdapter do
  def sync_notify(event) do
    Enum.each(magically_find_consumer_modules(), fn(consumer) ->
      consumer.handle_event(event)
    end)
  end
end

In dev/production, the adapter implementation would be like the example: https://github.com/elixir-lang/gen_stage/blob/master/examples/gen_event.exs#L31

So the issue is, how do I implement magically_find_consumer_modules()?

Or is there another way I should be going about this?

Thanks!
Benjamin

1 Like

Why do you want to magically find consumers instead of explicitly having consumers subscribe to producer which is how GenStage works?

1 Like

I simply want to be able to test against a completely synchronous system. I don’t want to have to poll or sleep during my test suite or otherwise worry that there is pending events to be processed after the sync_notify function has returned.

Edit:
Pulled the example from GenStage and wrote a little comment with what I’m trying to accomplish.

So my thinking was… if I could figure out which consumers have subscribed and get their modules, I could write a simple Enum.each and be on my merry way.

https://github.com/nerdyworm/gsx_example/blob/master/test/gsx_test.exs#L16

1 Like