Advertisement
tpeierls

Test BoundedExecutorService

Feb 8th, 2012
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.35 KB | None | 0 0
  1. package com.example.bqe;
  2.  
  3. import java.util.concurrent.Executors;
  4. import java.util.concurrent.ExecutorService;
  5. import java.util.concurrent.RejectedExecutionException;
  6. import java.util.concurrent.TimeUnit;
  7. import java.util.concurrent.atomic.AtomicInteger;
  8.  
  9. import org.junit.*;
  10. import static org.junit.Assert.*;
  11.  
  12.  
  13. public class BoundedExecutorServiceTest {
  14.  
  15.     static final int BOUND = 3;
  16.     static final TimeUnit UNIT = TimeUnit.MILLISECONDS;
  17.  
  18.     @After public void shutdown() throws InterruptedException {
  19.         testCount.incrementAndGet();
  20.         exec.shutdown();
  21.         exec.awaitTermination(BOUND * 100, UNIT);
  22.     }
  23.  
  24.     @Test public void acceptsSingleTask() {
  25.         exec.execute(newTask());
  26.     }
  27.  
  28.     @Test public void rejectsTaskOverLimit() {
  29.         for (int i = 0; i < BOUND; ++i) {
  30.             exec.execute(newTask());
  31.         }
  32.         try {
  33.             exec.execute(newTask());
  34.             fail("task should be rejected");
  35.         } catch (RejectedExecutionException ex) {
  36.             // expected
  37.         }
  38.     }
  39.  
  40.     @Test public void acceptsTaskOverLimitAfterWait() throws InterruptedException {
  41.         for (int i = 0; i < BOUND; ++i) {
  42.             exec.execute(newTask());
  43.         }
  44.         try {
  45.             UNIT.sleep(100); // wait for one task to finish
  46.             exec.execute(newTask());
  47.         } catch (RejectedExecutionException ex) {
  48.             fail("task should not have been rejected");
  49.         }
  50.     }
  51.  
  52.     Runnable newTask() {
  53.         return new Runnable() {
  54.             public void run() {
  55.                 try {
  56.                     System.out.printf("running task %s%n", id);
  57.                     UNIT.sleep(100);
  58.                     System.out.printf("finished task %s%n", id);
  59.                 } catch (InterruptedException ex) {
  60.                     Thread.currentThread().interrupt();
  61.                 }
  62.             }
  63.             private final String id = String.format("%d-%d",
  64.                 testCount.get(), taskCount.incrementAndGet());
  65.             {
  66.                 System.out.printf("created task %s%n", id);
  67.             }
  68.         };
  69.     }
  70.  
  71.     private static AtomicInteger testCount = new AtomicInteger();
  72.     private AtomicInteger taskCount = new AtomicInteger();
  73.  
  74.     private BoundedExecutorService exec =
  75.         new BoundedExecutorService(Executors.newCachedThreadPool(), BOUND);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement