Skip to content

Commit

Permalink
Clean up observer
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Nov 5, 2024
1 parent 15746c2 commit ccbe01a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
16 changes: 10 additions & 6 deletions lib/lexdee/observer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ defmodule Lexdee.Observer do
GenServer.call(pid, :node)
end

def connect(pid) do
GenServer.call(pid, :connect)
end

def connect(supervisor, pid) do
node = which_node(pid)

Expand All @@ -43,12 +47,7 @@ defmodule Lexdee.Observer do
end

def start_link(options) do
with {:ok, pid} <- GenServer.start_link(__MODULE__, options, []),
{:ok, :connected} <- GenServer.call(pid, :connect) do
{:ok, pid}
else
{:error, reason} -> {:error, reason}
end
GenServer.start_link(__MODULE__, options, [])
end

@impl true
Expand Down Expand Up @@ -374,6 +373,11 @@ defmodule Lexdee.Observer do
{:stop, :normal, state}
end

defp reply(state, {:ok, :connected}) do
if state.caller, do: GenServer.reply(state.caller, {:ok, state})
put_in(state.caller, nil)
end

defp reply(state, response) do
if state.caller, do: GenServer.reply(state.caller, response)
put_in(state.caller, nil)
Expand Down
20 changes: 16 additions & 4 deletions test/lexdee/observer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ defmodule Lexdee.ObserverTest do
end

test "can receive ping", %{url: url, client: client} do
assert {:ok, _pid} =
assert {:ok, pid} =
Lexdee.Observer.start_link(
url: url,
client: client,
handler: PingHandler
)

Lexdee.Observer.connect(pid)

server_pid = WebsocketServerMock.receive_socket_pid()

send(server_pid, {:send, {:ping, "keepalive"}})
Expand All @@ -63,6 +65,8 @@ defmodule Lexdee.ObserverTest do
handler: PingHandler
)

Lexdee.Observer.connect(pid)

send(pid, :check_connectivity)

assert_receive "ok"
Expand Down Expand Up @@ -102,6 +106,8 @@ defmodule Lexdee.ObserverTest do
parent: self()
)

{:ok, _state} = Lexdee.Observer.connect(pid)

WebsocketServerMock.shutdown(server_ref)

send(pid, :check_connectivity)
Expand Down Expand Up @@ -139,13 +145,15 @@ defmodule Lexdee.ObserverTest do
end

test "can receive task event", %{url: url, client: client} do
assert {:ok, _pid} =
assert {:ok, pid} =
Lexdee.Observer.start_link(
url: url,
client: client,
handler: TextHandler
)

Lexdee.Observer.connect(pid)

server_pid = WebsocketServerMock.receive_socket_pid()

payload = File.read!("test/support/fixtures/websocket/task.json")
Expand All @@ -156,13 +164,15 @@ defmodule Lexdee.ObserverTest do
end

test "can receive websocket", %{url: url, client: client} do
assert {:ok, _pid} =
assert {:ok, pid} =
Lexdee.Observer.start_link(
url: url,
client: client,
handler: TextHandler
)

{:ok, _state} = Lexdee.Observer.connect(pid)

server_pid = WebsocketServerMock.receive_socket_pid()

payload = File.read!("test/support/fixtures/websocket/websocket.json")
Expand All @@ -172,13 +182,15 @@ defmodule Lexdee.ObserverTest do
end

test "can receive lifecycle", %{url: url, client: client} do
assert {:ok, _pid} =
assert {:ok, pid} =
Lexdee.Observer.start_link(
url: url,
client: client,
handler: TextHandler
)

{:ok, _state} = Lexdee.Observer.connect(pid)

server_pid = WebsocketServerMock.receive_socket_pid()

payload = File.read!("test/support/fixtures/websocket/lifecycle.json")
Expand Down

0 comments on commit ccbe01a

Please sign in to comment.