package net.ranides.assira.concurrent;

import net.ranides.assira.functional.special.CancelableConsumer;
import net.ranides.assira.functional.special.CancelableRunnable;
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();

        CancelableRunnable 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();

        CancelableRunnable 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");
    }

    @Test
    public void lazyConsumer() {
        final long ts = System.currentTimeMillis();

        CancelableConsumer<Object> lazy = TaskInvoker.lazy(500, (v) -> {
            System.out.println("t=" + (System.currentTimeMillis()-ts) + " executed" );
        });

        System.out.println( "t=" + (System.currentTimeMillis()-ts));
        lazy.accept("1");
        ManualTestUtils.sleep(1000);

        System.out.println( "t=" + (System.currentTimeMillis()-ts));
        lazy.accept("2");
        ManualTestUtils.sleep(1000);

        System.out.println( "t=" + (System.currentTimeMillis()-ts));
        lazy.accept("3");
        ManualTestUtils.sleep(1000);

        System.out.println( "t=" + (System.currentTimeMillis()-ts) + " end");
    }
}