unit testing sleep and other concurrency primitives.

298 views
Skip to first unread message

Kevin Burton

unread,
Aug 20, 2014, 12:53:32 PM8/20/14
to mechanica...@googlegroups.com
I'm curious if there is a best practice for doing this or perhaps a library framework out there I can use.

I have a somewhat complex application with multiple modules. Some of the algorithms are complicated so I have a mock Clock that I use.  

In production we deploy with a SystemClock but during tests I deploy with a SyntheticClock.

Clock has a method called sleep() which does what you expect it to do.

In SyntheticClock, it just jumps the time forward for the sleep interval, and returns immediately, as if time past synthetically.

The problem is, some libraries, obviously aren't using my synthetic clock... so it's going to make testing them difficult.

If there was an existing framework for doing this I'd love to adopt it.

Vitaly Davidovich

unread,
Aug 20, 2014, 1:13:02 PM8/20/14
to mechanica...@googlegroups.com

jmockit can intercept static method calls, perhaps you can use that to do synthetic simulation?

Sent from my phone

--
You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nitsan Wakart

unread,
Aug 21, 2014, 5:22:03 AM8/21/14
to mechanica...@googlegroups.com
From experience, mocking System.currentTimeMillis is not enough for many applications and libraries. To consistently mock time across an application one has to consider other wall clock dependencies:
- Any and all IO/lock based methods which have a given timeout
- Timers and delay queues
- System.nanoTime for spinning backoff schemes
The test library then needs to correctly transition through the time points expected by the code so that things happen in order. Simply moving the clock forward and releasing all the threads is not going to produce correct results.
I don't know of any library which tackles the above, but it's an interesting problem.

Kasper Nielsen

unread,
Aug 26, 2014, 3:42:10 AM8/26/14
to mechanica...@googlegroups.com
Depending on the complexity of the code take a look at https://code.google.com/p/multithreadedtc/

Unfortunately it is not actively developed anymore.

gnash

unread,
Aug 26, 2014, 10:25:28 AM8/26/14
to mechanica...@googlegroups.com
There is a CSP concurrency library called Jetlang that allows for deterministic testing of various interleaving of events.  Not sure if that matches your use case as it doesn't support sleep, but it is actively maintained.

Kevin Burton

unread,
Sep 2, 2014, 1:02:27 PM9/2/14
to mechanica...@googlegroups.com, nit...@yahoo.com
ONE mock library that did all that would rock.

Gil Tene

unread,
Sep 2, 2014, 2:46:30 PM9/2/14
to
I don't have an intercept solution for you (which you'd need for other people's code), but I did build a similar and simple TimeServices class in my LatencyUtils project on github, in order for unit tests of various time based scenarios to work. All time related The services I support there include nanoTime, currentTimeMillis, variations of sleep, waitUntilTime, and a ScheduledExecutor. 

One thing I found (the hard way) is that jumping the clock "all at once" doesn't work well for many things. You need to slew it forward in tight loops of small delta jumps (e.g. in setCurrentTime), otherwise strange things happen to and between time-awaiting services (e.g. if multiple wakeup/sleep cycles would have happened in the time jump).
Reply all
Reply to author
Forward
0 new messages