From d0e62edf6e28b33221730cd020934a1307b0f5f1 Mon Sep 17 00:00:00 2001 From: gjorgievskivlatko Date: Fri, 8 May 2020 17:52:26 +0200 Subject: [PATCH] Bugfix - ServerTimeStatementsSource strategies generate incorrect/ambiguous update statements that can lead to false lock information --- .../Db2ServerTimeStatementsSource.java | 2 +- .../H2ServerTimeStatementsSource.java | 2 +- .../HsqlServerTimeStatementsSource.java | 2 +- .../MsSqlServerTimeStatementsSource.java | 2 +- .../MySqlServerTimeStatementsSource.java | 2 +- .../OracleServerTimeStatementsSource.java | 2 +- ...PostgresSqlServerTimeStatementsSource.java | 2 +- ...stractJdbcTemplateStorageAccessorTest.java | 108 ++++++++++++++++++ .../Db2JdbcTemplateStorageAccessorTest.java | 25 ++++ .../H2JdbcTemplateStorageAccessorTest.java | 25 ++++ .../HsqlJdbcTemplateStorageAccessorTest.java | 25 ++++ ...ariaDbJdbcTemplateStorageAccessorTest.java | 25 ++++ .../MsSqlJdbcTemplateStorageAccessorTest.java | 25 ++++ .../MySqlJdbcTemplateStorageAccessorTest.java | 25 ++++ ...OracleJdbcTemplateStorageAccessorTest.java | 25 ++++ ...stgresJdbcTemplateStorageAccessorTest.java | 64 ++++------- .../test/support/jdbc/MariaDbConfig.java | 2 +- .../test/support/jdbc/MySqlConfig.java | 2 +- 18 files changed, 313 insertions(+), 52 deletions(-) create mode 100644 providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/AbstractJdbcTemplateStorageAccessorTest.java create mode 100644 providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/Db2JdbcTemplateStorageAccessorTest.java create mode 100644 providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/H2JdbcTemplateStorageAccessorTest.java create mode 100644 providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/HsqlJdbcTemplateStorageAccessorTest.java create mode 100644 providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/MariaDbJdbcTemplateStorageAccessorTest.java create mode 100644 providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/MsSqlJdbcTemplateStorageAccessorTest.java create mode 100644 providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/MySqlJdbcTemplateStorageAccessorTest.java create mode 100644 providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/OracleJdbcTemplateStorageAccessorTest.java diff --git a/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/Db2ServerTimeStatementsSource.java b/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/Db2ServerTimeStatementsSource.java index 6631312b5..39e382a89 100644 --- a/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/Db2ServerTimeStatementsSource.java +++ b/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/Db2ServerTimeStatementsSource.java @@ -21,7 +21,7 @@ String getInsertStatement() { @Override public String getUpdateStatement() { - return "UPDATE " + tableName() + " SET " + lockUntil() + " = " + lockAtMostFor + ", " + lockedAt() + " = " + now + ", " + lockedBy() + " = :lockedBy WHERE " + lockUntil() + " <= " + now; + return "UPDATE " + tableName() + " SET " + lockUntil() + " = " + lockAtMostFor + ", " + lockedAt() + " = " + now + ", " + lockedBy() + " = :lockedBy WHERE " + name() + " = :name AND " + lockUntil() + " <= " + now; } @Override diff --git a/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/H2ServerTimeStatementsSource.java b/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/H2ServerTimeStatementsSource.java index 9e70de3c7..0fee5baf8 100644 --- a/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/H2ServerTimeStatementsSource.java +++ b/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/H2ServerTimeStatementsSource.java @@ -21,7 +21,7 @@ String getInsertStatement() { @Override public String getUpdateStatement() { - return "UPDATE " + tableName() + " SET " + lockUntil() + " = " + lockAtMostFor + ", " + lockedAt() + " = " + now + ", " + lockedBy() + " = :lockedBy WHERE " + lockUntil() + " <= " + now; + return "UPDATE " + tableName() + " SET " + lockUntil() + " = " + lockAtMostFor + ", " + lockedAt() + " = " + now + ", " + lockedBy() + " = :lockedBy WHERE " + name() + " = :name AND " + lockUntil() + " <= " + now; } @Override diff --git a/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/HsqlServerTimeStatementsSource.java b/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/HsqlServerTimeStatementsSource.java index d41c4e50f..931239fa3 100644 --- a/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/HsqlServerTimeStatementsSource.java +++ b/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/HsqlServerTimeStatementsSource.java @@ -21,7 +21,7 @@ String getInsertStatement() { @Override public String getUpdateStatement() { - return "UPDATE " + tableName() + " SET " + lockUntil() + " = " + lockAtMostFor + ", " + lockedAt() + " = " + now + ", " + lockedBy() + " = :lockedBy WHERE " + lockUntil() + " <= " + now; + return "UPDATE " + tableName() + " SET " + lockUntil() + " = " + lockAtMostFor + ", " + lockedAt() + " = " + now + ", " + lockedBy() + " = :lockedBy WHERE " + name() + " = :name AND " + lockUntil() + " <= " + now; } @Override diff --git a/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/MsSqlServerTimeStatementsSource.java b/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/MsSqlServerTimeStatementsSource.java index 50ce7417f..6ef04c2a8 100644 --- a/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/MsSqlServerTimeStatementsSource.java +++ b/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/MsSqlServerTimeStatementsSource.java @@ -21,7 +21,7 @@ String getInsertStatement() { @Override public String getUpdateStatement() { - return "UPDATE " + tableName() + " SET " + lockUntil() + " = " + lockAtMostFor + ", " + lockedAt() + " = " + now + ", " + lockedBy() + " = :lockedBy WHERE " + lockUntil() + " <= " + now; + return "UPDATE " + tableName() + " SET " + lockUntil() + " = " + lockAtMostFor + ", " + lockedAt() + " = " + now + ", " + lockedBy() + " = :lockedBy WHERE " + name() + " = :name AND " + lockUntil() + " <= " + now; } @Override diff --git a/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/MySqlServerTimeStatementsSource.java b/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/MySqlServerTimeStatementsSource.java index bf51ddb5f..c6cd80df4 100644 --- a/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/MySqlServerTimeStatementsSource.java +++ b/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/MySqlServerTimeStatementsSource.java @@ -21,7 +21,7 @@ String getInsertStatement() { @Override public String getUpdateStatement() { - return "UPDATE " + tableName() + " SET " + lockUntil() + " = " + lockAtMostFor + ", " + lockedAt() + " = " + now + ", " + lockedBy() + " = :lockedBy WHERE " + lockUntil() + " <= " + now; + return "UPDATE " + tableName() + " SET " + lockUntil() + " = " + lockAtMostFor + ", " + lockedAt() + " = " + now + ", " + lockedBy() + " = :lockedBy WHERE " + name() + " = :name AND " + lockUntil() + " <= " + now; } @Override diff --git a/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/OracleServerTimeStatementsSource.java b/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/OracleServerTimeStatementsSource.java index b32ba5f10..342f83a20 100644 --- a/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/OracleServerTimeStatementsSource.java +++ b/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/OracleServerTimeStatementsSource.java @@ -24,7 +24,7 @@ String getInsertStatement() { @Override public String getUpdateStatement() { - return "UPDATE " + tableName() + " SET " + lockUntil() + " = " + lockAtMostFor + ", " + lockedAt() + " = " + now + ", " + lockedBy() + " = :lockedBy WHERE " + lockUntil() + " <= " + now; + return "UPDATE " + tableName() + " SET " + lockUntil() + " = " + lockAtMostFor + ", " + lockedAt() + " = " + now + ", " + lockedBy() + " = :lockedBy WHERE " + name() + " = :name AND " + lockUntil() + " <= " + now; } @Override diff --git a/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/PostgresSqlServerTimeStatementsSource.java b/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/PostgresSqlServerTimeStatementsSource.java index f3bfabe13..5b918d3df 100644 --- a/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/PostgresSqlServerTimeStatementsSource.java +++ b/providers/jdbc/shedlock-provider-jdbc-template/src/main/java/net/javacrumbs/shedlock/provider/jdbctemplate/PostgresSqlServerTimeStatementsSource.java @@ -22,7 +22,7 @@ String getInsertStatement() { @NonNull private String updateClause() { - return " SET " + lockUntil() + " = " + lockAtMostFor + ", " + lockedAt() + " = " + now + ", " + lockedBy() + " = :lockedBy WHERE " + tableName() + "." + lockUntil() + " <= " + now; + return " SET " + lockUntil() + " = " + lockAtMostFor + ", " + lockedAt() + " = " + now + ", " + lockedBy() + " = :lockedBy WHERE " + tableName() + "." + name() + " = :name AND " + tableName() + "." + lockUntil() + " <= " + now; } @Override diff --git a/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/AbstractJdbcTemplateStorageAccessorTest.java b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/AbstractJdbcTemplateStorageAccessorTest.java new file mode 100644 index 000000000..806c1114f --- /dev/null +++ b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/AbstractJdbcTemplateStorageAccessorTest.java @@ -0,0 +1,108 @@ +package net.javacrumbs.shedlock.provider.jdbctemplate; + +import net.javacrumbs.shedlock.core.LockConfiguration; +import net.javacrumbs.shedlock.support.annotation.NonNull; +import net.javacrumbs.shedlock.test.support.jdbc.DbConfig; +import net.javacrumbs.shedlock.test.support.jdbc.JdbcTestUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import java.sql.Timestamp; +import java.time.Duration; + +import static java.lang.Thread.sleep; +import static org.assertj.core.api.Assertions.assertThat; + +public abstract class AbstractJdbcTemplateStorageAccessorTest { + + private static final String MY_LOCK = "my-lock"; + private static final String OTHER_LOCK = "other-lock"; + + private final JdbcTestUtils testUtils; + + protected AbstractJdbcTemplateStorageAccessorTest(DbConfig dbConfig) { + this.testUtils = new JdbcTestUtils(dbConfig); + } + + @AfterEach + public void cleanup() { + testUtils.clean(); + } + + @Test + void shouldNotUpdateOnInsertIfPreviousDidNotEndWhenNotUsingDbTime() { + shouldNotUpdateOnInsertIfPreviousDidNotEnd(false); + } + + @Test + void shouldNotUpdateOnInsertIfPreviousDidNotEndWhenUsingDbTime() { + shouldNotUpdateOnInsertIfPreviousDidNotEnd(true); + } + + private void shouldNotUpdateOnInsertIfPreviousDidNotEnd(boolean usingDbTime) { + JdbcTemplateStorageAccessor accessor = getAccessor(usingDbTime); + + assertThat( + accessor.insertRecord(new LockConfiguration(MY_LOCK, Duration.ofSeconds(10), Duration.ZERO)) + ).isEqualTo(true); + + Timestamp originalLockValidity = testUtils.getLockedUntil(MY_LOCK); + + assertThat( + accessor.insertRecord(new LockConfiguration(MY_LOCK, Duration.ofSeconds(10), Duration.ZERO)) + ).isEqualTo(false); + + assertThat(testUtils.getLockedUntil(MY_LOCK)).isEqualTo(originalLockValidity); + } + + @Test + void shouldNotUpdateOtherLockConfigurationsWhenNotUsingDbTime() throws InterruptedException { + shouldNotUpdateOtherLockConfigurations(false); + } + + @Test + void shouldNotUpdateOtherLockConfigurationsWhenUsingDbTime() throws InterruptedException { + shouldNotUpdateOtherLockConfigurations(true); + } + + private void shouldNotUpdateOtherLockConfigurations(boolean usingDbTime) throws InterruptedException { + JdbcTemplateStorageAccessor accessor = getAccessor(usingDbTime); + + Duration lockAtMostFor = Duration.ofMillis(10); + assertThat(accessor.insertRecord(new LockConfiguration(MY_LOCK, lockAtMostFor, Duration.ZERO))).isEqualTo(true); + assertThat(accessor.insertRecord(new LockConfiguration(OTHER_LOCK, lockAtMostFor, Duration.ZERO))).isEqualTo(true); + + Timestamp myLockLockedUntil = testUtils.getLockedUntil(MY_LOCK); + Timestamp otherLockLockedUntil = testUtils.getLockedUntil(OTHER_LOCK); + + // wait for a while so there will be a difference in the timestamp + // when system time is used seems there is no milliseconds in the timestamp so to make a difference we have to wait for at least a second + sleep(1000); + + + // act + assertThat(accessor.updateRecord(new LockConfiguration(MY_LOCK, lockAtMostFor, Duration.ZERO))).isEqualTo(true); + + + // assert + assertThat(testUtils.getLockedUntil(MY_LOCK)).isAfter(myLockLockedUntil); + // check that the other lock has not been affected by "my-lock" update + assertThat(testUtils.getLockedUntil(OTHER_LOCK)).isEqualTo(otherLockLockedUntil); + } + + @NonNull + protected JdbcTemplateStorageAccessor getAccessor(boolean usingDbTime) { + JdbcTemplateLockProvider.Configuration.Builder builder = JdbcTemplateLockProvider + .Configuration.builder() + .withJdbcTemplate(testUtils.getJdbcTemplate()); + if (usingDbTime) { + builder.usingDbTime(); + } + + return new JdbcTemplateStorageAccessor(builder.build()); + } + + protected JdbcTestUtils getTestUtils() { + return testUtils; + } +} diff --git a/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/Db2JdbcTemplateStorageAccessorTest.java b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/Db2JdbcTemplateStorageAccessorTest.java new file mode 100644 index 000000000..5c9ec20e1 --- /dev/null +++ b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/Db2JdbcTemplateStorageAccessorTest.java @@ -0,0 +1,25 @@ +package net.javacrumbs.shedlock.provider.jdbctemplate; + +import net.javacrumbs.shedlock.test.support.jdbc.Db2ServerConfig; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; + +class Db2JdbcTemplateStorageAccessorTest extends AbstractJdbcTemplateStorageAccessorTest { + + private static final Db2ServerConfig dbConfig = new Db2ServerConfig(); + + protected Db2JdbcTemplateStorageAccessorTest() { + super(dbConfig); + } + + @BeforeAll + public static void startDb() { + dbConfig.startDb(); + } + + @AfterAll + public static void shutdownDb() { + dbConfig.shutdownDb(); + } + +} diff --git a/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/H2JdbcTemplateStorageAccessorTest.java b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/H2JdbcTemplateStorageAccessorTest.java new file mode 100644 index 000000000..0c546162a --- /dev/null +++ b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/H2JdbcTemplateStorageAccessorTest.java @@ -0,0 +1,25 @@ +package net.javacrumbs.shedlock.provider.jdbctemplate; + +import net.javacrumbs.shedlock.test.support.jdbc.H2Config; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; + +class H2JdbcTemplateStorageAccessorTest extends AbstractJdbcTemplateStorageAccessorTest { + + private static final H2Config dbConfig = new H2Config(); + + protected H2JdbcTemplateStorageAccessorTest() { + super(dbConfig); + } + + @BeforeAll + public static void startDb() { + dbConfig.startDb(); + } + + @AfterAll + public static void shutdownDb() { + dbConfig.shutdownDb(); + } + +} diff --git a/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/HsqlJdbcTemplateStorageAccessorTest.java b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/HsqlJdbcTemplateStorageAccessorTest.java new file mode 100644 index 000000000..66f7d9983 --- /dev/null +++ b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/HsqlJdbcTemplateStorageAccessorTest.java @@ -0,0 +1,25 @@ +package net.javacrumbs.shedlock.provider.jdbctemplate; + +import net.javacrumbs.shedlock.test.support.jdbc.HsqlConfig; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; + +class HsqlJdbcTemplateStorageAccessorTest extends AbstractJdbcTemplateStorageAccessorTest { + + private static final HsqlConfig dbConfig = new HsqlConfig(); + + protected HsqlJdbcTemplateStorageAccessorTest() { + super(dbConfig); + } + + @BeforeAll + public static void startDb() { + dbConfig.startDb(); + } + + @AfterAll + public static void shutdownDb() { + dbConfig.shutdownDb(); + } + +} diff --git a/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/MariaDbJdbcTemplateStorageAccessorTest.java b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/MariaDbJdbcTemplateStorageAccessorTest.java new file mode 100644 index 000000000..8d889eedf --- /dev/null +++ b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/MariaDbJdbcTemplateStorageAccessorTest.java @@ -0,0 +1,25 @@ +package net.javacrumbs.shedlock.provider.jdbctemplate; + +import net.javacrumbs.shedlock.test.support.jdbc.MariaDbConfig; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; + +class MariaDbJdbcTemplateStorageAccessorTest extends AbstractJdbcTemplateStorageAccessorTest { + + private static final MariaDbConfig dbConfig = new MariaDbConfig(); + + protected MariaDbJdbcTemplateStorageAccessorTest() { + super(dbConfig); + } + + @BeforeAll + public static void startDb() { + dbConfig.startDb(); + } + + @AfterAll + public static void shutdownDb() { + dbConfig.shutdownDb(); + } + +} diff --git a/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/MsSqlJdbcTemplateStorageAccessorTest.java b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/MsSqlJdbcTemplateStorageAccessorTest.java new file mode 100644 index 000000000..f024c97bb --- /dev/null +++ b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/MsSqlJdbcTemplateStorageAccessorTest.java @@ -0,0 +1,25 @@ +package net.javacrumbs.shedlock.provider.jdbctemplate; + +import net.javacrumbs.shedlock.test.support.jdbc.MsSqlServerConfig; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; + +class MsSqlJdbcTemplateStorageAccessorTest extends AbstractJdbcTemplateStorageAccessorTest { + + private static final MsSqlServerConfig dbConfig = new MsSqlServerConfig(); + + protected MsSqlJdbcTemplateStorageAccessorTest() { + super(dbConfig); + } + + @BeforeAll + public static void startDb() { + dbConfig.startDb(); + } + + @AfterAll + public static void shutdownDb() { + dbConfig.shutdownDb(); + } + +} diff --git a/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/MySqlJdbcTemplateStorageAccessorTest.java b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/MySqlJdbcTemplateStorageAccessorTest.java new file mode 100644 index 000000000..ea20c641f --- /dev/null +++ b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/MySqlJdbcTemplateStorageAccessorTest.java @@ -0,0 +1,25 @@ +package net.javacrumbs.shedlock.provider.jdbctemplate; + +import net.javacrumbs.shedlock.test.support.jdbc.MySqlConfig; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; + +class MySqlJdbcTemplateStorageAccessorTest extends AbstractJdbcTemplateStorageAccessorTest { + + private static final MySqlConfig dbConfig = new MySqlConfig(); + + protected MySqlJdbcTemplateStorageAccessorTest() { + super(dbConfig); + } + + @BeforeAll + public static void startDb() { + dbConfig.startDb(); + } + + @AfterAll + public static void shutdownDb() { + dbConfig.shutdownDb(); + } + +} diff --git a/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/OracleJdbcTemplateStorageAccessorTest.java b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/OracleJdbcTemplateStorageAccessorTest.java new file mode 100644 index 000000000..22ec6dfdf --- /dev/null +++ b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/OracleJdbcTemplateStorageAccessorTest.java @@ -0,0 +1,25 @@ +package net.javacrumbs.shedlock.provider.jdbctemplate; + +import net.javacrumbs.shedlock.test.support.jdbc.OracleServerConfig; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; + +class OracleJdbcTemplateStorageAccessorTest extends AbstractJdbcTemplateStorageAccessorTest { + + private static final OracleServerConfig dbConfig = new OracleServerConfig(); + + protected OracleJdbcTemplateStorageAccessorTest() { + super(dbConfig); + } + + @BeforeAll + public static void startDb() { + dbConfig.startDb(); + } + + @AfterAll + public static void shutdownDb() { + dbConfig.shutdownDb(); + } + +} diff --git a/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/PostgresJdbcTemplateStorageAccessorTest.java b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/PostgresJdbcTemplateStorageAccessorTest.java index f970b019e..c384576ad 100644 --- a/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/PostgresJdbcTemplateStorageAccessorTest.java +++ b/providers/jdbc/shedlock-provider-jdbc-template/src/test/java/net/javacrumbs/shedlock/provider/jdbctemplate/PostgresJdbcTemplateStorageAccessorTest.java @@ -1,26 +1,27 @@ package net.javacrumbs.shedlock.provider.jdbctemplate; import net.javacrumbs.shedlock.core.LockConfiguration; -import net.javacrumbs.shedlock.test.support.jdbc.JdbcTestUtils; import net.javacrumbs.shedlock.test.support.jdbc.PostgresConfig; -import net.javacrumbs.shedlock.support.annotation.NonNull; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.sql.Timestamp; import java.time.Duration; -import java.time.Instant; import static java.lang.Thread.sleep; import static org.assertj.core.api.Assertions.assertThat; -class PostgresJdbcTemplateStorageAccessorTest { +class PostgresJdbcTemplateStorageAccessorTest extends AbstractJdbcTemplateStorageAccessorTest { + + private static final String MY_LOCK = "my-lock"; + private static final String OTHER_LOCK = "other-lock"; + private static final PostgresConfig dbConfig = new PostgresConfig(); - public static final String MY_LOCK = "my-lock"; - private final JdbcTestUtils testUtils = new JdbcTestUtils(dbConfig); - private final Instant startTime = Instant.parse("2020-04-11T05:30:00Z"); + + protected PostgresJdbcTemplateStorageAccessorTest() { + super(dbConfig); + } @BeforeAll public static void startDb() { @@ -32,18 +33,22 @@ public static void shutdownDb() { dbConfig.shutdownDb(); } - @AfterEach - public void cleanup() { - testUtils.clean(); + @Test + void shouldUpdateOnInsertAfterValidityOfPreviousEndedWhenNotUsingDbTime() throws InterruptedException { + shouldUpdateOnInsertAfterValidityOfPreviousEnded(false); } @Test - void shouldUpdateOnInsertAfterValidityOfPreviousEnded() throws InterruptedException { - JdbcTemplateStorageAccessor accessor = getAccessor(); + void shouldUpdateOnInsertAfterValidityOfPreviousEndedWhenUsingDbTime() throws InterruptedException { + shouldUpdateOnInsertAfterValidityOfPreviousEnded(true); + } + + private void shouldUpdateOnInsertAfterValidityOfPreviousEnded(boolean usingDbTime) throws InterruptedException { + JdbcTemplateStorageAccessor accessor = getAccessor(usingDbTime); - accessor.insertRecord(new LockConfiguration("other", Duration.ofSeconds(5), Duration.ZERO)); - Timestamp otherLockValidity = testUtils.getLockedUntil("other"); + accessor.insertRecord(new LockConfiguration(OTHER_LOCK, Duration.ofSeconds(5), Duration.ZERO)); + Timestamp otherLockValidity = getTestUtils().getLockedUntil(OTHER_LOCK); assertThat( accessor.insertRecord(new LockConfiguration(MY_LOCK, Duration.ofMillis(10), Duration.ZERO)) @@ -56,34 +61,7 @@ void shouldUpdateOnInsertAfterValidityOfPreviousEnded() throws InterruptedExcept ).isEqualTo(true); // check that the other lock has not been affected by "my-lock" update - assertThat(testUtils.getLockedUntil("other")).isEqualTo(otherLockValidity); - } - - @Test - void shouldNotUpdateOnInsertIfPreviousDidNotEnd() { - JdbcTemplateStorageAccessor accessor = getAccessor(); - - assertThat( - accessor.insertRecord(new LockConfiguration(MY_LOCK, Duration.ofSeconds(10), Duration.ZERO)) - ).isEqualTo(true); - - Timestamp originalLockValidity = testUtils.getLockedUntil(MY_LOCK); - - assertThat( - accessor.insertRecord(new LockConfiguration(MY_LOCK, Duration.ofSeconds(10), Duration.ZERO)) - ).isEqualTo(false); - - assertThat(testUtils.getLockedUntil(MY_LOCK)).isEqualTo(originalLockValidity); - } - - - @NonNull - private JdbcTemplateStorageAccessor getAccessor() { - return new JdbcTemplateStorageAccessor(JdbcTemplateLockProvider - .Configuration.builder() - .withJdbcTemplate(testUtils.getJdbcTemplate()) - .build() - ); + assertThat(getTestUtils().getLockedUntil(OTHER_LOCK)).isEqualTo(otherLockValidity); } } diff --git a/providers/jdbc/shedlock-test-support-jdbc/src/main/java/net/javacrumbs/shedlock/test/support/jdbc/MariaDbConfig.java b/providers/jdbc/shedlock-test-support-jdbc/src/main/java/net/javacrumbs/shedlock/test/support/jdbc/MariaDbConfig.java index 6b692722e..871f74452 100644 --- a/providers/jdbc/shedlock-test-support-jdbc/src/main/java/net/javacrumbs/shedlock/test/support/jdbc/MariaDbConfig.java +++ b/providers/jdbc/shedlock-test-support-jdbc/src/main/java/net/javacrumbs/shedlock/test/support/jdbc/MariaDbConfig.java @@ -22,7 +22,7 @@ import java.util.function.Consumer; -class MariaDbConfig implements DbConfig { +public final class MariaDbConfig implements DbConfig { private static final String TEST_SCHEMA_NAME = "shedlock_test"; private static final Logger logger = LoggerFactory.getLogger(PostgresConfig.class); diff --git a/providers/jdbc/shedlock-test-support-jdbc/src/main/java/net/javacrumbs/shedlock/test/support/jdbc/MySqlConfig.java b/providers/jdbc/shedlock-test-support-jdbc/src/main/java/net/javacrumbs/shedlock/test/support/jdbc/MySqlConfig.java index 739737fcd..78dcf9633 100644 --- a/providers/jdbc/shedlock-test-support-jdbc/src/main/java/net/javacrumbs/shedlock/test/support/jdbc/MySqlConfig.java +++ b/providers/jdbc/shedlock-test-support-jdbc/src/main/java/net/javacrumbs/shedlock/test/support/jdbc/MySqlConfig.java @@ -22,7 +22,7 @@ import java.util.function.Consumer; -class MySqlConfig implements DbConfig { +public final class MySqlConfig implements DbConfig { private static final String TEST_SCHEMA_NAME = "shedlock_test"; private static final Logger logger = LoggerFactory.getLogger(PostgresConfig.class);