Skip to content

Commit

Permalink
unsubscribe test works
Browse files Browse the repository at this point in the history
  • Loading branch information
m1dnight committed May 18, 2018
1 parent 05bee9c commit 4d07921
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/behavior/gen_observable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ defmodule Observables.GenObservable do
{:noreply, state}
end

def handle_cast(:unsubscribe, state) do
# First notify all our dependees that we no longer want their data.
# This has as a side effect qwe no longer listen to them as well.
state.listeningto
|> Enum.map(fn producer ->
stop_send_to(producer, self())
end)

# Now it should be safe to stop.
GenObservable.stop(self())

{:noreply, state}
end

def handle_info({:event, value}, state) do
cast(self(), {:event, value})
{:noreply, state}
Expand Down Expand Up @@ -235,7 +249,15 @@ defmodule Observables.GenObservable do
Makes an observable stop and gracefully shut down.
"""
def stop(producer, reason \\ :normal) do
cast(producer, {:stop, reason})
#cast(producer, {:stop, reason})
cast(producer, :stop)
end

@doc """
Unsubscribes ourself from all our dependencies.
"""
def unsubscribe(producer) do
cast(producer, :unsubscribe)
end

###########
Expand Down
11 changes: 11 additions & 0 deletions lib/subject.1.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Observables.Observable do
alias Observables.GenObservable

@moduledoc """
WRITE ME PLEASE KIND SIR
"""

def unsubscribe() do
GenObservable.unsubscribe(self())
end
end
28 changes: 28 additions & 0 deletions test/unsubscribe_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule UnsubscribeTest do
use ExUnit.Case
alias Observables.{Obs}
require Logger

@tag :unsubscribe
test "ubsubscribe" do
Code.load_file("test/util.ex")

testproc = self()

Obs.range(1, :infinity, 500)
|> Obs.map(fn v ->
if v > 5 do
Observables.Observable.unsubscribe()
else
send(testproc, v)
end
end)

Test.Util.sleep(2000)

1..5
|> Enum.map(fn v ->
assert_receive(^v, 5000, "did not get this message #{v}")
end)
end
end

0 comments on commit 4d07921

Please sign in to comment.