ExecutionListTest.java revision 1d580d0f6ee4f21eb309ba7b509d2c6d671c4044
11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2007 The Guava Authors 31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); 51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * you may not use this file except in compliance with the License. 61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * You may obtain a copy of the License at 71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0 91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software 111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS, 121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * See the License for the specific language governing permissions and 141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License. 151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.util.concurrent; 181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor; 201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.testing.NullPointerTester; 221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.util.concurrent.ExecutionList; 231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport junit.framework.TestCase; 251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.CountDownLatch; 271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.Executor; 281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.Executors; 291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.concurrent.TimeUnit; 301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/** 321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unit tests for {@link ExecutionList}. 331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Nishant Thakkar 351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Sven Mawson 361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpublic class ExecutionListTest extends TestCase { 381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert protected ExecutionList list = new ExecutionList(); 401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert protected Executor exec = Executors.newCachedThreadPool(); 411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testRunOnPopulatedList() throws Exception { 431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert CountDownLatch countDownLatch = new CountDownLatch(3); 441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert list.add(new MockRunnable(countDownLatch), exec); 451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert list.add(new MockRunnable(countDownLatch), exec); 461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert list.add(new MockRunnable(countDownLatch), exec); 471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertEquals(countDownLatch.getCount(), 3L); 481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert list.execute(); 501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // Verify that all of the runnables execute in a reasonable amount of time. 521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(countDownLatch.await(1L, TimeUnit.SECONDS)); 531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testAddAfterRun() throws Exception { 561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // Run the previous test 571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert testRunOnPopulatedList(); 581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // If it passed, then verify an Add will be executed without calling run 601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert CountDownLatch countDownLatch = new CountDownLatch(1); 611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert list.add(new MockRunnable(countDownLatch), exec); 621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert assertTrue(countDownLatch.await(1L, TimeUnit.SECONDS)); 631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private class MockRunnable implements Runnable { 661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert CountDownLatch countDownLatch; 671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert MockRunnable(CountDownLatch countDownLatch) { 691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert this.countDownLatch = countDownLatch; 701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void run() { 741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert countDownLatch.countDown(); 751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testExceptionsCaught() { 791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert ExecutionList list = new ExecutionList(); 801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert list.add(THROWING_RUNNABLE, sameThreadExecutor()); 811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert list.execute(); 821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert list.add(THROWING_RUNNABLE, sameThreadExecutor()); 831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public void testNulls() throws Exception { 861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert NullPointerTester tester = new NullPointerTester(); 871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert tester.setDefault(Executor.class, sameThreadExecutor()); 881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert tester.setDefault(Runnable.class, DO_NOTHING); 891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert tester.testAllPublicInstanceMethods(new ExecutionList()); 901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static final Runnable THROWING_RUNNABLE = new Runnable() { 931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public void run() { 941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert throw new RuntimeException(); 951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private static final Runnable DO_NOTHING = new Runnable() { 981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override public void run() { 991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert }; 1011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert} 102