Example #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();
    }
  }