public void testSynchronisedInvocationDispatcherBlocksAddingExpectationsWhileOtherTestsDispatch()
throws Throwable {
final CyclicBarrier barrier = new CyclicBarrier(2);
MockExpectation expectation1 = new MockExpectation(true, NOT_RELEVANT, NOT_RELEVANT);
MockExpectation expectation2 = new MockExpectation(false, NOT_RELEVANT, NOT_RELEVANT);
final InvocationDispatcher dispatcher = new SynchronisingInvocationDispatcherWrapper(
new UnsynchronisedInvocationDispatcher(new ArrayList<Expectation>(), new ArrayList<StateMachine>()));
dispatcher.add(expectation1);
new Thread(new Runnable() {
@Override
public void run() {
try {
dispatcher.dispatch(invocation);
barrier.await();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
}, "Concurrent Dispatch").start();
barrier.await();
dispatcher.add(expectation2);
expectation1.shouldBeInvokedWith(invocation);
assertTrue("expectation1 should have been invoked",
expectation1.wasInvoked);
}