1/* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.google.common.util.concurrent; 18 19import static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor; 20 21import com.google.common.testing.NullPointerTester; 22import com.google.common.util.concurrent.ExecutionList; 23 24import junit.framework.TestCase; 25 26import java.util.concurrent.CountDownLatch; 27import java.util.concurrent.Executor; 28import java.util.concurrent.Executors; 29import java.util.concurrent.TimeUnit; 30 31/** 32 * Unit tests for {@link ExecutionList}. 33 * 34 * @author Nishant Thakkar 35 * @author Sven Mawson 36 */ 37public class ExecutionListTest extends TestCase { 38 39 protected ExecutionList list = new ExecutionList(); 40 protected Executor exec = Executors.newCachedThreadPool(); 41 42 public void testRunOnPopulatedList() throws Exception { 43 CountDownLatch countDownLatch = new CountDownLatch(3); 44 list.add(new MockRunnable(countDownLatch), exec); 45 list.add(new MockRunnable(countDownLatch), exec); 46 list.add(new MockRunnable(countDownLatch), exec); 47 assertEquals(countDownLatch.getCount(), 3L); 48 49 list.execute(); 50 51 // Verify that all of the runnables execute in a reasonable amount of time. 52 assertTrue(countDownLatch.await(1L, TimeUnit.SECONDS)); 53 } 54 55 public void testAddAfterRun() throws Exception { 56 // Run the previous test 57 testRunOnPopulatedList(); 58 59 // If it passed, then verify an Add will be executed without calling run 60 CountDownLatch countDownLatch = new CountDownLatch(1); 61 list.add(new MockRunnable(countDownLatch), exec); 62 assertTrue(countDownLatch.await(1L, TimeUnit.SECONDS)); 63 } 64 65 private class MockRunnable implements Runnable { 66 CountDownLatch countDownLatch; 67 68 MockRunnable(CountDownLatch countDownLatch) { 69 this.countDownLatch = countDownLatch; 70 } 71 72 @Override 73 public void run() { 74 countDownLatch.countDown(); 75 } 76 } 77 78 public void testExceptionsCaught() { 79 ExecutionList list = new ExecutionList(); 80 list.add(THROWING_RUNNABLE, sameThreadExecutor()); 81 list.execute(); 82 list.add(THROWING_RUNNABLE, sameThreadExecutor()); 83 } 84 85 public void testNulls() throws Exception { 86 NullPointerTester tester = new NullPointerTester(); 87 tester.setDefault(Executor.class, sameThreadExecutor()); 88 tester.setDefault(Runnable.class, DO_NOTHING); 89 tester.testAllPublicInstanceMethods(new ExecutionList()); 90 } 91 92 private static final Runnable THROWING_RUNNABLE = new Runnable() { 93 @Override public void run() { 94 throw new RuntimeException(); 95 } 96 }; 97 private static final Runnable DO_NOTHING = new Runnable() { 98 @Override public void run() { 99 } 100 }; 101} 102