コード例 #1
1
ファイル: DelayerTest.java プロジェクト: theHilikus/JRoboCom
  /**
   * Tests the normal behaviour of the delayer
   *
   * @throws InterruptedException
   * @throws ExecutionException
   */
  @Test(timeOut = 1000)
  public void blockMe() throws InterruptedException, ExecutionException {

    final int BLOCKED_ID = 311;

    Callable<Boolean> blocked =
        new Callable<Boolean>() {

          @Override
          public Boolean call() throws Exception {
            try {
              TU.waitFor(BLOCKED_ID, 2, "blockMe test");
            } catch (Exception exc) {
              return false;
            }

            return true;
          }
        };

    TU.addListener(BLOCKED_ID);
    FutureTask<Boolean> blockingTask = new FutureTask<>(blocked);
    Thread thread = new Thread(blockingTask, "Fake blocking task");
    thread.start();

    while (!thread.isAlive() || thread.getState() != State.WAITING) ;
    // assertEquals(State.BLOCKED, thread.getState(), "Blocked");
    TU.tick();
    assertEquals(State.WAITING, thread.getState(), "Still Blocked");
    TU.tick();
    assertTrue(blockingTask.get(), "Blocking thread finished correctly");
  }
コード例 #2
1
ファイル: DelayerTest.java プロジェクト: theHilikus/JRoboCom
  /**
   * Test the same robot trying to wait twice (not allowed in single thread mode)
   *
   * @throws Throwable
   */
  @Test(expectedExceptions = IllegalArgumentException.class, timeOut = 3000)
  public void testMutipleWaitsSameRobot() throws Throwable {

    TU.addListener(332);

    Callable<Boolean> second =
        new Callable<Boolean>() {

          @Override
          public Boolean call() throws Exception {
            TU.waitFor(332, 4, "testMutipleWaitsSameRobot-second");
            return true;
          }
        };

    FutureTask<Boolean> secondTask = new FutureTask<>(second);
    Thread secondThread = new Thread(secondTask, "Fake second robot");
    secondThread.start();

    while (secondThread.getState() != State.WAITING) {
      Thread.yield();
    }

    TU.waitFor(
        332,
        2,
        "testMutipleWaitsSameRobot"); // should fail since fake second should be blocked already
    try {
      secondTask.get();
    } catch (ExecutionException exc) {
      throw exc.getCause();
    }
    fail("Should throw exception");
  }
コード例 #3
0
ファイル: RobotTest.java プロジェクト: theHilikus/JRoboCom
  /**
   * Tests whether a robot's thread ends when a robot dies
   *
   * @throws InterruptedException
   */
  @Test(timeOut = 1000)
  public void testRobotClean() throws InterruptedException {
    World mockWorld = mock(World.class);
    Player mockPlayer = mock(Player.class);
    Delayer delayer = mock(Delayer.class);
    ChangerBank bank = new ChangerBank();
    Robot TU = new Robot(mockWorld, delayer, new Bank[] {bank, null}, "Test Robot", mockPlayer);
    TU.setEventDispatcher(mock(EventDispatcher.class));

    // clock.addListener(TU.getSerialNumber());
    TU.getData().setActiveState(1);

    Thread thread = new Thread(TU);
    thread.start();

    while (bank.ticking) {
      delayer.tick();
    }

    thread.join();
  }
コード例 #4
0
ファイル: DelayerTest.java プロジェクト: theHilikus/JRoboCom
  /**
   * Multiple users at the same time
   *
   * @throws Throwable
   */
  @Test(
      dependsOnMethods = {"com.github.thehilikus.jrobocom.timing.DelayerTest.blockMe"},
      timeOut = 3000)
  public void testMultipleWaits() throws Throwable {
    TU.addListener(332);
    TU.addListener(333);

    Callable<Boolean> first = new SimulatedRobot(332);
    Callable<Boolean> second = new SimulatedRobot(333);

    FutureTask<Boolean> firstTask = new FutureTask<>(first);
    Thread firstThread = new Thread(firstTask, "Fake first robot");
    firstThread.start();

    while (firstThread.getState() != State.WAITING) {
      Thread.yield();
    }
    assertFalse(firstTask.isDone(), "first robot is waiting");

    FutureTask<Boolean> secondTask = new FutureTask<>(second);
    Thread secondThread = new Thread(secondTask, "Fake second robot");
    secondThread.start();

    while (secondThread.getState() != State.WAITING) {
      Thread.yield();
    }

    assertFalse(firstTask.isDone(), "first robot is still waiting");
    assertFalse(secondTask.isDone(), "Second robot is still waiting");
    TU.tick();
    TU.tick();
    boolean finished = false;
    try {
      finished = secondTask.get();
    } catch (ExecutionException exc) {
      throw exc.getCause();
    }
    assertTrue(finished, "Second thread finished");
  }
コード例 #5
0
ファイル: DelayerTest.java プロジェクト: theHilikus/JRoboCom
 /** Tests signalling a robot that's not registered */
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void testNotRegisteredSignal() {
   TU.waitFor(123, 3, "testNotRegisteredSignal");
 }