<NME> WaiterTest.java
<BEF> package net.jodah.concurrentunit;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.io.IOException;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

import org.testng.annotations.Test;

/**
 * Tests {@link Waiter}.
 */
@Test
public class WaiterTest {
  @Test
  public void shouldSupportMultipleThreads() throws Throwable {
    final Waiter waiter = new Waiter();

    for (int i = 0; i < 5; i++)
      new Thread(new Runnable() {
        public void run() {
          waiter.assertTrue(true);
  }

  /**
   * Should throw an exception.
   */
  public void waitShouldSupportResume() throws Throwable {
    final Waiter w = new Waiter();

    new Thread(new Runnable() {
      public void run() {
        waitForMainThread();
        w.resume();
      }
    }).start();
    new Thread(new Runnable() {
      public void run() {
        w.resume();
      }
    }).start();

    w.await();
  }

  /**
   * Should throw an assertion error caused by the failure.
   */
  public void waitShouldSupportFail() throws Throwable {
    final Waiter w = new Waiter();

    new Thread(new Runnable() {
      public void run() {
        w.fail(new IllegalArgumentException());
      }
    }).start();

    try {
      w.await();
      fail();
    } catch (AssertionError e) {
      assertTrue(e.getCause() instanceof IllegalArgumentException);

    new Thread(new Runnable() {
      public void run() {
        waitForMainThread();
        w.assertTrue(false);
      }
    }).start();
  @Test(expectedExceptions = AssertionError.class)
  public void waitShouldSupportAssertionErrors() throws Throwable {
    final Waiter w = new Waiter();

  /**
   * Should timeout.
   * 
   * @throws Throwable
   */
  @Test(expectedExceptions = TimeoutException.class)
  public void waitShouldSupportTimeouts() throws Throwable {

    w.await();
  }

  /**
   * Ensures that waiting for multiple resumes works as expected.
   * 
   * @throws Throwable
   */
  public void shouldSupportMultipleResumes() throws Throwable {
    final Waiter w = new Waiter();

    new Thread(new Runnable() {
      public void run() {
        waitForMainThread();
        for (int i = 0; i < 5; i++)
          w.resume();
      }
   */
  public void shouldSupportMultipleResumes() throws Throwable {
    final Waiter w = new Waiter();

    new Thread(new Runnable() {
      public void run() {
        for (int i = 0; i < 5; i++)

    new Thread(new Runnable() {
      public void run() {
        waitForMainThread();
        for (int i = 0; i < 5; i++)
          w.resume();
      }

  public void shouldSupportThreadWait0WithResumeCount() throws Throwable {
    final Waiter w = new Waiter();

    new Thread(new Runnable() {
      public void run() {
        for (int i = 0; i < 5; i++)
          w.resume();
      }
    }).start();

    w.await(0, 5);
  }


    new Thread(new Runnable() {
      public void run() {
        waitForMainThread();
        w.assertNull("test");
      }
    }).start();
  @Test(expectedExceptions = AssertionError.class)
  public void shouldFailNullAssertionFromWorkerThreadWithReason() throws Throwable {
    final Waiter w = new Waiter();

    new Thread(new Runnable() {
      public void run() {
        w.assertNull("test");
      }
    }).start();

    w.await();
  }

  public void shouldHandleResumesThenAwait() throws Throwable {
    final Waiter w = new Waiter();
    final AtomicInteger expectedResumes = new AtomicInteger();

    new Thread(new Runnable() {
      public void run() {
        try {
          w.resume();
          expectedResumes.incrementAndGet();
          Thread.sleep(400);
          expectedResumes.incrementAndGet();
          w.resume();
        } catch (InterruptedException e) {
        }
      }
    }).start();

    Thread.sleep(200);
    w.await(0, 2);
    assertEquals(expectedResumes.get(), 2, "Expected two resumes from separate thread");
  }

  @Test(expectedExceptions = AssertionError.class)
  public void shouldHandleFailThenAwait() throws Throwable {
    final Waiter w = new Waiter();

    new Thread(new Runnable() {
      public void run() {
        w.fail();
      }
    }).start();

    Thread.sleep(400);
    w.await(0, 5);
  }

  @Test(expectedExceptions = AssertionError.class)
  public void shouldHandleResumeFailAwait() throws Throwable {
    final Waiter w = new Waiter();

    new Thread(new Runnable() {
      public void run() {
        try {
          w.resume();
          w.fail();
        } catch (Throwable ignore) {
        }
      }
    }).start();

    Thread.sleep(400);
    w.await();
  }

  @Test(expectedExceptions = AssertionError.class)
  public void shouldHandleFailResumeAwait() throws Throwable {
    final Waiter w = new Waiter();

    new Thread(new Runnable() {
      public void run() {
        try {
          w.fail();
        } catch (Throwable ignore) {
        }
        w.resume();
      }
    }).start();

    Thread.sleep(400);
    w.await();
  }
  
  @Test(expectedExceptions = IOException.class)
  public void shouldRethrow() throws Throwable {
    final Waiter w = new Waiter();
    w.resume();
    w.await();
  }

  /**
   * Waits momentarily to allow the main thread to start blocking.
   */
  private void waitForMainThread() {
    try {
      Thread.sleep(250);
    } catch (InterruptedException e) {
    }
  }
}
      }
    }).start();

    w.await();
  }

  @Test(expectedExceptions = InterruptedException.class)
  public void shouldHandleInterruption() throws Throwable {
    final Waiter w = new Waiter();
    final Thread main = Thread.currentThread();
    new Thread(new Runnable() {
      public void run() {
        main.interrupt();
      }
    }).start();
    w.await(1000);
  }

  public void shouldSupportSuccessiveResumeAwaits() throws Throwable {
    final Waiter w = new Waiter();
    w.resume();
    w.await();
    w.resume();
    w.await();
  }

  public void shouldSupportSuccessiveResumes() throws Throwable {
    final Waiter w = new Waiter();
    w.resume();
    w.resume();
    w.resume();
    w.await();
  }
}

<MSG> Removed waitForMainThread stuff in tests.

<DFF> @@ -26,14 +26,13 @@ public class WaiterTest {
   }
 
   /**
-   * Should throw an exception.
+   * Should support resume.
    */
   public void waitShouldSupportResume() throws Throwable {
     final Waiter w = new Waiter();
 
     new Thread(new Runnable() {
       public void run() {
-        waitForMainThread();
         w.resume();
       }
     }).start();
@@ -66,7 +65,6 @@ public class WaiterTest {
 
     new Thread(new Runnable() {
       public void run() {
-        waitForMainThread();
         w.assertTrue(false);
       }
     }).start();
@@ -76,8 +74,6 @@ public class WaiterTest {
 
   /**
    * Should timeout.
-   * 
-   * @throws Throwable
    */
   @Test(expectedExceptions = TimeoutException.class)
   public void waitShouldSupportTimeouts() throws Throwable {
@@ -87,15 +83,12 @@ public class WaiterTest {
 
   /**
    * Ensures that waiting for multiple resumes works as expected.
-   * 
-   * @throws Throwable
    */
   public void shouldSupportMultipleResumes() throws Throwable {
     final Waiter w = new Waiter();
 
     new Thread(new Runnable() {
       public void run() {
-        waitForMainThread();
         for (int i = 0; i < 5; i++)
           w.resume();
       }
@@ -109,7 +102,6 @@ public class WaiterTest {
 
     new Thread(new Runnable() {
       public void run() {
-        waitForMainThread();
         for (int i = 0; i < 5; i++)
           w.resume();
       }
@@ -130,7 +122,6 @@ public class WaiterTest {
 
     new Thread(new Runnable() {
       public void run() {
-        waitForMainThread();
         w.assertNull("test");
       }
     }).start();
@@ -225,14 +216,4 @@ public class WaiterTest {
     w.resume();
     w.await();
   }
-
-  /**
-   * Waits momentarily to allow the main thread to start blocking.
-   */
-  private void waitForMainThread() {
-    try {
-      Thread.sleep(250);
-    } catch (InterruptedException e) {
-    }
-  }
 }

{'addition_count': 1, 'commit_subject': 'Removed waitForMainThread stuff in tests.', 'deletion_count': 20, 'file_extension': '.java', 'lang': 'java', 'license': 'apache-2.0', 'repo_name': 'jhalterman/concurrentunit'}