@Test public void testCounter() throws Exception { final long countResetTimePeriodMs = 200L; final Counter c = new Counter(countResetTimePeriodMs); final int n = DFSUtil.getRandom().nextInt(512) + 512; final List<Future<Integer>> futures = new ArrayList<Future<Integer>>(n); final ExecutorService pool = Executors.newFixedThreadPool(32); try { // increment for(int i = 0; i < n; i++) { futures.add(pool.submit(new Callable<Integer>() { @Override public Integer call() throws Exception { return (int)c.increment(); } })); } // sort and wait for the futures Collections.sort(futures, CMP); } finally { pool.shutdown(); } // check futures Assert.assertEquals(n, futures.size()); for(int i = 0; i < n; i++) { Assert.assertEquals(i + 1, futures.get(i).get().intValue()); } Assert.assertEquals(n, c.getCount()); // test auto-reset Thread.sleep(countResetTimePeriodMs + 100); Assert.assertEquals(1, c.increment()); }
@Test public void testCounter() throws Exception { final long countResetTimePeriodMs = 200L; final Counter c = new Counter(countResetTimePeriodMs); final int n = ThreadLocalRandom.current().nextInt(512) + 512; final List<Future<Integer>> futures = new ArrayList<Future<Integer>>(n); final ExecutorService pool = Executors.newFixedThreadPool(32); try { // increment for(int i = 0; i < n; i++) { futures.add(pool.submit(new Callable<Integer>() { @Override public Integer call() throws Exception { return (int)c.increment(); } })); } // sort and wait for the futures Collections.sort(futures, CMP); } finally { pool.shutdown(); } // check futures Assert.assertEquals(n, futures.size()); for(int i = 0; i < n; i++) { Assert.assertEquals(i + 1, futures.get(i).get().intValue()); } Assert.assertEquals(n, c.getCount()); // test auto-reset Thread.sleep(countResetTimePeriodMs + 100); Assert.assertEquals(1, c.increment()); }