/** * Drives the actual test on an Executor and verifies the result * * @param maps the caches to be tested * @throws IOException * @throws InterruptedException */ private void testConcurrentLocking(List<ConcurrentMap<String, String>> maps) throws IOException, InterruptedException { SharedStats stats = new SharedStats(); ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(NODES_NUM); List<StressingThread> threads = new ArrayList<StressingThread>(); for (ConcurrentMap<String, String> map : maps) { StressingThread thread = new StressingThread(stats, map); threads.add(thread); executor.execute(thread); } executor.shutdown(); Thread.sleep(5000); int putsAfter5Seconds = stats.succesfullPutsCounter.get(); System.out.println("\nSituation after 5 seconds:"); System.out.println(stats.toString()); executor.awaitTermination(STRESS_TIME_MINUTES, TimeUnit.MINUTES); stats.globalQuit = true; executor.awaitTermination(10, TimeUnit.SECONDS); // give some time to awake and quit executor.shutdownNow(); System.out.println("\nFinal situation:"); System.out.println(stats.toString()); assert !stats.seenFailures : "at least one thread has seen unexpected state"; assert stats.succesfullPutsCounter.get() > 0 : "the lock should have been taken at least once"; assert stats.succesfullPutsCounter.get() > putsAfter5Seconds : "the lock count didn't improve since the first 5 seconds. Deadlock?"; assert stats.succesfullPutsCounter.get() == stats.lockReleasedCounter.get() : "there's a mismatch in acquires and releases count"; assert stats.lockOwnersCounter.get() == 0 : "the lock is still held at test finish"; }
private void checkIsTrue(boolean assertion, String message) { if (assertion == false) { stats.seenFailures = true; System.out.println(message); } }