Ranides Atterwim %!s(int64=3) %!d(string=hai) anos
pai
achega
9fc1cdac03

+ 2 - 2
assira.commons/src/test/java/net/ranides/assira/observable/ObservableBeanFactoryTest.java

@@ -2,7 +2,7 @@ package net.ranides.assira.observable;
 
 import lombok.extern.slf4j.Slf4j;
 import net.ranides.assira.events.EventDispatcher;
-import net.ranides.assira.junit.TestLogger;
+import net.ranides.assira.junit.ManualTestUtils;
 import org.junit.Test;
 
 import java.util.ArrayList;
@@ -20,7 +20,7 @@ public class ObservableBeanFactoryTest {
         EventDispatcher dispatcher = EventDispatcher.newInstance();
         dispatcher.addEventListener(BeanEvent.Change.class, e -> {
             events.add(e.property() + "=" + e.current());
-            TestLogger.trace(e);
+            ManualTestUtils.trace(e);
         });
 
         SimpleModel model = ObservableBeanFactory.getSystem().newObservable(SimpleModel.class, dispatcher);

+ 16 - 2
assira.core/src/main/java/net/ranides/assira/concurrent/TaskInvoker.java

@@ -74,9 +74,23 @@ public class TaskInvoker {
     }
 
     /**
-     * Executes action after "delay" milliseconds.
+     * Creates object which can be used to execute action after "delay" milliseconds.
      * If the same action is scheduled more than one time, it will cancel previous calls.
-     * Returned object can be used to cancel action.
+     * Returned object can be used to execute or cancel action.
+     *
+     * Example usage:
+     *      action = lazy(1000, () -> code...)
+     *
+     *      action.run()
+     *      ...
+     *      action.run()
+     *
+     * Effect will be dependent on delay between "run" calls.
+     *
+     * It is guaranteed that "code..." will be executed ~500ms after last "run".
+     *
+     * Other "run" calls can cause execution of code, only if delay between them is bigger than 500ms
+     * If not, "code..." execution will be canceled and just moved 500ms into the future.
      *
      * @param delay delay in ms
      * @param action action

+ 59 - 0
assira.core/src/test/java/net/ranides/assira/concurrent/TaskInvokerTest.java

@@ -0,0 +1,59 @@
+package net.ranides.assira.concurrent;
+
+import net.ranides.assira.functional.special.Cancelable;
+import net.ranides.assira.junit.ManualTestUtils;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+@Ignore("manual")
+public class TaskInvokerTest {
+
+    @Test
+    public void lazyOnce() {
+        final long ts = System.currentTimeMillis();
+
+        Cancelable lazy = TaskInvoker.lazy(500, () -> {
+            System.out.println("t=" + (System.currentTimeMillis()-ts) + " executed" );
+        });
+
+        System.out.println( "t=" + (System.currentTimeMillis()-ts));
+        lazy.run();
+        ManualTestUtils.sleep(100);
+
+        System.out.println( "t=" + (System.currentTimeMillis()-ts));
+        lazy.run();
+        ManualTestUtils.sleep(100);
+
+        System.out.println( "t=" + (System.currentTimeMillis()-ts));
+        lazy.run();
+        ManualTestUtils.sleep(100);
+
+        ManualTestUtils.sleep(1000);
+        System.out.println( "t=" + (System.currentTimeMillis()-ts) + " end");
+    }
+
+    @Test
+    public void lazyMany() {
+        final long ts = System.currentTimeMillis();
+
+        Cancelable lazy = TaskInvoker.lazy(500, () -> {
+            System.out.println("t=" + (System.currentTimeMillis()-ts) + " executed" );
+        });
+
+        System.out.println( "t=" + (System.currentTimeMillis()-ts));
+        lazy.run();
+        ManualTestUtils.sleep(1000);
+
+        System.out.println( "t=" + (System.currentTimeMillis()-ts));
+        lazy.run();
+        ManualTestUtils.sleep(1000);
+
+        System.out.println( "t=" + (System.currentTimeMillis()-ts));
+        lazy.run();
+        ManualTestUtils.sleep(1000);
+
+        System.out.println( "t=" + (System.currentTimeMillis()-ts) + " end");
+    }
+}

+ 9 - 1
assira.junit/src/main/java/net/ranides/assira/junit/TestLogger.java

@@ -2,7 +2,15 @@ package net.ranides.assira.junit;
 
 import java.util.Arrays;
 
-public class TestLogger {
+public class ManualTestUtils {
+
+    public static void sleep(long millis) {
+        try {
+            Thread.sleep(millis);
+        } catch (InterruptedException e) {
+            throw TestUtils.rethrow(e);
+        }
+    }
 
     public static void trace(Object message) {
         if(IdeaInner.IDEA) {