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

added parametrized schedule, with test application.properties for #30 #35

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/com/example/schedulingtasks/ScheduledTasks.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

Expand All @@ -35,4 +36,15 @@ public class ScheduledTasks {
public void reportCurrentTime() {
log.info("The time is now {}", dateFormat.format(new Date()));
}

@Value("${my.app.time.report.skip:false}")
private boolean skipReportTime = false;

@Scheduled(fixedRateString = "${my.app.time.report.cycle:8000}", initialDelayString = "${my.app.time.report.delay:0}")
public void reportCurrentTimeParametered() {
if (skipReportTime) {
return;
}
log.info("[parametered] The time is now {}", dateFormat.format(new Date()));
}
}
18 changes: 13 additions & 5 deletions src/test/java/com/example/schedulingtasks/ScheduledTasksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@

package com.example.schedulingtasks;

import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.verify;

import org.awaitility.Durations;
import org.junit.jupiter.api.Test;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;

import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.verify;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("config")
public class ScheduledTasksTest {

@SpyBean
Expand All @@ -38,4 +39,11 @@ public void reportCurrentTime() {
verify(tasks, atLeast(2)).reportCurrentTime();
});
}

@Test
public void reportCurrentTimeParametered() {
await().atMost(Durations.TEN_SECONDS).untilAsserted(() -> {
verify(tasks, atLeast(2)).reportCurrentTimeParametered();
});
}
}
7 changes: 7 additions & 0 deletions src/test/resources/application-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
my:
app:
time:
report:
cycle: 3000
delay: 1000
# skip: true