### Question:
SerializeReentrantCallsDirectExecutor implements Executor { @Override public void execute(Runnable task) { Preconditions.checkNotNull(task, "'task' must not be null."); if (!executing) { executing = true; try { task.run(); } catch (Throwable t) { log.log(Level.SEVERE, "Exception while executing runnable " + task, t); } finally { if (taskQueue != null) { completeQueuedTasks(); } executing = false; } } else { enqueue(task); } }  @Override void execute(Runnable task);  }### Answer:
@Test public void reentrantCallsShouldBeSerialized() { final List<Integer> callOrder = new ArrayList<>(4); executor.execute(new Runnable() { @Override public void run() { executor.execute(new Runnable() { @Override public void run() { executor.execute(new Runnable() { @Override public void run() { callOrder.add(3); } }); callOrder.add(2); executor.execute(new Runnable() { @Override public void run() { callOrder.add(4); } }); } }); callOrder.add(1); } }); assertEquals(asList(1, 2, 3, 4), callOrder); }
@Test public void executeCanBeRepeated() { final List<Integer> executes = new ArrayList<>(); executor.execute(new Runnable() { @Override public void run() { executes.add(1); } }); executor.execute(new Runnable() { @Override public void run() { executes.add(2); } }); assertEquals(asList(1,2), executes); }
@Test public void reentrantCallsShouldBeSerialized() { final List<Integer> callOrder = new ArrayList<Integer>(4); executor.execute(new Runnable() { @Override public void run() { executor.execute(new Runnable() { @Override public void run() { executor.execute(new Runnable() { @Override public void run() { callOrder.add(3); } }); callOrder.add(2); executor.execute(new Runnable() { @Override public void run() { callOrder.add(4); } }); } }); callOrder.add(1); } }); assertEquals(asList(1, 2, 3, 4), callOrder); }
@Test public void exceptionShouldNotCancelQueuedTasks() { final AtomicBoolean executed1 = new AtomicBoolean(); final AtomicBoolean executed2 = new AtomicBoolean(); executor.execute(new Runnable() { @Override public void run() { executor.execute(new Runnable() { @Override public void run() { executed1.set(true); throw new RuntimeException("Two"); } }); executor.execute(new Runnable() { @Override public void run() { executed2.set(true); } }); throw new RuntimeException("One"); } }); assertTrue(executed1.get()); assertTrue(executed2.get()); }
@Test(expected = NullPointerException.class) public void executingNullShouldFail() { executor.execute(null); }
@Test public void executeCanBeRepeated() { final List<Integer> executes = new ArrayList<Integer>(); executor.execute(new Runnable() { @Override public void run() { executes.add(1); } }); executor.execute(new Runnable() { @Override public void run() { executes.add(2); } }); assertEquals(asList(1,2), executes); }
@Test public void interruptDoesNotAffectExecution() { final AtomicInteger callsExecuted = new AtomicInteger(); Thread.currentThread().interrupt(); executor.execute(new Runnable() { @Override public void run() { Thread.currentThread().interrupt(); callsExecuted.incrementAndGet(); } }); executor.execute(new Runnable() { @Override public void run() { callsExecuted.incrementAndGet(); } }); Thread.interrupted(); }