Created
September 4, 2014 14:15
-
-
Save Hades32/5669cfed29f0f3907d18 to your computer and use it in GitHub Desktop.
Problem with GenEvent.stream vs. GenEvent.add_handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule KV.RegistryTest do | |
use ExUnit.Case, async: true | |
alias KV.Registry, as: Reg | |
# test helper module | |
defmodule Forwarder do | |
use GenEvent | |
def handle_event(event, parent) do | |
send parent, event | |
{:ok, parent} | |
end | |
end | |
setup do | |
{:ok, sup} = KV.Bucket.Supervisor.start_link() | |
{:ok, manager} = GenEvent.start_link() | |
{:ok, registry} = Reg.start_link(manager, sup) | |
##### this does not work! #### | |
#parent = self() | |
#spawn_link fn -> | |
# for msg <- GenEvent.stream(manager) do | |
# send parent, msg | |
#sometest = {:ok, :ornot} | |
#end | |
#end | |
#### but this does! #### | |
GenEvent.add_handler(manager, Forwarder, self()) | |
{:ok, registry: registry, manager: manager, sup: sup} | |
end | |
test "spawns buckets", %{registry: reg} do | |
assert Reg.lookup(reg, "shop1") == :error | |
Reg.create(reg,"shop1") | |
assert {:ok, _bucket} = Reg.lookup(reg, "shop1") | |
end | |
test "removes bucket on crash", %{registry: reg} do | |
Reg.create(reg, "shop2") | |
{:ok, bucket} = Reg.lookup(reg, "shop2") | |
Process.exit(bucket, :shutdown) | |
assert_receive {:exit, "shop2", ^bucket} | |
assert Reg.lookup(reg, "shop2") == :error | |
end | |
test "sends event on create and crash", %{registry: reg} do | |
Reg.create(reg,"shop3") | |
{:ok, bucket} = Reg.lookup(reg, "shop3") | |
assert_receive {:create, "shop3", ^bucket} | |
Process.exit(bucket, :shutdown) | |
assert_receive {:exit, "shop3", ^bucket} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can anybody explain to me what the real difference between the stream and the handler approach is? When I run the tests I seem to get a race condition. Sometimes "all green", sometimes the ":create" or ":exit" messages are not received.
My "printf debugging" has shown that "GenEvent.sync_notify" seems to block indefinitely...
The rest of the source is as described in the elixir tutorial after cap 5.2