Add Helpers.wait_for(pid). Remove :noop call
parent
7fad9e7231
commit
06323242c2
|
@ -44,8 +44,6 @@ defmodule FarmbotExt.API.ImageUploader do
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_continue([], state), do: {:noreply, state, @checkup_time_ms}
|
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
|
# 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
|
# from the cache instead of from the firmware directly. It's close enough and
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
defmodule FarmbotExt.API.ImageUploaderTest do
|
defmodule FarmbotExt.API.ImageUploaderTest do
|
||||||
|
require Helpers
|
||||||
use ExUnit.Case, async: false
|
use ExUnit.Case, async: false
|
||||||
use Mimic
|
use Mimic
|
||||||
alias FarmbotExt.API.ImageUploader
|
alias FarmbotExt.API.ImageUploader
|
||||||
|
@ -22,12 +23,21 @@ defmodule FarmbotExt.API.ImageUploaderTest do
|
||||||
end)
|
end)
|
||||||
|
|
||||||
expect(FarmbotExt.API, :upload_image, 4, fn
|
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: %{}}}
|
_image_filename, _meta -> {:ok, %{status: 201, body: %{}}}
|
||||||
end)
|
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()
|
ImageUploader.force_checkup()
|
||||||
send(pid, :timeout)
|
send(pid, :timeout)
|
||||||
:ok = GenServer.call(pid, :noop)
|
Helpers.wait_for(pid)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -19,6 +19,32 @@ System.put_env("LOG_SILENCE", "true")
|
||||||
ExUnit.start(assert_receive_timeout: String.to_integer(timeout))
|
ExUnit.start(assert_receive_timeout: String.to_integer(timeout))
|
||||||
|
|
||||||
defmodule Helpers do
|
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
|
defmacro expect_log(message) do
|
||||||
quote do
|
quote do
|
||||||
expect(FarmbotCore.LogExecutor, :execute, fn log ->
|
expect(FarmbotCore.LogExecutor, :execute, fn log ->
|
||||||
|
|
Loading…
Reference in New Issue