Exemple #1
0
  @Test
  public void constraintTest() {
    Debug.testStart("Constraint");

    final int COUNT = 100;

    final TestMonitorable mon = new TestMonitorable();

    final Random rand = new Random();

    final Predicate<TestMonitorable> pred1 = m -> m.get() < COUNT;
    final Predicate<TestMonitorable> pred2 = m -> m.get() % 2 == 0;

    final Monitor<TestMonitorable> monitor1 = new ConstraintMonitor<>(pred1);
    mon.addChangeMonitor(monitor1);
    final Monitor<TestMonitorable> monitor2 = Monitors.monitor(mon).constraint(pred2);

    for (int i = 0; i < COUNT; i++) {
      boolean failed = false;
      try {
        mon.increment();
      } catch (final UnprotectedMonitorException cve) {
        failed = true;
      }
      final boolean shouldFail = !pred1.test(mon) || !pred2.test(mon);

      if (failed) {
        if (!shouldFail) throw new Error("False negative at " + mon.get());
      } else if (shouldFail) throw new Error("False positive at " + mon.get());
    }

    Debug.testEnd("Constraint");
  }
Exemple #2
0
  @Test
  public void manyMonitorsTest() {
    Debug.testStart("Many Monitors");

    final long time = System.currentTimeMillis();

    final int COUNT = 10000;

    final TestMonitorable mon = new TestMonitorable();

    final Random rand = new Random();

    final TestToggleMonitor[] monitors = new TestToggleMonitor[COUNT];

    for (int i = 0; i < monitors.length; i++) {
      monitors[i] = new TestToggleMonitor();
      mon.addChangeMonitor(monitors[i]);
    }

    for (int i = 0; i < 100; i++) {
      final int index = rand.nextInt(COUNT);
      final TestToggleMonitor replacement = new TestToggleMonitor();
      if (rand.nextFloat() > .5) {
        mon.removeChangeMonitor(monitors[i]);
        mon.addChangeMonitor(replacement);
      } else {
        mon.addChangeMonitor(replacement);
        mon.removeChangeMonitor(monitors[i]);
      }
      monitors[i] = replacement;
    }

    mon.increment();

    for (final TestToggleMonitor monitor : monitors)
      if (!monitor.toggle) throw new Error("Monitor was not triggered");

    Debug.printLn("Millis (Benchmark): " + (System.currentTimeMillis() - time));

    Debug.testEnd("Many Monitors");
  }