コード例 #1
0
ファイル: MonitorTest.java プロジェクト: Etherous/Loom
  @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");
  }
コード例 #2
0
ファイル: MonitorTest.java プロジェクト: Etherous/Loom
 @Override
 public void accept(final TestMonitorable t) {
   this.lastVal = t.get();
   Debug.printLn("R:" + t.get());
   Monitors.monitor(t)
       .when(m -> m.get() == this.lastVal + 1, new TestRecursiveMonitor())
       .execute();
 }
コード例 #3
0
ファイル: MonitorTest.java プロジェクト: Etherous/Loom
  public void activeFilterTest(
      final Filter<TestMonitorable> f, final Predicate<TestMonitorable> pred) {
    Debug.testStart("Active Filter: " + f.getClass().getSimpleName());

    final int COUNT = 10000;

    final TestMonitorable[] mons = new TestMonitorable[COUNT];

    final Random rand = new Random();

    for (int i = 0; i < COUNT; i++) mons[i] = new TestMonitorable(rand.nextInt(1000));

    for (final TestMonitorable m : mons) f.add(m);

    for (final TestMonitorable m : f.trueSet())
      if (!pred.test(m)) throw new Error("False positive");
    for (final TestMonitorable m : f.falseSet())
      if (pred.test(m)) throw new Error("False negative");

    final long startTime = System.currentTimeMillis();
    for (int i = 0; i < COUNT; i++) {
      final TestMonitorable mon = mons[rand.nextInt(COUNT)];
      if (rand.nextFloat() > .5) mon.increment();
      else mon.decrement();
    }
    final long endTime = System.currentTimeMillis();
    Debug.printLn("Time: " + ((endTime - startTime) / 1000.) + " sec");

    if (f.trueSet().size() + f.falseSet().size() != COUNT) throw new Error("Size is incorrect");

    for (final TestMonitorable m : f.trueSet())
      if (!pred.test(m)) throw new Error("False positive");
    for (final TestMonitorable m : f.falseSet())
      if (pred.test(m)) throw new Error("False negative");

    Debug.testEnd("Active Filter: " + f.getClass().getSimpleName());
  }
コード例 #4
0
ファイル: MonitorTest.java プロジェクト: Etherous/Loom
  public void testSortedSet(final SortedSet<TestMonitorable> s) {
    Debug.testStart("Active Sorted Set: " + s.getClass().getSimpleName());

    final int COUNT = 10000;

    final TestMonitorable[] mons = new TestMonitorable[COUNT];

    final Random rand = new Random();

    for (int i = 0; i < COUNT; i++) mons[i] = new TestMonitorable(rand.nextInt());

    for (final TestMonitorable m : mons) s.add(m);

    final long startTime = System.currentTimeMillis();
    for (int i = 0; i < COUNT; i++) {
      if (rand.nextFloat() > .5) mons[rand.nextInt(COUNT)].increment();
      else mons[rand.nextInt(COUNT)].decrement();
    }
    final long endTime = System.currentTimeMillis();
    Debug.printLn("Time: " + ((endTime - startTime) / 1000.) + " sec");

    if (s.size() != COUNT)
      throw new Error(
          "Set is missing " + (COUNT - s.size()) + " elements (may be due to duplicates)");

    int last = Integer.MIN_VALUE;
    for (final TestMonitorable m : s.toArray(mons)) {
      if (m == null) break;
      // Debug.printDel (m.get ());
      if (last > m.get()) throw new Error("ActiveSortedSet out of order");
      last = m.get();
    }
    // Debug.printLn ("");

    Debug.testEnd("Active Sorted Set: " + s.getClass().getSimpleName());
  }
コード例 #5
0
ファイル: MonitorTest.java プロジェクト: Etherous/Loom
  @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");
  }
コード例 #6
0
ファイル: MonitorTest.java プロジェクト: Etherous/Loom
  // Tests a wide spectrum of the Monitors utility class
  @Test
  public void wideChainTest() {
    Debug.testStart("Monitor Chaining");

    final TestMonitorable mon = new TestMonitorable();
    final int COUNT = 100;

    // Cascade test
    final Monitor<?> m1 = Monitors.monitor(mon).cascade(m -> Debug.printDel(m.get()));

    // When test
    final Monitor<?> m2 =
        Monitors.monitor(mon)
            .when(
                m -> m.get() == 50,
                m -> {
                  Debug.printDelAssert("When 50: " + m.get(), m.get() == 50);
                  this.wideChainTest_when50++;
                })
            .execute();

    // Until test
    final Monitor<?> m3 =
        Monitors.monitor(mon)
            .until(
                m -> m.get() == 10,
                m -> {
                  Debug.printDelAssert("Until 10: " + m.get(), m.get() < 10);
                  this.wideChainTest_until10++;
                })
            .execute();

    {
      final SimpleMonitorChain<TestMonitorable> chain =
          Monitors.monitor(mon)
              .recover(
                  (e, t) -> {
                    throw new RuntimeException(e);
                  });

      // Test of chained When and Until
      chain
          .when(m -> m.get() == 50)
          .until(
              m -> m.get() == 60,
              m -> {
                Debug.printDelAssert("In 50: " + m.get(), m.get() >= 50 && m.get() < 60);
                this.wideChainTest_in50++;
              });
      // mon.watchLifecycle
      // (((SimpleMonitorChain<TestMonitorable>.MyMonitorPart) chain
      // .getLast ()).get ());

      // Test of chained When-Until-When
      chain.when(
          new Predicate<TestMonitorable>() {
            int count = 0;

            @Override
            public boolean test(final TestMonitorable t) {
              return this.count++ == 20;
            }
          },
          m -> {
            Debug.printDelAssert("20 more than 60: " + m.get(), m.get() == 80 || m.get() == 81);
            this.wideChainTest_81++;
          });

      // Test of chained When-Until-When-When
      chain.when(
          m -> m.get() == COUNT - 1,
          m -> {
            Debug.printDelAssert("Almost done", m.get() == COUNT - 1);
            this.wideChainTest_almostdone++;
          });

      chain.execute();
    }

    // Recursive test (also tests for skips)
    // Monitors.monitor (mon).when (m -> true, new TestRecursiveMonitor
    // ()).execute ();

    // Loop (two whens that run each other)
    {
      final MonitorChain<TestMonitorable> chain = Monitors.monitor(mon);
      final MonitorChain.Part<TestMonitorable> a =
          chain
              .when(m -> m.get() == COUNT, m -> Debug.printAssert("A", m.get() == COUNT))
              .getLast();
      final MonitorChain.Part<TestMonitorable> b =
          chain
              .when(m -> m.get() == (COUNT - 1), m -> Debug.printAssert("B", m.get() == COUNT - 1))
              .getLast();
      b.next(a);
      chain.execute();
    }
    {
      final MonitorChain<TestMonitorable> chain = Monitors.monitor(mon);
      final MonitorChain.Part<TestMonitorable> a =
          chain
              .when(m -> m.get() == COUNT, m -> Debug.printAssert("A", m.get() == COUNT))
              .getLast();
      final MonitorChain.Part<TestMonitorable> b =
          chain
              .when(m -> m.get() == (COUNT - 1), m -> Debug.printAssert("B", m.get() == COUNT - 1))
              .getLast();
      b.next(a);
      chain.execute();
    }

    // Where the action is
    while (mon.get() < COUNT) mon.increment();
    for (int i = 0; i < 100; i++) {
      mon.decrement();
      mon.increment();
    }

    if (this.wideChainTest_until10 != 9)
      throw new Error("Expected 9 instances of Until 10, but got " + this.wideChainTest_until10);
    if (this.wideChainTest_when50 != 1)
      throw new Error("Expected 1 instance of When 50, but got " + this.wideChainTest_when50);
    if (this.wideChainTest_in50 != 9 && this.wideChainTest_in50 != 10)
      throw new Error("Expected 9 or 10 instances of In 50, but got " + this.wideChainTest_in50);
    if (this.wideChainTest_81 != 1)
      throw new Error("Expected 1 instance of 20 more than 61, but got " + this.wideChainTest_81);
    if (this.wideChainTest_almostdone != 1)
      throw new Error(
          "Expected 1 instance of Almost Done, but got " + this.wideChainTest_almostdone);

    Debug.testEnd("Monitor Chaining");
  }