Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestHelper.createManualScheduler() does not work for MySQL 8 #640

Open
hipnoizz opened this issue Mar 3, 2025 · 0 comments
Open

TestHelper.createManualScheduler() does not work for MySQL 8 #640

hipnoizz opened this issue Mar 3, 2025 · 0 comments

Comments

@hipnoizz
Copy link

hipnoizz commented Mar 3, 2025

Running a following test

package org.example;

import com.github.kagkarlsson.scheduler.task.helper.RecurringTask;
import com.github.kagkarlsson.scheduler.task.helper.Tasks;
import com.github.kagkarlsson.scheduler.task.schedule.FixedDelay;
import com.github.kagkarlsson.scheduler.testhelper.ManualScheduler;
import com.github.kagkarlsson.scheduler.testhelper.SettableClock;
import com.github.kagkarlsson.scheduler.testhelper.TestHelper;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.time.Duration;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        DataSource dataSource = dataSource();
        RecurringTask<Void> task = Tasks
                .recurring("test-task", FixedDelay.ofSeconds(10))
                .execute((taskInstance, executionContext) -> System.out.println("Hello, world!"));
        SettableClock clock = new SettableClock();

        ManualScheduler scheduler = TestHelper
                .createManualScheduler(dataSource, List.of(task))
                .clock(clock)
                .build();

        scheduler.start();
        scheduler.tick(Duration.ofSeconds(20));
        scheduler.runAnyDueExecutions();
    }

    private static DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        config.setUsername("mysql");
        config.setPassword("mysql");
        config.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        return new HikariDataSource(config);
    }
}

against MySQL 8 results in a failure Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OFFSET 0 ROWS FETCH FIRST 30 ROWS ONLY' at line 1.

The reason for that is that TestHelper will always use DefaultJdbcCustomization:

and DefaultJdbcCustomization tries to use ANSI pagination constructs that aren't supported in MySQL.

It would be better to honour jdbcCustomization set on the builder and fall back to AutodetectJdbcCustomization if not set. It would boil down to something like

      ...
      final JdbcCustomization jdbcCustomization =
          ofNullable(this.jdbcCustomization)
              .orElseGet(
                  () -> new AutodetectJdbcCustomization(dataSource, alwaysPersistTimestampInUTC));
      final JdbcTaskRepository schedulerTaskRepository =
          new JdbcTaskRepository(
              dataSource,
              true,
              jdbcCustomization,
              tableName,
              taskResolver,
              new SchedulerName.Fixed("manual"),
              serializer,
              enablePriority,
              clock);
      final JdbcTaskRepository clientTaskRepository =
          new JdbcTaskRepository(
              dataSource,
              commitWhenAutocommitDisabled,
              jdbcCustomization,
              tableName,
              taskResolver,
              new SchedulerName.Fixed("manual"),
              serializer,
              enablePriority,
              clock);
      ...

in ManualSchedulerBuilder.build().

I run all tests with this small change and they are all green. @kagkarlsson I can contribute a PR if you are interested. It would be even better to have some (integration) test(s) but that would I think require adding support for running an embedded MySQL instance (similar to what I can see for PostgreSQL).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant