Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distinguish reading empty binaries from timeout #33

Open
pallix opened this issue Jan 29, 2018 · 3 comments
Open

Distinguish reading empty binaries from timeout #33

pallix opened this issue Jan 29, 2018 · 3 comments

Comments

@pallix
Copy link

pallix commented Jan 29, 2018

Nerves.UART.read returns {:ok, ""} upon timeout. When testing some code I noticed it is possible to use Nervers.UART.write with an empty string ("") and it is possible to read it: {:ok, ""} will also be returned. So it is not possible to distinguish between empty binaries being written and timeout.

I do not need to write empty binaries on the serial but noticed this while using propcheck and tty0tty.

Would it make sense to distinguish timeout and empty binaries? (by returning :timeout for example)

@fhunleth
Copy link
Contributor

At first I thought it was a little weird to write an empty string, but I suppose that if you have a framer that allows it, then it would be an ok thing to do. Given that, I agree that Nerves.UART.read should return {:error, :timeout} so that you can tell the difference.

Also, that's awesome that propcheck caught this case!

@adkron
Copy link
Contributor

adkron commented Apr 11, 2018

@pallix, do you have the propcheck test for this? I would love to see it.

@pallix
Copy link
Author

pallix commented Apr 11, 2018

Really there is nothing special to the test. We have a wrapper around Nerves (called Nerves in the code below) in the project and the test looks like this:

  property "can read and write on the serial cable", [:quiet], ctx do
    # exclude empty bitstring from test as we never want to write
    # that on the serial and Nerves use it for timeout.
    forall buffer <- not_empty(binary()) do
      Nerves.write(ctx.test_nerves_out, buffer)
      # tty0tty is slow, wait a litte bit
      :timer.sleep(@rw_timeout - 10)
      {ret, data} = Nerves.read(ctx.test_nerves_in, 1024)
      ret == :ok and data == buffer
    end
  end

Basically the test would fail because ret would equal :to if we allow to write empty buffers.

Setup is done with:

    {:ok, nerves_in} = start_supervised({Nerves, [device: device_in, speed: 9600,
                                                  name: :test_nerves_in]},
      id: :test_nerves_in,  restart: :temporary)
    {:ok, nerves_out} = start_supervised({Nerves, [device: device_out, speed: 9600,
                                                   name: :test_nerves_out]},
    [test_nerves_in: nerves_in, test_nerves_out: nerves_out]

I hope this help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants