Skip to content
Permalink
Browse files

(9.1%) Test case: Error when re-attaching UART

  • Loading branch information
RickCarlino committed Jan 13, 2020
1 parent 010cf90 commit a61c69c816b4d5b620e6326a7dc3d51712b37896
Showing with 35 additions and 16 deletions.
  1. +35 −16 farmbot_firmware/test/farmbot_firmware/transports/uart_transport_test.exs
@@ -5,9 +5,10 @@ defmodule FarmbotFirmware.UARTTransportTest do
import Mox
setup [:verify_on_exit!]
doctest FarmbotFirmware.UARTTransport
alias FarmbotFirmware.{UartTestAdapter, UARTTransport}

test "FarmbotFirmware.UARTTransport.init/1" do
expect(FarmbotFirmware.UartTestAdapter, :start_link, fn ->
test "UARTTransport.init/1" do
expect(UartTestAdapter, :start_link, fn ->
{:ok, :FAKE_UART}
end)

@@ -17,58 +18,76 @@ defmodule FarmbotFirmware.UARTTransportTest do
reset: :FAKE_RESETER
]

{:ok, state, 0} = FarmbotFirmware.UARTTransport.init(init_args)
{:ok, state, 0} = UARTTransport.init(init_args)
assert state.device == Keyword.fetch!(init_args, :device)
assert state.handle_gcode == Keyword.fetch!(init_args, :handle_gcode)
assert state.reset == Keyword.fetch!(init_args, :reset)
end

test "FarmbotFirmware.UARTTransport.terminate/2" do
expect(FarmbotFirmware.UartTestAdapter, :stop, fn uart ->
test "UARTTransport.terminate/2" do
expect(UartTestAdapter, :stop, fn uart ->
assert uart == :whatever
end)

state = %{uart: :whatever}
FarmbotFirmware.UARTTransport.terminate(nil, state)
UARTTransport.terminate(nil, state)
end

test "FarmbotFirmware.UARTTransport resets UART on timeout" do
test "UARTTransport resets UART on timeout" do
state = %{uart: :FAKE_UART, device: :FAKE_DEVICE, open: false}

fake_opts = [fake_opts: true]

expect(FarmbotFirmware.UartTestAdapter, :generate_opts, fn ->
expect(UartTestAdapter, :generate_opts, fn ->
fake_opts
end)

expect(FarmbotFirmware.UartTestAdapter, :open, fn uart, dev, opts ->
expect(UartTestAdapter, :open, fn uart, dev, opts ->
assert uart == state.uart
assert dev == state.device
assert fake_opts == opts
:ok
end)

{:noreply, state2} = FarmbotFirmware.UARTTransport.handle_info(:timeout, state)
{:noreply, state2} = UARTTransport.handle_info(:timeout, state)
# Expect the `open` state to toggle back to "true" from "false":
refute state.open == state2.open
end

test "FarmbotFirmware.UARTTransport.reset/2" do
test "UARTTransport handles unexpected UART errors" do
state = %{uart: :FAKE_UART, device: :FAKE_DEVICE, open: false}

fake_opts = [fake_opts: true]

expect(UartTestAdapter, :generate_opts, fn ->
fake_opts
end)

expect(UartTestAdapter, :open, fn _, _, _ ->
{:error, "Simulated UART failure. This is OK"}
end)

{:noreply, state2, retry_timeout} = UARTTransport.handle_info(:timeout, state)
assert retry_timeout == 5000
assert state.open == state2.open
end

test "UARTTransport.reset/2" do
empty_state = %{reset: nil}
ok_state = %{reset: %{reset: :fake_reset}}
assert :ok == FarmbotFirmware.UARTTransport.reset(empty_state)
assert :fake_reset == FarmbotFirmware.UARTTransport.reset(ok_state)
assert :ok == UARTTransport.reset(empty_state)
assert :fake_reset == UARTTransport.reset(ok_state)
end

test "FarmbotFirmware.UARTTransport.open/2" do
test "UARTTransport.open/2" do
me = self()

expect(FarmbotFirmware.UartTestAdapter, :open, fn pid, path, opts ->
expect(UartTestAdapter, :open, fn pid, path, opts ->
assert pid == me
assert path == "/dev/null"
assert opts == [a: :b]
end)

FarmbotFirmware.UARTTransport.open(me, "/dev/null", a: :b)
UARTTransport.open(me, "/dev/null", a: :b)
end
end

0 comments on commit a61c69c

Please sign in to comment.
You can’t perform that action at this time.