Re:Testable IO in Haskell
by chadaustin on Sunday June 21, 2015 @01:31PM
(#9754191)
That's cool. I chatted with a friend about this too and they said they've used SecurityManager in this way. But one thing they said is that SecurityManager can't be used to restrict things like accessing the current time. Do you know if that's true or not?
That's true -- out of the box. As with all things JVM, this, too is very pliable. You can create a new permission -- "access clock" -- and then it is quite easy to use an agent to inject a call for the security manager to check for this permission whenever the clock is accessed.
In fact, since we started this discussion, I've written a small library that uses an agent to fake all clock accesses on the JVM with a user-supplied virtual clock, that can be set globally or per-thread. Injecting s security check is even easier.
I do. Not often, as I don't have a lot of IO in my tests, but I've found the security manager useful for that purpose from time to time. I also use it to help our users enforce global contracts, such as prohibit IO or any blocking code in fork-join computations.