ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.18 by dl, Tue Jan 20 20:30:08 2004 UTC vs.
Revision 1.23 by dl, Tue Aug 23 12:50:00 2005 UTC

# Line 7 | Line 7
7   */
8  
9   import java.util.concurrent.*;
10 + import java.util.concurrent.atomic.*;
11   import junit.framework.*;
12   import java.util.*;
13  
# Line 36 | Line 37 | public class ThreadPoolExecutorTest exte
37          }
38      }
39  
40 +    static class FailingThreadFactory implements ThreadFactory{
41 +        int calls = 0;
42 +        public Thread newThread(Runnable r){
43 +            if (++calls > 1) return null;
44 +            return new Thread(r);
45 +        }  
46 +    }
47 +    
48 +
49      /**
50       *  execute successfully executes a runnable
51       */
# Line 340 | Line 350 | public class ThreadPoolExecutorTest exte
350              assertSame(q, wq);
351              assertFalse(wq.contains(tasks[0]));
352              assertTrue(wq.contains(tasks[4]));
353 +            for (int i = 1; i < 5; ++i)
354 +                tasks[i].cancel(true);
355 +            p1.shutdownNow();
356          } catch(Exception e) {
357              unexpectedException();
358          } finally {
# Line 1498 | Line 1511 | public class ThreadPoolExecutorTest exte
1511          }
1512      }
1513  
1514 +    /**
1515 +     * Execution continues if there is at least one thread even if
1516 +     * thread factory fails to create more
1517 +     */
1518 +    public void testFailingThreadFactory() {
1519 +        ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1520 +        try {
1521 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1522 +            for (int k = 0; k < 100; ++k) {
1523 +                e.execute(new NoOpRunnable());
1524 +            }
1525 +            Thread.sleep(LONG_DELAY_MS);
1526 +        } catch(Exception ex) {
1527 +            unexpectedException();
1528 +        } finally {
1529 +            joinPool(e);
1530 +        }
1531 +    }
1532 +
1533 +    /**
1534 +     * allowsCoreThreadTimeOut is by default false.
1535 +     */
1536 +    public void testAllowsCoreThreadTimeOut() {
1537 +        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1538 +        assertFalse(tpe.allowsCoreThreadTimeOut());
1539 +        joinPool(tpe);
1540 +    }
1541  
1542 +    /**
1543 +     * allowCoreThreadTimeOut(true) causes idle threads to time out
1544 +     */
1545 +    public void testAllowCoreThreadTimeOut_true() {
1546 +        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1547 +        tpe.allowCoreThreadTimeOut(true);
1548 +        tpe.execute(new NoOpRunnable());
1549 +        try {
1550 +            Thread.sleep(MEDIUM_DELAY_MS);
1551 +            assertEquals(0, tpe.getPoolSize());
1552 +        } catch(InterruptedException e){
1553 +            unexpectedException();
1554 +        } finally {
1555 +            joinPool(tpe);
1556 +        }
1557 +    }
1558 +
1559 +    /**
1560 +     * allowCoreThreadTimeOut(false) causes idle threads not to time out
1561 +     */
1562 +    public void testAllowCoreThreadTimeOut_false() {
1563 +        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1564 +        tpe.allowCoreThreadTimeOut(false);
1565 +        tpe.execute(new NoOpRunnable());
1566 +        try {
1567 +            Thread.sleep(MEDIUM_DELAY_MS);
1568 +            assertTrue(tpe.getPoolSize() >= 1);
1569 +        } catch(InterruptedException e){
1570 +            unexpectedException();
1571 +        } finally {
1572 +            joinPool(tpe);
1573 +        }
1574 +    }
1575 +
1576 +    /**
1577 +     * execute allows the same task to be submitted multiple times, even
1578 +     * if rejected
1579 +     */
1580 +    public void testRejectedRecycledTask() {
1581 +        final int nTasks = 1000;
1582 +        final AtomicInteger nRun = new AtomicInteger(0);
1583 +        final Runnable recycledTask = new Runnable() {
1584 +                public void run() {
1585 +                    nRun.getAndIncrement();
1586 +                } };
1587 +        final ThreadPoolExecutor p =
1588 +            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
1589 +                                   new ArrayBlockingQueue(30));
1590 +        try {
1591 +            for (int i = 0; i < nTasks; ++i) {
1592 +                for (;;) {
1593 +                    try {
1594 +                        p.execute(recycledTask);
1595 +                        break;
1596 +                    }
1597 +                    catch (RejectedExecutionException ignore) {
1598 +                    }
1599 +                }
1600 +            }
1601 +            Thread.sleep(5000); // enough time to run all tasks
1602 +            assertEquals(nRun.get(), nTasks);
1603 +        } catch(Exception ex) {
1604 +            ex.printStackTrace();
1605 +            unexpectedException();
1606 +        } finally {
1607 +            p.shutdown();
1608 +        }
1609 +    }
1610 +            
1611   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines