Unit tests for AutoSyncChannel.terminate

pull/1168/head
Rick Carlino 2020-03-09 11:48:07 -05:00
parent 5ef5bb7b91
commit 1d85b95a48
3 changed files with 9 additions and 4 deletions

View File

@ -23,3 +23,4 @@ erl_crash.dump
farmbot_ext-*.tar
*.sqlite3
*.coverdata

View File

@ -68,12 +68,15 @@ defmodule FarmbotExt.AMQP.AutoSyncChannel do
def terminate(reason, state) do
FarmbotCore.Logger.error(1, "Disconnected from AutoSync channel: #{inspect(reason)}")
# If a channel was still open, close it.
if state.chan, do: ConnectionWorker.close_channel(state.chan)
if state.chan do
ConnectionWorker.close_channel(state.chan)
end
try do
EagerLoader.Supervisor.drop_all_cache()
catch
_, _ -> :ok
_, _ ->
:ok
end
end

View File

@ -54,14 +54,15 @@ defmodule AutoSyncChannelTest do
# test suite except under limited circumstances.
expect(ConnectionWorker, :maybe_connect_autosync, ok1)
stub(FarmbotExt.API.EagerLoader.Supervisor, :drop_all_cache, ok)
expect(FarmbotExt.API.EagerLoader.Supervisor, :drop_all_cache, ok)
{:ok, pid} = AutoSyncChannel.start_link([jwt: jwt], [])
pid
end
test "init / terminate" do
# Most assertions are handled by `gnerate_pid`:
pid = generate_pid()
assert %{chan: nil, conn: nil, preloaded: true} == AutoSyncChannel.network_status(pid)
Process.exit(pid, :normal)
GenServer.stop(pid, :normal)
end
end