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

assertLocked fails with IllegalStateException: The task is not locked. #2283

Closed
lackovic opened this issue Nov 29, 2024 · 2 comments
Closed

Comments

@lackovic
Copy link

lackovic commented Nov 29, 2024

The following code:

@Scheduled(...)
@SchedulerLock(name = "scheduledTaskName")
public void scheduledTask() {
   LockAssert.assertLocked();
   // do something
}

throws the exception:

java.lang.IllegalStateException: The task is not locked.
        at net.javacrumbs.shedlock.core.LockAssert.assertLocked(LockAssert.java:63) ~[shedlock-core-5.16.0.jar:na]

using Shedlock 5.16.0 with Spring Boot 3.4.0.

Either downgrading Spring Boot to 3.3.5 or upgrading Shedlock to 6.0.2 solves the issue.

How can I write an integration test to verify the IllegalStateException is not thrown? I tried with the following:

@SpringBootTest
@RunWith(SpringRunner.class)
@EnableSchedulerLock(defaultLockAtMostFor = "PT30S")
public class MyJobIntegrationTest {

  @Autowired private MyJob myJob;

  @Test
  public void testExecute() {
    myJob.scheduledTask();
  }
}

but this always fail with the same error, regardless of the versions of Shedlock or Spring Boot used:

java.lang.IllegalStateException: The task is not locked.
        at net.javacrumbs.shedlock.core.LockAssert.assertLocked(LockAssert.java:63) ~[shedlock-core-5.16.0.jar:na]

This problem was already addressed in #221 with version 4.7.0 but looks like the problem still happens with version 6.0.2.

@lackovic lackovic changed the title @Scheduled(...) @SchedulerLock(name = "scheduledTaskName") public void scheduledTask() { LockAssert.assertLocked(); // do something } assertLocked fails with IllegalStateException: The task is not locked. Nov 29, 2024
@lackovic
Copy link
Author

lackovic commented Nov 29, 2024

I did some experiments with my scheduler configuration:

@Configuration
@Aspect
@EnableScheduling
@EnableSchedulerLock(interceptMode = PROXY_SCHEDULER, defaultLockAtMostFor = "2h")
class SchedulerConfig {

  @Bean
  LockProvider lockProvider(DataSource dataSource) {
    return new JdbcTemplateLockProvider(dataSource, "scheduler_lock");
  }

and I found out that changing the interceptMode to PROXY_METHOD makes both the application and the tests work without throwing the IllegalStateException, regardless of the versions of Shedlock or Spring Boot used.

I can't explain though why, using PROXY_SCHEDULER, the combination of Shedlock to 6.0.2 and Spring Boot to 3.4.0 solves the issue for the running application but not for the tests.

@lukas-krecan
Copy link
Owner

Looks like this #2255, I will check about the tests

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

2 participants