Package org.apache.niolex.commons.test

Examples of org.apache.niolex.commons.test.Counter


    }

    public static void test() throws InterruptedException {
        hashMap = new HashMap<Integer, Counter>();
        for (int i = 0; i < locksNum; ++i) {
            hashMap.put(i, new Counter());
        }
        long tt = 0;
        for (int i = 0; i < 20; ++i) {
            tt += main();
        }
View Full Code Here


        }
    }

    public static void inc() {
        int key = MockUtil.randInt(locksNum);
        Counter c = hashMap.get(key);
        synchronized (c) {
            // intentionally do this.
            c.set(c.cnt() + 1);
            SystemUtil.sleep(1);
        }
    }
View Full Code Here

        wait.waitForResult(2000);
        assertEquals(1, ad.cnt());
        assertEquals(1, monitor.currentChildren().size());
        monitor.removeListener(cli);

        final Counter cld = new Counter();
        final Blocker<String> blocker = new Blocker<String>();
        ChildrenListener cli = new ChildrenListener() {

            @Override
            public void notify(EventType type, long happenTime) {
                blocker.release("s", "");
            }

            @Override
            public void childrenChange(EventType type, List<String> list) {
                if (type == EventType.NOT_DIR)
                    cld.set(1);
                blocker.release("s", "");
                System.out.println(type + " " + list);
            }
        };
        DirMonitor monitor2 = new DirMonitor(1, TMP + "/dir-monitor/not-dir.txt");
        monitor2.addListener(cli);
        wait = blocker.init("s");

        FileUtil.setCharacterFileContentToFileSystem(TMP + "/dir-monitor/not-dir.txt", "It's not a DIR", StringUtil.US_ASCII);
        wait.waitForResult(1000);
        assertEquals(1, cld.cnt());
        boolean b = monitor2.removeListener(cli);
        assertTrue(b);
        assertFalse(monitor2.isDir());
        FileOutputStream fo = new FileOutputStream(TMP + "/dir-monitor/not-dir.txt");
        wait = blocker.init("s");
View Full Code Here

   * Test method for {@link org.apache.niolex.commons.concurrent.Blocker#waitForResult(java.lang.Object, long)}.
   * @throws InterruptedException
   */
  @Test
  public void testWaitForResult() throws InterruptedException {
    final Counter c = new Counter();
    final CountDownLatch cl = new CountDownLatch(1);
    Thread t = new Thread() {
      public void run() {
        try {
            cl.countDown();
          int k = blocker.waitForResult("man", 1000);
          assertEquals(1546, k);
          c.inc();
        } catch (Exception e) {
        }
      }
    };

    t.start();
    cl.await();
    Thread.sleep(10);
    blocker.release("man", 1546);
    t.join();
    assertEquals(1, c.cnt());
  }
View Full Code Here

   * Test method for {@link org.apache.niolex.commons.concurrent.Blocker#release(java.lang.Object, java.lang.Exception)}.
   * @throws InterruptedException
   */
  @Test
  public void testReleaseWithException() throws InterruptedException {
    final Counter c = new Counter();
    final WaitOn<Integer> on = blocker.init("man");
    Thread t = new Thread() {
      public void run() {
        try {
          int k = on.waitForResult(100);
          assertEquals(1546, k);
          assertFalse(true);
        } catch (Exception e) {
          assertEquals("J", e.getMessage());
          c.inc();
        }
      }
    };
    t.start();
    blocker.release("man", new BlockerException("J"));
    t.join();
    assertEquals(1, c.cnt());
  }
View Full Code Here

     * Test method for {@link org.apache.niolex.commons.bean.MutableOne#MutableOne()}.
     */
    @Test
    public void testMutableOne() {
        MutableOne<String> one = new MutableOne<String>();
        final Counter cnt = new Counter();
        MutableOne.DataChangeListener<String> li = new MutableOne.DataChangeListener<String>() {
            @Override
            public void onDataChange(String old, String one) {
                System.out.println("New data -- " + one);
                cnt.inc();
            }
        };
        one.addListener(li);
        one.updateData("Not yet implemented");
        one.removeListener(li);
        one.updateData("DOIFDOIFDOIJFDOJFDO");
        assertEquals(1, cnt.cnt());
        assertEquals("DOIFDOIFDOIJFDOJFDO", one.data());
    }
View Full Code Here

     * Test method for {@link org.apache.niolex.commons.bean.MutableOne#MutableOne(java.lang.Object)}.
     */
    @Test
    public void testMutableOneT() {
        MutableOne<String> one = new MutableOne<String>("A");
        final Counter cnt = new Counter();
        MutableOne.DataChangeListener<String> li = new MutableOne.DataChangeListener<String>() {
            @Override
            public void onDataChange(String old, String one) {
                System.out.println("New data -- " + one);
                cnt.inc();
            }
        };
        one.addListener(li);
        one.updateData("Not yet implemented");
        one.removeListener(li);
        one.updateData("DOIFDOIFDOIJFDOJFDO");
        assertEquals(1, cnt.cnt());
        assertEquals("DOIFDOIFDOIJFDOJFDO", one.data());
    }
View Full Code Here

     * Test method for {@link org.apache.niolex.commons.bean.MutableOne#addListener(org.apache.niolex.commons.bean.MutableOne.DataChangeListener)}.
     */
    @Test
    public void testAddListener() {
        MutableOne<String> one = new MutableOne<String>();
        final Counter cnt = new Counter();
        MutableOne.DataChangeListener<String> li = new MutableOne.DataChangeListener<String>() {
            @Override
            public void onDataChange(String old, String one) {
                System.out.println("New data -- " + one);
                cnt.inc();
            }
        };
        one.addListener(li);
        one.addListener(li);
        one.updateData("Not yet implemented");
        one.removeListener(li);
        one.updateData("DOIFDOIFDOIJFDOJFDO");
        assertEquals(3, cnt.cnt());
        assertEquals("DOIFDOIFDOIJFDOJFDO", one.data());
    }
View Full Code Here

     * Test method for {@link org.apache.niolex.commons.bean.MutableOne#removeListener(org.apache.niolex.commons.bean.MutableOne.DataChangeListener)}.
     */
    @Test
    public void testRemoveListener() {
        MutableOne<String> one = new MutableOne<String>();
        final Counter cnt = new Counter();
        MutableOne.DataChangeListener<String> li = new MutableOne.DataChangeListener<String>() {
            @Override
            public void onDataChange(String old, String one) {
                System.out.println("New data -- " + one);
                cnt.inc();
            }
        };
        one.addListener(li);
        one.updateData("Not yet implemented");
        assertTrue(one.removeListener(li));
        assertFalse(one.removeListener(li));
        one.updateData("DOIFDOIFDOIJFDOJFDO");
        assertEquals(1, cnt.cnt());
        assertEquals("DOIFDOIFDOIJFDOJFDO", one.data());
    }
View Full Code Here

TOP

Related Classes of org.apache.niolex.commons.test.Counter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.