/* This file is part of VoltDB.
 * Copyright (C) 2021 VoltDB Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.voltdb.task;

import static org.junit.Assert.assertTrue;

import java.util.concurrent.TimeUnit;

import org.junit.Test;

public class TestIntervalGenerators {
    @Test
    public void cron() throws Exception {
        CronIntervalGenerator cron = new CronIntervalGenerator();
        cron.initialize("* * * * * *");

        // Cron schedule is for every second so it should be at most a second in the future so check it is 1ms
        Interval interval = cron.getFirstInterval();
        long intervalNs = interval.getInterval(TimeUnit.NANOSECONDS);
        assertTrue(intervalNs > TimeUnit.MILLISECONDS.toNanos(1));
        assertTrue(intervalNs <= TimeUnit.SECONDS.toNanos(1));

        interval = interval.getCallback().apply(null);
        intervalNs = interval.getInterval(TimeUnit.NANOSECONDS);
        assertTrue(intervalNs > TimeUnit.MILLISECONDS.toNanos(1));
        assertTrue(intervalNs <= TimeUnit.SECONDS.toNanos(1));
    }

    @Test
    public void interval() throws Exception {
        IntervalIntervalGenerator intervalGenerator = new IntervalIntervalGenerator();
        intervalGenerator.initialize(null, 5, TimeUnit.MINUTES.name());

        Interval interval = intervalGenerator.getFirstInterval();
        long firstIntervalNs = interval.getInterval(TimeUnit.NANOSECONDS);
        // First scheduled interval will has jitter randomly generated so this is 1/2 actual interval
        assertTrue(firstIntervalNs > TimeUnit.SECONDS.toNanos(148));
        assertTrue(firstIntervalNs <= TimeUnit.MINUTES.toNanos(5));

        interval = interval.getCallback().apply(null);
        long intervalNs = interval.getInterval(TimeUnit.NANOSECONDS);
        assertTrue(intervalNs > firstIntervalNs + TimeUnit.MINUTES.toNanos(5) - TimeUnit.SECONDS.toNanos(2));
        assertTrue(intervalNs < firstIntervalNs + TimeUnit.MINUTES.toNanos(5) + TimeUnit.SECONDS.toNanos(2));
    }

    @Test
    public void delay() throws Exception {
        DelayIntervalGenerator delay = new DelayIntervalGenerator();
        delay.initialize(null, 30, TimeUnit.SECONDS.name());

        Interval interval = delay.getFirstInterval();
        long intervalNs = interval.getInterval(TimeUnit.NANOSECONDS);
        assertTrue(intervalNs > TimeUnit.SECONDS.toNanos(29));
        assertTrue(intervalNs < TimeUnit.SECONDS.toNanos(31));

        interval = interval.getCallback().apply(null);
        intervalNs = interval.getInterval(TimeUnit.NANOSECONDS);
        assertTrue(intervalNs > TimeUnit.SECONDS.toNanos(29));
        assertTrue(intervalNs < TimeUnit.SECONDS.toNanos(31));
    }
}

@@ -69,340 +69,634 @@
 %0A * 
-This program is free software: you can redistribute it and/or modify
+Permission is hereby granted, free of charge, to any person obtaining%0A * a copy of this software and associated documentation files (the%0A * %22Software%22), to deal in the Software without restriction, including
 %0A * 
+w
 it
- under the terms of the GNU Affero General Public L
+hout limitation the rights to use, copy, modify, merge, publish,%0A * distribute, subl
 icense
+,
  a
-s%0A * published by the Free Software Foundation, either v
+nd/or sell copies of the Software, and to%0A * permit p
 ers
-i
 on
- 3 of the%0A * License, or (at your option) any later version.%0A *%0A * This program is distributed in the hope that it will be useful,%0A * but
+s to whom the Software is furnished to do so, subject to%0A * the following conditions:%0A *%0A * The above copyright notice and this permission notice shall be%0A * included in all copies or substantial portions of the Software.%0A *%0A * THE SOFTWARE IS PROVIDED %22AS IS%22,
  WIT
@@ -704,58 +704,99 @@
 OUT 
+WARR
 AN
+T
 Y 
-WARRANTY; without even the implied warranty of
+OF ANY KIND,%0A * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 %0A * 
@@ -810,19 +810,17 @@
 TABILITY
- or
+,
  FITNESS
@@ -848,218 +848,278 @@
 POSE
-.  See the%0A * GNU Affero General Public License for more details.%0A *%0A * You should have received a copy of the GNU Affero General Public License%0A * along with VoltDB.  If not, see %3Chttp://www.gnu.org/licenses/%3E
+ AND NONINFRINGEMENT.%0A * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR%0A * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,%0A * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR%0A * OTHER DEALINGS IN THE SOFTWARE
 .%0A *

Fix license