Add scheduling sample

This commit is contained in:
Sébastien Deleuze
2023-09-04 10:23:51 +02:00
parent 7d7cddff37
commit 87be1668b1
6 changed files with 95 additions and 0 deletions

View File

@@ -0,0 +1 @@
Tests if `@Scheduled` is working

View File

@@ -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")
}

View File

@@ -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()"));
}
}

View File

@@ -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()");
}
}

View File

@@ -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();
}
}