Add Helpers.wait_for(pid). Remove :noop call

pull/1184/head
Rick Carlino 2020-03-31 10:36:48 -05:00
parent 7fad9e7231
commit 06323242c2
3 changed files with 38 additions and 4 deletions

View File

@ -44,8 +44,6 @@ defmodule FarmbotExt.API.ImageUploader do
end
def handle_continue([], state), do: {:noreply, state, @checkup_time_ms}
# This only exists to flush handle_cast's. I think. -RC
def handle_call(:noop, _, s), do: {:reply, :ok, s}
# the meta here is likely inaccurate here because of pulling the location data
# from the cache instead of from the firmware directly. It's close enough and

View File

@ -1,4 +1,5 @@
defmodule FarmbotExt.API.ImageUploaderTest do
require Helpers
use ExUnit.Case, async: false
use Mimic
alias FarmbotExt.API.ImageUploader
@ -22,12 +23,21 @@ defmodule FarmbotExt.API.ImageUploaderTest do
end)
expect(FarmbotExt.API, :upload_image, 4, fn
"/tmp/images/d.gif", _meta -> {:ok, %{status: 401, body: %{}}}
"/tmp/images/d.gif", _meta -> {:error, %{status: 401, body: %{}}}
_image_filename, _meta -> {:ok, %{status: 201, body: %{}}}
end)
err_msg =
"Upload Error (/tmp/images/d.gif): " <>
"{:error, %{body: %{}, status: 401}}"
Helpers.expect_log("Uploaded image: /tmp/images/a.jpg")
Helpers.expect_log("Uploaded image: /tmp/images/b.jpeg")
Helpers.expect_log("Uploaded image: /tmp/images/c.png")
Helpers.expect_log(err_msg)
ImageUploader.force_checkup()
send(pid, :timeout)
:ok = GenServer.call(pid, :noop)
Helpers.wait_for(pid)
end
end

View File

@ -19,6 +19,32 @@ System.put_env("LOG_SILENCE", "true")
ExUnit.start(assert_receive_timeout: String.to_integer(timeout))
defmodule Helpers do
# Maybe I don't need this?
# Maybe I could use `start_supervised`?
# https://hexdocs.pm/ex_unit/ExUnit.Callbacks.html#start_supervised/2
# Base case: We have a pid
def wait_for(pid) when is_pid(pid), do: continue_waiting(pid)
# Failure case: We failed to find a pid for a module.
def wait_for(nil), do: raise("Attempted to wait on bad module/pid")
# Edge case: We have a module and need to try finding its pid.
def wait_for(mod), do: wait_for(Process.whereis(mod))
defp continue_waiting(pid) do
wait(pid, Process.info(pid, :message_queue_len))
end
defp wait(_pid, {:message_queue_len, 0}), do: :ok
defp wait(pid, {:message_queue_len, n}) when n < 20 do
Process.sleep(100)
continue_waiting(pid)
end
defp wait(pid, {:message_queue_len, n}) do
raise "No longer waiting on #{inspect(pid)} after #{n} attempts"
end
defmacro expect_log(message) do
quote do
expect(FarmbotCore.LogExecutor, :execute, fn log ->