From daebf6a97b8f4c0511df6f5067d10284fc8fe45e Mon Sep 17 00:00:00 2001 From: Richard Startin Date: Wed, 16 Sep 2020 14:03:14 +0100 Subject: [PATCH] add a smoke test for spring webmvc -> async -> grpc --- .../SpringbootGrpcApplication.java | 18 ------- .../smoketest/springboot/AsyncTask.java | 20 ++++++++ .../springboot/SpringbootGrpcApplication.java | 48 +++++++++++++++++++ .../springboot/controller/WebController.java | 26 +++++----- .../SpringBootGrpcAsyncAnnotationTest.groovy | 14 ++++++ 5 files changed, 95 insertions(+), 31 deletions(-) delete mode 100644 dd-smoke-tests/springboot-grpc/src/main_java8/java/datadog.smoketest.springboot/SpringbootGrpcApplication.java create mode 100644 dd-smoke-tests/springboot-grpc/src/main_java8/java/datadog/smoketest/springboot/AsyncTask.java create mode 100644 dd-smoke-tests/springboot-grpc/src/main_java8/java/datadog/smoketest/springboot/SpringbootGrpcApplication.java create mode 100644 dd-smoke-tests/springboot-grpc/src/test/groovy/datadog/smoketest/SpringBootGrpcAsyncAnnotationTest.groovy diff --git a/dd-smoke-tests/springboot-grpc/src/main_java8/java/datadog.smoketest.springboot/SpringbootGrpcApplication.java b/dd-smoke-tests/springboot-grpc/src/main_java8/java/datadog.smoketest.springboot/SpringbootGrpcApplication.java deleted file mode 100644 index 3674d219772..00000000000 --- a/dd-smoke-tests/springboot-grpc/src/main_java8/java/datadog.smoketest.springboot/SpringbootGrpcApplication.java +++ /dev/null @@ -1,18 +0,0 @@ -package datadog.smoketest.springboot; - -import java.lang.management.ManagementFactory; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ConfigurableApplicationContext; - -@SpringBootApplication -public class SpringbootGrpcApplication { - - public static void main(final String[] args) { - ConfigurableApplicationContext app = - SpringApplication.run(SpringbootGrpcApplication.class, args); - Integer port = app.getBean("local.server.port", Integer.class); - System.out.println( - "Bound to " + port + " in " + ManagementFactory.getRuntimeMXBean().getUptime() + "ms"); - } -} diff --git a/dd-smoke-tests/springboot-grpc/src/main_java8/java/datadog/smoketest/springboot/AsyncTask.java b/dd-smoke-tests/springboot-grpc/src/main_java8/java/datadog/smoketest/springboot/AsyncTask.java new file mode 100644 index 00000000000..27c05ec327d --- /dev/null +++ b/dd-smoke-tests/springboot-grpc/src/main_java8/java/datadog/smoketest/springboot/AsyncTask.java @@ -0,0 +1,20 @@ +package datadog.smoketest.springboot; + +import datadog.smoketest.springboot.grpc.AsynchronousGreeter; +import org.springframework.scheduling.annotation.Async; + +import java.util.concurrent.CompletableFuture; + +public class AsyncTask { + + private final AsynchronousGreeter greeter; + + public AsyncTask(AsynchronousGreeter greeter) { + this.greeter = greeter; + } + + @Async + public CompletableFuture greet() { + return CompletableFuture.completedFuture(greeter.greet()); + } +} diff --git a/dd-smoke-tests/springboot-grpc/src/main_java8/java/datadog/smoketest/springboot/SpringbootGrpcApplication.java b/dd-smoke-tests/springboot-grpc/src/main_java8/java/datadog/smoketest/springboot/SpringbootGrpcApplication.java new file mode 100644 index 00000000000..b86268e37f6 --- /dev/null +++ b/dd-smoke-tests/springboot-grpc/src/main_java8/java/datadog/smoketest/springboot/SpringbootGrpcApplication.java @@ -0,0 +1,48 @@ +package datadog.smoketest.springboot; + +import java.io.IOException; +import java.lang.management.ManagementFactory; + +import datadog.smoketest.springboot.grpc.AsynchronousGreeter; +import datadog.smoketest.springboot.grpc.LocalInterface; +import datadog.smoketest.springboot.grpc.SynchronousGreeter; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; + +@SpringBootApplication +@EnableAsync +@EnableScheduling +public class SpringbootGrpcApplication { + + @Bean + AsyncTask asyncTask(AsynchronousGreeter greeter) { + return new AsyncTask(greeter); + } + + @Bean + AsynchronousGreeter asynchronousGreeter(LocalInterface localInterface) { + return new AsynchronousGreeter(localInterface.getPort()); + } + + @Bean + SynchronousGreeter synchronousGreeter(LocalInterface localInterface) { + return new SynchronousGreeter(localInterface.getPort()); + } + + @Bean + LocalInterface localInterface() throws IOException { + return new LocalInterface(); + } + + public static void main(final String[] args) { + ConfigurableApplicationContext app = + SpringApplication.run(SpringbootGrpcApplication.class, args); + Integer port = app.getBean("local.server.port", Integer.class); + System.out.println( + "Bound to " + port + " in " + ManagementFactory.getRuntimeMXBean().getUptime() + "ms"); + } +} diff --git a/dd-smoke-tests/springboot-grpc/src/main_java8/java/datadog/smoketest/springboot/controller/WebController.java b/dd-smoke-tests/springboot-grpc/src/main_java8/java/datadog/smoketest/springboot/controller/WebController.java index 5b9c7f70a8d..74bade965ab 100644 --- a/dd-smoke-tests/springboot-grpc/src/main_java8/java/datadog/smoketest/springboot/controller/WebController.java +++ b/dd-smoke-tests/springboot-grpc/src/main_java8/java/datadog/smoketest/springboot/controller/WebController.java @@ -1,9 +1,9 @@ package datadog.smoketest.springboot.controller; +import datadog.smoketest.springboot.AsyncTask; import datadog.smoketest.springboot.grpc.AsynchronousGreeter; -import datadog.smoketest.springboot.grpc.LocalInterface; import datadog.smoketest.springboot.grpc.SynchronousGreeter; -import java.io.IOException; + import java.util.concurrent.Callable; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; @@ -16,20 +16,21 @@ @Slf4j @RestController -public class WebController implements AutoCloseable { +public class WebController { private final ExecutorService pool = Executors.newFixedThreadPool(5); private final AsynchronousGreeter asyncGreeter; private final SynchronousGreeter greeter; - private final LocalInterface localInterface; + private final AsyncTask asyncTask; - public WebController() throws IOException { - this.localInterface = new LocalInterface(); - this.asyncGreeter = new AsynchronousGreeter(localInterface.getPort()); - this.greeter = new SynchronousGreeter(localInterface.getPort()); + public WebController(AsynchronousGreeter asyncGreeter, SynchronousGreeter greeter, AsyncTask asyncTask) { + this.asyncGreeter = asyncGreeter; + this.greeter = greeter; + this.asyncTask = asyncTask; } + @RequestMapping("/greeting") public String greeting() { return greeter.greet(); @@ -74,10 +75,9 @@ public String call() { return response; } - @Override - public void close() { - localInterface.close(); - greeter.close(); - asyncGreeter.close(); + @RequestMapping("async_annotation_greeting") + public String asyncAnnotationGreeting() { + return asyncTask.greet().join(); } + } diff --git a/dd-smoke-tests/springboot-grpc/src/test/groovy/datadog/smoketest/SpringBootGrpcAsyncAnnotationTest.groovy b/dd-smoke-tests/springboot-grpc/src/test/groovy/datadog/smoketest/SpringBootGrpcAsyncAnnotationTest.groovy new file mode 100644 index 00000000000..5d5a285195d --- /dev/null +++ b/dd-smoke-tests/springboot-grpc/src/test/groovy/datadog/smoketest/SpringBootGrpcAsyncAnnotationTest.groovy @@ -0,0 +1,14 @@ +package datadog.smoketest + +class SpringBootGrpcAsyncAnnotationTest extends SpringBootWithGRPCTest { + @Override + Set expectedTraces() { + return ["[grpc.server[grpc.message]]", + "[servlet.request[spring.handler[AsyncTask.greet[grpc.client[grpc.message]]]]]"].toSet() + } + + @Override + String route() { + return "async_annotation_greeting" + } +}