Skip to content

Commit

Permalink
Add explicit test for store-in-utc JdbcCustomization for Oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
kagkarlsson committed Mar 11, 2024
1 parent 71941db commit f32b1d7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,16 @@ public AutodetectJdbcCustomization(DataSource dataSource, boolean persistTimesta
detectedCustomization = new MySQLJdbcCustomization(true);
}
} else {
LOG.info("No database-specific jdbc-overrides applied. Assuming time-related columns "
+ "to be of type compatibe with 'TIMESTAMP WITH TIME ZONE'. If not, consider overriding "
+ "to always UTC via '.alwaysPersistTimestampInUTC()'.");
if (persistTimestampInUTC) {
LOG.info(
"No database-specific jdbc-overrides applied. Behavior overridden to always store "
+ "timestamps in zone UTC");
} else {
LOG.warn(
"No database-specific jdbc-overrides applied. Assuming time-related columns "
+ "to be of type compatibe with 'TIMESTAMP WITH TIME ZONE', i.e. zone is persisted."
+ " If not, consider overriding to always UTC via '.alwaysPersistTimestampInUTC()'.");
}
}

} catch (SQLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import com.google.common.collect.Lists;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
Expand All @@ -51,11 +53,14 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("ConstantConditions")
public abstract class CompatibilityTest {

private static final int POLLING_LIMIT = 10_000;
private Logger LOG = LoggerFactory.getLogger(getClass());
private final boolean supportsSelectForUpdate;
private final boolean shouldHavePersistentTimezone;

Expand Down Expand Up @@ -330,14 +335,54 @@ public void test_jdbc_repository_compatibility_set_data() {
assertNull(round3.taskInstance.getData());
}

/**
* Very uncertain on how to verify that the timestamp is stored in UTC. The asserts in this test
* will likely always work, but at least the code for storing in UTC is exercised.
*/
@Test
public void test_jdbc_customization_supports_timestamps_in_utc() {
Optional<JdbcCustomization> jdbcCustomization = getJdbcCustomizationForUTCTimestampTest();

if (jdbcCustomization.isEmpty()) {
LOG.info(
"Skipping test_jdbc_customization_supports_timestamps_in_utc since to JdbcCustomization is spesified.");
return;
}
LOG.info(
"Running test_jdbc_customization_supports_timestamps_in_utc for: "
+ jdbcCustomization.get().getName());

TaskResolver defaultTaskResolver = new TaskResolver(StatsRegistry.NOOP, new ArrayList<>());
defaultTaskResolver.addTask(oneTime);

JdbcTaskRepository taskRepo =
createRepositoryForJdbcCustomization(defaultTaskResolver, jdbcCustomization.get());

ZonedDateTime zonedDateTime =
ZonedDateTime.of(2020, 1, 1, 12, 0, 0, 0, ZoneId.of("Europe/Oslo"));

TaskInstance<String> instance1 = oneTime.instance("future1");
taskRepo.createIfNotExists(SchedulableInstance.of(instance1, zonedDateTime.toInstant()));

assertThat(taskRepo.getExecution(instance1).get().executionTime, is(zonedDateTime.toInstant()));
}

private JdbcTaskRepository createRepositoryForForZone(
TaskResolver defaultTaskResolver, String zoneId) {

ZoneSpecificJdbcCustomization jdbcCustomization =
new ZoneSpecificJdbcCustomization(
getJdbcCustomization().orElse(new AutodetectJdbcCustomization(getDataSource())),
GregorianCalendar.getInstance(TimeZone.getTimeZone(zoneId)));
return createRepositoryForJdbcCustomization(defaultTaskResolver, jdbcCustomization);
}

private JdbcTaskRepository createRepositoryForJdbcCustomization(
TaskResolver defaultTaskResolver, JdbcCustomization jdbcCustomization) {
return new JdbcTaskRepository(
getDataSource(),
commitWhenAutocommitDisabled(),
new ZoneSpecificJdbcCustomization(
getJdbcCustomization().orElse(new AutodetectJdbcCustomization(getDataSource())),
GregorianCalendar.getInstance(TimeZone.getTimeZone(zoneId))),
jdbcCustomization,
DEFAULT_TABLE_NAME,
defaultTaskResolver,
new SchedulerName.Fixed("scheduler1"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.github.kagkarlsson.scheduler.compatibility;

import com.github.kagkarlsson.scheduler.DbUtils;
import com.github.kagkarlsson.scheduler.jdbc.JdbcCustomization;
import com.github.kagkarlsson.scheduler.jdbc.OracleJdbcCustomization;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.util.DriverDataSource;
import java.time.Duration;
import java.util.Optional;
import java.util.Properties;
import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -58,4 +61,9 @@ public DataSource getDataSource() {
public boolean commitWhenAutocommitDisabled() {
return false;
}

@Override
public Optional<JdbcCustomization> getJdbcCustomizationForUTCTimestampTest() {
return Optional.of(new OracleJdbcCustomization(true));
}
}

0 comments on commit f32b1d7

Please sign in to comment.