From 87be1668b17f68e36fb869b194519be4e99268bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Mon, 4 Sep 2023 10:23:51 +0200 Subject: [PATCH] Add scheduling sample --- framework/scheduled/README.adoc | 1 + framework/scheduled/build.gradle | 17 +++++++++ .../ScheduledApplicationAotTests.java | 37 +++++++++++++++++++ .../main/java/com/example/scheduled/Bean.java | 24 ++++++++++++ .../scheduled/ScheduledApplication.java | 16 ++++++++ .../src/main/resources/application.properties | 0 6 files changed, 95 insertions(+) create mode 100644 framework/scheduled/README.adoc create mode 100644 framework/scheduled/build.gradle create mode 100644 framework/scheduled/src/appTest/java/com/example/scheduled/ScheduledApplicationAotTests.java create mode 100644 framework/scheduled/src/main/java/com/example/scheduled/Bean.java create mode 100644 framework/scheduled/src/main/java/com/example/scheduled/ScheduledApplication.java create mode 100644 framework/scheduled/src/main/resources/application.properties diff --git a/framework/scheduled/README.adoc b/framework/scheduled/README.adoc new file mode 100644 index 0000000..4318870 --- /dev/null +++ b/framework/scheduled/README.adoc @@ -0,0 +1 @@ +Tests if `@Scheduled` is working diff --git a/framework/scheduled/build.gradle b/framework/scheduled/build.gradle new file mode 100644 index 0000000..047655a --- /dev/null +++ b/framework/scheduled/build.gradle @@ -0,0 +1,17 @@ +plugins { + id "java" + id "org.springframework.boot" + id "org.springframework.cr.smoke-test" +} + +dependencies { + implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)) + implementation("org.springframework.boot:spring-boot-starter") + implementation("org.crac:crac:$cracVersion") + implementation(project(":cr-listener")) + + testImplementation("org.springframework.boot:spring-boot-starter-test") + + appTestImplementation(project(":cr-smoke-test-support")) + appTestImplementation("org.awaitility:awaitility:4.2.0") +} diff --git a/framework/scheduled/src/appTest/java/com/example/scheduled/ScheduledApplicationAotTests.java b/framework/scheduled/src/appTest/java/com/example/scheduled/ScheduledApplicationAotTests.java new file mode 100644 index 0000000..fc1d537 --- /dev/null +++ b/framework/scheduled/src/appTest/java/com/example/scheduled/ScheduledApplicationAotTests.java @@ -0,0 +1,37 @@ +package com.example.scheduled; + +import java.time.Duration; + +import org.awaitility.Awaitility; +import org.junit.jupiter.api.Test; + +import org.springframework.cr.smoketest.support.assertj.AssertableOutput; +import org.springframework.cr.smoketest.support.junit.ApplicationTest; + +import static org.assertj.core.api.Assertions.assertThat; + +@ApplicationTest +class ScheduledApplicationAotTests { + + @Test + void fixedRateShouldBeCalled(AssertableOutput output) { + Awaitility.await() + .atMost(Duration.ofSeconds(10)) + .untilAsserted(() -> assertThat(output).hasLineContaining("fixedRate()")); + } + + @Test + void fixedDelayShouldBeCalled(AssertableOutput output) { + Awaitility.await() + .atMost(Duration.ofSeconds(10)) + .untilAsserted(() -> assertThat(output).hasLineContaining("fixedDelay()")); + } + + @Test + void cronShouldBeCalled(AssertableOutput output) { + Awaitility.await() + .atMost(Duration.ofSeconds(10)) + .untilAsserted(() -> assertThat(output).hasLineContaining("cron()")); + } + +} diff --git a/framework/scheduled/src/main/java/com/example/scheduled/Bean.java b/framework/scheduled/src/main/java/com/example/scheduled/Bean.java new file mode 100644 index 0000000..31059c7 --- /dev/null +++ b/framework/scheduled/src/main/java/com/example/scheduled/Bean.java @@ -0,0 +1,24 @@ +package com.example.scheduled; + +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component +class Bean { + + @Scheduled(fixedRate = 1000) + void fixedRate() { + System.out.println("fixedRate()"); + } + + @Scheduled(fixedDelay = 1000) + void fixedDelay() { + System.out.println("fixedDelay()"); + } + + @Scheduled(cron = "*/2 * * * * *") + void cron() { + System.out.println("cron()"); + } + +} diff --git a/framework/scheduled/src/main/java/com/example/scheduled/ScheduledApplication.java b/framework/scheduled/src/main/java/com/example/scheduled/ScheduledApplication.java new file mode 100644 index 0000000..476b937 --- /dev/null +++ b/framework/scheduled/src/main/java/com/example/scheduled/ScheduledApplication.java @@ -0,0 +1,16 @@ +package com.example.scheduled; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; + +@SpringBootApplication +@EnableScheduling +public class ScheduledApplication { + + public static void main(String[] args) throws InterruptedException { + SpringApplication.run(ScheduledApplication.class, args); + Thread.currentThread().join(); + } + +} diff --git a/framework/scheduled/src/main/resources/application.properties b/framework/scheduled/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29