コード例 #1
0
  /** Test the instantiation of the ThreadPool and the allocation of the threads */
  @Test
  public void AllocationTest() {

    int expectedThreadCount = 2;
    Note dummyNote = new Note(60, 4, 1, null);
    SongValidator dummyValidator = new SongValidator();

    List<Expirator> expirators = new ArrayList<Expirator>();

    ThreadPool testThreadPool = new ThreadPool(dummyValidator, expectedThreadCount, 100);
    Expirator temporaryExpirator;

    for (int actualThreadCount = 0; actualThreadCount < expectedThreadCount; actualThreadCount++) {
      temporaryExpirator = testThreadPool.getAvailableExpirator();
      if (temporaryExpirator != null) {
        expirators.add(temporaryExpirator);
        try {
          temporaryExpirator.expireNote(
              new NoteEvent(dummyNote, NoteAction.BEGIN, System.currentTimeMillis()));
        } catch (ExpiratorBusyException e) {
          fail("Expirator should not be busy");
        }
      } else {
        fail("There were fewer threads than expected (" + actualThreadCount + ")");
      }
    }

    assertTrue(testThreadPool.getAvailableExpirator() == null);

    // Tear down nicely so validator doesnt get innundated with noteExpired calls
    for (Expirator e : expirators) {
      e.resolveNote();
    }
  }
コード例 #2
0
  /**
   * Test the expiration times of the thread pool
   *
   * @throws ExpiratorBusyException
   * @throws InterruptedException
   */
  @Test
  public void ExpireTest() throws ExpiratorBusyException, InterruptedException {

    SongValidator validator =
        new SongValidator() {
          public void noteExpired(NoteEvent event) {
            // Giving the expiration an acceptable error of +/- 3 milliseconds
            // This is due to Thread.sleep (2 should be the worst case scenario)
            assertTrue(
                (System.currentTimeMillis() - start) < 103
                    && (System.currentTimeMillis() - start) > 97);
          }
        };

    ThreadPool testThreadPool = new ThreadPool(validator, 2, 100);
    Expirator testExpirator = testThreadPool.getAvailableExpirator();
    start = System.currentTimeMillis();
    testExpirator.expireNote(
        new NoteEvent(dummyNote, NoteAction.BEGIN, System.currentTimeMillis()));
    Thread.sleep(200); // This is so we don't terminate before waiting for the expiration
  }