Skip to content

Commit

Permalink
add a smoke test for spring webmvc -> async -> grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
richardstartin committed Sep 16, 2020
1 parent b5f90d3 commit daebf6a
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 31 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<String> greet() {
return CompletableFuture.completedFuture(greeter.greet());
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package datadog.smoketest

class SpringBootGrpcAsyncAnnotationTest extends SpringBootWithGRPCTest {
@Override
Set<String> expectedTraces() {
return ["[grpc.server[grpc.message]]",
"[servlet.request[spring.handler[AsyncTask.greet[grpc.client[grpc.message]]]]]"].toSet()
}

@Override
String route() {
return "async_annotation_greeting"
}
}

0 comments on commit daebf6a

Please sign in to comment.