@Test
public void testConcurrentSubscribeAndHandleRequest() throws Exception {

	final ExecutorService executor = Executors.newFixedThreadPool(2);
	final TestNotificationListener listener = new TestNotificationListener();
	final Callable<Boolean> subscriber = new Callable<Boolean>() {
		@Override
		public Boolean call() throws Exception {
			return writer.registerAllRequestsProcessedListener(listener);
		}
	};
	final Callable<Void> requestHandler = new Callable<Void>() {
		@Override
		public Void call() throws Exception {
			handleRequest();
			return null;
		}
	};
	try {
		// Repeat this to provoke races
		for (int i = 0; i < 50000; i++) {
			listener.reset();
			addRequest();
			Future<Void> handleRequestFuture = executor.submit(requestHandler);
			Future<Boolean> subscribeFuture = executor.submit(subscriber);
			handleRequestFuture.get();
			try {
				if (subscribeFuture.get()) {
					assertEquals("Race: Successfully subscribed, but was never notified.", 1, listener.getNumberOfNotifications());
				}
				else {
					assertEquals("Race: Never subscribed successfully, but was notified.", 0, listener.getNumberOfNotifications());
				}
			}
			catch (Throwable t) {
				System.out.println(i);
				Assert.fail(t.getMessage());
			}
		}
	}
	finally {
		executor.shutdownNow();
	}
}