clojure-jna icon indicating copy to clipboard operation
clojure-jna copied to clipboard

Event and Timeout Tests for select

Open zeroem opened this issue 11 years ago • 6 comments

Pretty simple test just to get some coverage of libc-utils/select. Working on figuring out how to generate file system events so I can test the non-timeout behavior.

zeroem avatar Nov 30 '14 05:11 zeroem

So, one thing I'm not comfortable about is open/close vs fopen/fclose. Open is lower level and I'm relatively certain I'm not using it correctly. However, when trying to import fopen/fclose, the JVM segfaults. So going to look into that as well.

zeroem avatar Nov 30 '14 17:11 zeroem

OK, so the test is just broken in general. open/close return a file descriptor whereas fopen/fclose return a file pointer. the select call is likely segfaulting when I use fopen/fclose because of the filepointer. And the use of open is definitely just plain wrong.

zeroem avatar Nov 30 '14 17:11 zeroem

Thanks for working on this!

Your patch looks about right to me. You're using open/close which return integer file handles, and that's exactly what select expects. The test succeeded for me several times, though I occasionally do get a test fail because select actually detects an event on the "project.clj" file handle.

On the other hand, I had to mess with leiningen project settings a bit to get anything to work. I don't know if it's recent versions of leiningen itself, or Ubuntu, or the JVM or what, but I had to add to project.clj:

:jvm-opts ["-Djna.nosys=true"] ...and in the :dependencies, added: [org.clojure/tools.nrepl "0.2.5"]

Did you have to do any of that?

Chouser avatar Nov 30 '14 19:11 Chouser

I had to do the :jvm-opts, but I did it from the command line, I assumed it was something wrong with my environment and not a project level issue. I did not need the additional nrepl dependency.

My problem with the open call is that according to man 2 open, the second argument to open should be bit flags, not the fopen style string indicator. Additionally, the value I'm getting back from the open call is alternating between 0 and -1. Given that the return value is described as:

A call to open() creates a new open file description, an entry in the system-wide table of open files.

and

A file descriptor is a reference to one of these entries;

I have a hard time believing that 0 or -1 is a valid result.

zeroem avatar Nov 30 '14 22:11 zeroem

Alrighty, figured out why the fopen call was puking and got everything working nicely (I think).

zeroem avatar Dec 03 '14 06:12 zeroem

Interestingly, with the tools I added just to do the tests for select, it would be possible to allow it to take strings or File objects instead of just the low level file descriptors making clojure access significantly simpler.

zeroem avatar Dec 03 '14 06:12 zeroem