Inspired by imunit I have put up a very early version of a concurrent testing library for junit which allows you to test your thread-safe objects with multiple execution schedules.
You can specify constraints on each schedule which contest will enforce – if the test progresses the execution schedule will meet the constraint (at the moment contest doesn’t detect impossible schedules or runtime deadlocks).
Tests look like this:
@Test @Schedules({ @Schedule( when = AddAddRemove.class, then = SizeIsOne.class), @Schedule( when = AddRemoveAdd.class, then = SizeIsOne.class), @Schedule( when = RemoveAddAdd.class, then = SizeIsTwo.class) }) public void twoAddsOneRemove() { context.checking(new TestRun() { { inThread(Producer).action(FirstAdd).is(multiset).add(42); inThread(Producer).action(SecondAdd).is(multiset).add(42); inThread(Consumer).action(Remove).is(multiset).remove(42); } }); } |
The following is a schedule constraint read as “Action FirstAdd happens before action Remove, and action SecondAdd also happens before action Remove“.
class AddAddRemove extends BaseSchedule { { action(FirstAdd).isBefore(Remove); action(SecondAdd).isBefore(Remove); } } |
Theories are defined like this (using hamcrest matchers):
class SizeIsOne extends BaseTheory { { asserting(that(multiset).size(), equalTo(1)); } } |
Get it from maven central:
<groupId>com.lexicalscope.contest</groupId> <artifactId>contest</artifactId> <version>0.0.1</version> |