From e13d64e71f6166a9bde9f89f016c3ac348cc403c Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Tue, 9 Aug 2022 14:51:38 +0200 Subject: [PATCH] Add cache-redis smoke test See gh-40 --- cache-redis/README.adoc | 1 + cache-redis/build.gradle | 18 +++++++++++++++ cache-redis/docker-compose.yml | 6 +++++ .../redis/CacheRedisApplicationAotTests.java | 23 +++++++++++++++++++ .../java/com/example/cache/redis/CLR.java | 21 +++++++++++++++++ .../cache/redis/CacheConfiguration.java | 10 ++++++++ .../cache/redis/CacheRedisApplication.java | 17 ++++++++++++++ .../com/example/cache/redis/TestService.java | 17 ++++++++++++++ .../src/main/resources/application.properties | 3 +++ .../redis/CacheRedisApplicationTests.java | 14 +++++++++++ ci/smoke-tests.yml | 1 + settings.gradle | 1 + 12 files changed, 132 insertions(+) create mode 100644 cache-redis/README.adoc create mode 100644 cache-redis/build.gradle create mode 100644 cache-redis/docker-compose.yml create mode 100644 cache-redis/src/aotTest/java/com/example/cache/redis/CacheRedisApplicationAotTests.java create mode 100644 cache-redis/src/main/java/com/example/cache/redis/CLR.java create mode 100644 cache-redis/src/main/java/com/example/cache/redis/CacheConfiguration.java create mode 100644 cache-redis/src/main/java/com/example/cache/redis/CacheRedisApplication.java create mode 100644 cache-redis/src/main/java/com/example/cache/redis/TestService.java create mode 100644 cache-redis/src/main/resources/application.properties create mode 100644 cache-redis/src/test/java/com/example/cache/redis/CacheRedisApplicationTests.java diff --git a/cache-redis/README.adoc b/cache-redis/README.adoc new file mode 100644 index 00000000..da919d67 --- /dev/null +++ b/cache-redis/README.adoc @@ -0,0 +1 @@ +Tests if caching with Redis works diff --git a/cache-redis/build.gradle b/cache-redis/build.gradle new file mode 100644 index 00000000..ae96ef3d --- /dev/null +++ b/cache-redis/build.gradle @@ -0,0 +1,18 @@ +plugins { + id 'java' + id 'org.springframework.boot' + id 'org.springframework.aot.smoke-test' + id 'org.graalvm.buildtools.native' +} + +dependencies { + implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)) + implementation("org.springframework.boot:spring-boot-starter-cache") + implementation("org.springframework.boot:spring-boot-starter-data-redis") + implementation(project(":aot-smoke-test-third-party-hints")) + + testImplementation("org.springframework.boot:spring-boot-starter-test") + + aotTestImplementation(project(":aot-smoke-test-support")) + aotTestImplementation("org.awaitility:awaitility:4.2.0") +} diff --git a/cache-redis/docker-compose.yml b/cache-redis/docker-compose.yml new file mode 100644 index 00000000..83048be3 --- /dev/null +++ b/cache-redis/docker-compose.yml @@ -0,0 +1,6 @@ +version: '3.1' +services: + redis: + image: 'redis:7' + ports: + - 6379 diff --git a/cache-redis/src/aotTest/java/com/example/cache/redis/CacheRedisApplicationAotTests.java b/cache-redis/src/aotTest/java/com/example/cache/redis/CacheRedisApplicationAotTests.java new file mode 100644 index 00000000..4df8aa09 --- /dev/null +++ b/cache-redis/src/aotTest/java/com/example/cache/redis/CacheRedisApplicationAotTests.java @@ -0,0 +1,23 @@ +package com.example.cache.redis; + +import java.time.Duration; + +import org.awaitility.Awaitility; +import org.junit.jupiter.api.Test; + +import org.springframework.aot.smoketest.support.assertj.AssertableOutput; +import org.springframework.aot.smoketest.support.junit.AotSmokeTest; + +import static org.assertj.core.api.Assertions.assertThat; + +@AotSmokeTest +class CacheRedisApplicationAotTests { + + @Test + void methodIsCached(AssertableOutput output) { + Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> { + assertThat(output).hasSingleLineContaining("invoke: 1").hasNoLinesContaining("invoke: 2"); + }); + } + +} diff --git a/cache-redis/src/main/java/com/example/cache/redis/CLR.java b/cache-redis/src/main/java/com/example/cache/redis/CLR.java new file mode 100644 index 00000000..a54006c9 --- /dev/null +++ b/cache-redis/src/main/java/com/example/cache/redis/CLR.java @@ -0,0 +1,21 @@ +package com.example.cache.redis; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +@Component +class CLR implements CommandLineRunner { + + private final TestService testService; + + public CLR(TestService testService) { + this.testService = testService; + } + + @Override + public void run(String... args) { + this.testService.invoke(); + this.testService.invoke(); + } + +} diff --git a/cache-redis/src/main/java/com/example/cache/redis/CacheConfiguration.java b/cache-redis/src/main/java/com/example/cache/redis/CacheConfiguration.java new file mode 100644 index 00000000..94df8aa0 --- /dev/null +++ b/cache-redis/src/main/java/com/example/cache/redis/CacheConfiguration.java @@ -0,0 +1,10 @@ +package com.example.cache.redis; + +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Configuration; + +@Configuration(proxyBeanMethods = false) +@EnableCaching +class CacheConfiguration { + +} diff --git a/cache-redis/src/main/java/com/example/cache/redis/CacheRedisApplication.java b/cache-redis/src/main/java/com/example/cache/redis/CacheRedisApplication.java new file mode 100644 index 00000000..2c09d604 --- /dev/null +++ b/cache-redis/src/main/java/com/example/cache/redis/CacheRedisApplication.java @@ -0,0 +1,17 @@ +package com.example.cache.redis; + +import org.springframework.aot.smoketest.thirdpartyhints.NettyRuntimeHints; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ImportRuntimeHints; + +@SpringBootApplication +@ImportRuntimeHints(NettyRuntimeHints.class) +public class CacheRedisApplication { + + public static void main(String[] args) throws InterruptedException { + SpringApplication.run(CacheRedisApplication.class, args); + Thread.currentThread().join(); // To be able to measure memory consumption + } + +} diff --git a/cache-redis/src/main/java/com/example/cache/redis/TestService.java b/cache-redis/src/main/java/com/example/cache/redis/TestService.java new file mode 100644 index 00000000..c46a9e02 --- /dev/null +++ b/cache-redis/src/main/java/com/example/cache/redis/TestService.java @@ -0,0 +1,17 @@ +package com.example.cache.redis; + +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +@Service +class TestService { + + private int counter = 1; + + @Cacheable(cacheNames = "invoke") + public void invoke() { + System.out.printf("invoke: %d%n", this.counter); + this.counter++; + } + +} diff --git a/cache-redis/src/main/resources/application.properties b/cache-redis/src/main/resources/application.properties new file mode 100644 index 00000000..60b9a426 --- /dev/null +++ b/cache-redis/src/main/resources/application.properties @@ -0,0 +1,3 @@ +spring.cache.type=redis +spring.data.redis.host=${REDIS_HOST:localhost} +spring.data.redis.port=${REDIS_PORT_6379:6379} diff --git a/cache-redis/src/test/java/com/example/cache/redis/CacheRedisApplicationTests.java b/cache-redis/src/test/java/com/example/cache/redis/CacheRedisApplicationTests.java new file mode 100644 index 00000000..0946b4b3 --- /dev/null +++ b/cache-redis/src/test/java/com/example/cache/redis/CacheRedisApplicationTests.java @@ -0,0 +1,14 @@ +package com.example.cache.redis; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class CacheRedisApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/ci/smoke-tests.yml b/ci/smoke-tests.yml index 79ea79f9..ea7eb882 100644 --- a/ci/smoke-tests.yml +++ b/ci/smoke-tests.yml @@ -9,6 +9,7 @@ smoke_tests: - cache-cache2k - cache-caffeine - cache-hazelcast + - cache-redis - cache-simple - cloud-function-web - cloud-function-webflux diff --git a/settings.gradle b/settings.gradle index c4db65d3..33066f3e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -37,6 +37,7 @@ include "batch" include "cache-cache2k" include "cache-caffeine" include "cache-hazelcast" +include "cache-redis" include "cache-simple" include "cloud-function-web" include "cloud-function-webflux"