|
| 1 | +package com.stubbornjava.common.undertow.handlers.diagnostic; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertTrue; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | +import java.util.concurrent.Callable; |
| 7 | +import java.util.concurrent.ExecutorService; |
| 8 | +import java.util.concurrent.Executors; |
| 9 | +import java.util.concurrent.Future; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | +import java.util.stream.Collectors; |
| 12 | +import java.util.stream.IntStream; |
| 13 | + |
| 14 | +import org.jooq.lambda.Seq; |
| 15 | +import org.jooq.lambda.Unchecked; |
| 16 | +import org.junit.Assert; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +import com.google.common.base.Stopwatch; |
| 20 | +import com.google.common.util.concurrent.MoreExecutors; |
| 21 | +import com.stubbornjava.common.Http; |
| 22 | +import com.stubbornjava.common.HttpClient; |
| 23 | +import com.stubbornjava.common.undertow.Exchange; |
| 24 | +import com.stubbornjava.common.undertow.UndertowUtil; |
| 25 | +import com.stubbornjava.common.undertow.handlers.CustomHandlers; |
| 26 | +import com.stubbornjava.undertow.handlers.MiddlewareBuilder; |
| 27 | + |
| 28 | +import io.undertow.Undertow; |
| 29 | +import io.undertow.server.HttpHandler; |
| 30 | +import io.undertow.server.handlers.BlockingHandler; |
| 31 | +import okhttp3.OkHttpClient; |
| 32 | +import okhttp3.Response; |
| 33 | + |
| 34 | +public class DelayedExecutionHandlerTest { |
| 35 | + |
| 36 | + // Delay for 500ms then return "ok" |
| 37 | + private static final DelayedExecutionHandler delayedHandler = |
| 38 | + DiagnosticHandlers.fixedDelay((exchange) -> { |
| 39 | + Exchange.body().sendText(exchange, "ok"); |
| 40 | + }, |
| 41 | + 500, TimeUnit.MILLISECONDS); |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testOnXIoThread() throws InterruptedException { |
| 45 | + int numThreads = 10; |
| 46 | + run(delayedHandler, numThreads); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testOnWorkerThread() throws InterruptedException { |
| 51 | + int numThreads = 10; |
| 52 | + run(new BlockingHandler(delayedHandler), numThreads); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Spin up a new server with a single IO thread and worker thread. |
| 57 | + * Run N GET requests against it concurrently and make sure they |
| 58 | + * do not take N * 500ms total. This is not the best test but it |
| 59 | + * should show that we are delaying N requests at once using a single |
| 60 | + * thread. |
| 61 | + * |
| 62 | + * @param handler |
| 63 | + * @param numThreads |
| 64 | + * @throws InterruptedException |
| 65 | + */ |
| 66 | + private void run(HttpHandler handler, int numThreads) throws InterruptedException { |
| 67 | + HttpHandler route = MiddlewareBuilder.begin(CustomHandlers::accessLog) |
| 68 | + .complete(handler); |
| 69 | + Undertow.Builder builder = Undertow.builder() |
| 70 | + .setWorkerThreads(1) |
| 71 | + .setIoThreads(1); |
| 72 | + UndertowUtil.useLocalServer(builder, route, host -> { |
| 73 | + ExecutorService exec = Executors.newFixedThreadPool(numThreads); |
| 74 | + OkHttpClient client = new OkHttpClient().newBuilder() |
| 75 | + .addInterceptor(HttpClient.getLoggingInterceptor()) |
| 76 | + .build(); |
| 77 | + |
| 78 | + // Using time in tests isn't the best approach but this one seems |
| 79 | + // A little difficult to test another way. |
| 80 | + Stopwatch sw = Stopwatch.createStarted(); |
| 81 | + List<Callable<Response>> callables = IntStream.range(0, numThreads) |
| 82 | + .mapToObj(i -> (Callable<Response>) () -> Http.get(client, host)) |
| 83 | + .collect(Collectors.toList()); |
| 84 | + sw.stop(); |
| 85 | + Seq.seq(Unchecked.supplier(() -> exec.invokeAll(callables)).get()) |
| 86 | + .map(Unchecked.function(Future::get)) |
| 87 | + .forEach(DelayedExecutionHandlerTest::assertSuccess); |
| 88 | + assertTrue("Responses took too long", sw.elapsed().toMillis() < 1_000); |
| 89 | + MoreExecutors.shutdownAndAwaitTermination(exec, 10, TimeUnit.SECONDS); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + private static void assertSuccess(Response response) { |
| 94 | + Assert.assertTrue("Response should be a 200", response.isSuccessful()); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments