Merge pull request #1184 from FarmBot/tues_test_updates

Tues test updates
pull/1187/head
Rick Carlino 2020-03-31 15:00:37 -05:00 committed by GitHub
commit bcb0dae229
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 98 additions and 18 deletions

View File

@ -1,6 +1,7 @@
use Mix.Config
if Mix.env() == :test do
config :ex_unit, capture_logs: true
mapper = fn mod -> config :farmbot_ext, mod, children: [] end
list = [

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 AutoSyncAssetHandlerTest do
require Helpers
use ExUnit.Case, async: false
use Mimic
@ -8,7 +9,10 @@ defmodule AutoSyncAssetHandlerTest do
alias FarmbotExt.AMQP.AutoSyncAssetHandler
alias FarmbotCore.{Asset, BotState, Leds}
import ExUnit.CaptureLog
def auto_sync_off, do: expect(Asset.Query, :auto_sync?, fn -> false end)
def auto_sync_on, do: expect(Asset.Query, :auto_sync?, fn -> true end)
def expect_sync_status_to_be(status),
do: expect(BotState, :set_sync_status, fn ^status -> :ok end)
@ -22,4 +26,41 @@ defmodule AutoSyncAssetHandlerTest do
expect_green_leds(:slow_blink)
AutoSyncAssetHandler.handle_asset("Point", 23, nil)
end
test "Handles @no_cache_kinds" do
id = 64
params = %{}
kind =
~w(Device FbosConfig FirmwareConfig FarmwareEnv FarmwareInstallation)
|> Enum.shuffle()
|> Enum.at(0)
expect(Asset.Command, :update, 1, fn ^kind, ^id, ^params -> :ok end)
assert :ok = AutoSyncAssetHandler.cache_sync(kind, id, params)
end
test "handling of deleted assets when auto_sync is enabled" do
auto_sync_on()
expect_sync_status_to_be("syncing")
expect_sync_status_to_be("synced")
expect_green_leds(:really_fast_blink)
expect_green_leds(:solid)
AutoSyncAssetHandler.handle_asset("Point", 32, nil)
end
test "cache sync" do
id = 64
params = %{}
kind = "Point"
# Helpers.expect_log("Autocaching sync #{kind} #{id} #{inspect(params)}")
changeset = %{ab: :cd}
changesetfaker = fn ^kind, ^id, ^params -> changeset end
expect(FarmbotCore.Asset.Command, :new_changeset, 1, changesetfaker)
expect(FarmbotExt.API.EagerLoader, :cache, 1, fn ^changeset -> :ok end)
expect_sync_status_to_be("sync_now")
expect_green_leds(:slow_blink)
do_it = fn -> AutoSyncAssetHandler.cache_sync(kind, id, params) end
assert capture_log(do_it) =~ "Autocaching sync Point 64 %{}"
end
end

View File

@ -55,7 +55,7 @@ defmodule AutoSyncChannelTest do
expect(Preloader, :preload_all, 1, fn -> :ok end)
pid = generate_pid()
send(pid, msg)
Process.sleep(5)
Helpers.wait_for(pid)
end
test "basic_cancel", do: ensure_response_to({:basic_cancel, :anything})
@ -153,6 +153,6 @@ defmodule AutoSyncChannelTest do
:ok
end)
Process.sleep(1200)
Helpers.wait_for(pid)
end
end

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

@ -1,17 +1,21 @@
Application.ensure_all_started(:farmbot)
Mimic.copy(AMQP.Channel)
Mimic.copy(FarmbotCeleryScript.SysCalls.Stubs)
Mimic.copy(FarmbotCore.Asset.Command)
Mimic.copy(FarmbotCore.Asset.Query)
Mimic.copy(FarmbotCore.BotState)
Mimic.copy(FarmbotCore.Leds)
Mimic.copy(FarmbotCore.LogExecutor)
Mimic.copy(FarmbotExt.AMQP.ConnectionWorker)
Mimic.copy(FarmbotExt.API.EagerLoader.Supervisor)
Mimic.copy(FarmbotExt.API.Preloader)
Mimic.copy(FarmbotExt.API)
Mimic.copy(FarmbotExt.AMQP.AutoSyncAssetHandler)
[
AMQP.Channel,
FarmbotCeleryScript.SysCalls.Stubs,
FarmbotCore.Asset.Command,
FarmbotCore.Asset.Query,
FarmbotCore.BotState,
FarmbotCore.Leds,
FarmbotCore.LogExecutor,
FarmbotExt.AMQP.AutoSyncAssetHandler,
FarmbotExt.AMQP.ConnectionWorker,
FarmbotExt.API,
FarmbotExt.API.EagerLoader,
FarmbotExt.API.EagerLoader.Supervisor,
FarmbotExt.API.Preloader
]
|> Enum.map(&Mimic.copy/1)
timeout = System.get_env("EXUNIT_TIMEOUT") || "5000"
System.put_env("LOG_SILENCE", "true")
@ -19,6 +23,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
@wait_time 60
# Base case: We have a pid
def wait_for(pid) when is_pid(pid), do: check_on_mbox(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))
# Enter recursive loop
defp check_on_mbox(pid) do
Process.sleep(@wait_time)
wait(pid, Process.info(pid, :message_queue_len))
end
# Exit recursive loop (mbox is clear)
defp wait(_, {:message_queue_len, 0}), do: Process.sleep(@wait_time * 3)
# Exit recursive loop (pid is dead)
defp wait(_, nil), do: Process.sleep(@wait_time * 3)
# Continue recursive loop
defp wait(pid, {:message_queue_len, _n}), do: check_on_mbox(pid)
defmacro expect_log(message) do
quote do
expect(FarmbotCore.LogExecutor, :execute, fn log ->