package acceptance;

import org.junit.Test;
import utils.CommandStatus;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static java.util.Arrays.asList;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static utils.TestUtils.addWorkflow;
import static utils.TestUtils.assertCommandStatus;
import static utils.TestUtils.main;

public class ParamSetRedisIT
        extends BaseParamRedisIT
{
    private static long TTL_90_DAYS_PLUS_1SEC = 60 * 60 * 24 * 90 + 1; // 90days + 1sec
    private static long TTL_89_DAYS = 60 * 60 * 24 * 89; // 89days

    @Test
    public void setValueToRedis()
            throws IOException
    {
        Path projectDir = folder.newFolder().toPath();
        addWorkflow(projectDir, "acceptance/params/set.dig");
        Path config = projectDir.resolve("config");
        Files.write(config, asList(
                "param_server.type=redis",
                "param_server.host=" + DIGDAG_TEST_REDIS
        ));

        CommandStatus status = main("run",
                "-o", folder.newFolder().getAbsolutePath(),
                "--config", config.toString(),
                "--project", projectDir.toString(),
                projectDir.resolve("set.dig").toString()
        );
        assertCommandStatus(status);

        long key1_ttl = redisClient.ttl("0:key1");
        long key2_ttl = redisClient.ttl("0:key2");

        assertTrue(key1_ttl < TTL_90_DAYS_PLUS_1SEC && key1_ttl > TTL_89_DAYS);
        assertTrue(key2_ttl < TTL_90_DAYS_PLUS_1SEC && key2_ttl > TTL_89_DAYS);

        assertThat(redisClient.get("0:key1"), is("{\"value_type\":0,\"value\":{\"value\":\"value1\"}}"));
        assertThat(redisClient.get("0:key2"), is("{\"value_type\":0,\"value\":{\"value\":\"value2\"}}"));
    }

    @Test
    public void parallelSetValueToRedis()
            throws IOException
    {
        Path projectDir = folder.newFolder().toPath();
        addWorkflow(projectDir, "acceptance/params/parallel_set.dig");
        Path config = projectDir.resolve("config");
        Files.write(config, asList(
                "param_server.type=redis",
                "param_server.host=" + DIGDAG_TEST_REDIS
        ));

        CommandStatus status = main("run",
                "-o", folder.newFolder().getAbsolutePath(),
                "--config", config.toString(),
                "--project", projectDir.toString(),
                projectDir.resolve("parallel_set.dig").toString()
        );
        assertCommandStatus(status);

        long key1_ttl = redisClient.ttl("0:key1");
        long key2_ttl = redisClient.ttl("0:key2");

        assertTrue(key1_ttl < TTL_90_DAYS_PLUS_1SEC && key1_ttl > TTL_89_DAYS);
        assertTrue(key2_ttl < TTL_90_DAYS_PLUS_1SEC && key2_ttl > TTL_89_DAYS);

        assertThat(redisClient.get("0:key1"), is("{\"value_type\":0,\"value\":{\"value\":\"value1\"}}"));
        assertThat(redisClient.get("0:key2"), is("{\"value_type\":0,\"value\":{\"value\":\"value2\"}}"));
    }
}

@@ -552,26 +552,16 @@
 _90_DAYS
-_PLUS_1SEC
  = 60 * 
@@ -572,20 +572,16 @@
  24 * 90
- + 1
 ; // 90d
@@ -587,15 +587,8 @@
 days
- + 1sec
 %0A   
@@ -1483,32 +1483,33 @@
 tTrue(key1_ttl %3C
+=
  TTL_90_DAYS_PLU
@@ -1500,34 +1500,24 @@
  TTL_90_DAYS
-_PLUS_1SEC
  && key1_ttl
@@ -1554,32 +1554,33 @@
 tTrue(key2_ttl %3C
+=
  TTL_90_DAYS_PLU
@@ -1571,34 +1571,24 @@
  TTL_90_DAYS
-_PLUS_1SEC
  && key2_ttl
@@ -2685,24 +2685,25 @@
 e(key1_ttl %3C
+=
  TTL_90_DAYS
@@ -2698,34 +2698,24 @@
  TTL_90_DAYS
-_PLUS_1SEC
  && key1_ttl
@@ -2760,16 +2760,17 @@
 y2_ttl %3C
+=
  TTL_90_
@@ -2777,18 +2777,8 @@
 DAYS
-_PLUS_1SEC
  && 

Use <= instead of <