Skip to content

Patch GenServer :handle/2 - Idea / question? #46

Description

@Matsa59

Hello I have some GenServer that subscribe to Phoenix.PubSub channel to reduce code coupling

I feel like something is missing Patch (that is really nice btw).

My workflow is everytime a character is created, I broadcast a message that will be catch by some GenServers to do their shit (e.g. send welcome email, push discord msg, or log the event in a db or w.e)

I want to test this but because it's a PhoenixPubSub event, it's not easily testable. I end with the following test (that works great).

defmoduleForHonor.Feed.AdventureEventSubscriberTestdousePatch#...test"on character created",%{character: character}dopatch_handle_info(AdventureEventSubscriber)msg={:character_created,character}Phoenix.PubSub.broadcast(ForHonor.PubSub,"game:events",msg)assert_handle_info(AdventureEventSubscriber)# test if side effect was apply or w.eenddefppatch_handle_info(module)dopid=self()patch(module,:handle_info,fnmsg,state->real(module).handle_info(msg,state)|>tap(fn_->send(pid,module)end)end)enddefpassert_handle_info(module,timeout\\:timer.seconds(5))doreceivedo^module->asserttrueaftertimeout->assertfalseendendend

I don't have a global overview of patch, maybe I miss something. I would like to have your opinion on the previous code. If their is no other way to accomplish that, are you interested by a pull request ?

Thanks a lot ;)

Metadata

Metadata

Assignees

No one assigned

    Labels

    ❓ QuestionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions