package com.github.phantomthief.concurrent;

import static com.github.phantomthief.concurrent.MoreFutures.scheduleWithDynamicDelay;
import static com.google.common.util.concurrent.Uninterruptibles.sleepUninterruptibly;
import static java.time.Duration.ofSeconds;
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.time.Duration;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author w.vela
 * Created on 2019-11-26.
 */
class MoreFuturesDynamicTest {

    private static final Logger logger = LoggerFactory.getLogger(MoreFuturesDynamicTest.class);

    @Disabled
    @Test
    void test() {
        ScheduledExecutorService scheduled = newSingleThreadScheduledExecutor();
        int[] i = {1};
        int[] run = {0};
        Future<?> future = scheduleWithDynamicDelay(scheduled, () -> {
            Duration duration = ofSeconds(i[0]);
            i[0]++;
            return duration;
        }, () -> {
            logger.info("run...");
            run[0]++;
        });
        sleepUninterruptibly(7, SECONDS);
        assertEquals(3, run[0]);
        future.cancel(false);
        sleepUninterruptibly(5, SECONDS);
        assertEquals(3, run[0]);
    }
}

@@ -125,16 +125,107 @@
 cDelay;%0A
+import static com.google.common.util.concurrent.MoreExecutors.shutdownAndAwaitTermination;%0A
 import s
@@ -539,16 +539,75 @@
 tEquals;
+%0Aimport static org.junit.jupiter.api.Assertions.assertTrue;
 %0A%0Aimport
@@ -1635,20 +1635,92 @@
 3, run%5B0%5D);%0A
+        assertTrue(shutdownAndAwaitTermination(scheduled, 1, SECONDS));%0A
     %7D%0A%7D%0A

add test case for shutdown executor for dynamic scheduled executor.