diff options
Diffstat (limited to 'vespaclient-container-plugin/src/test/java/com/yahoo/document/restapi/resource/RestApiMaxThreadTest.java')
-rw-r--r-- | vespaclient-container-plugin/src/test/java/com/yahoo/document/restapi/resource/RestApiMaxThreadTest.java | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/vespaclient-container-plugin/src/test/java/com/yahoo/document/restapi/resource/RestApiMaxThreadTest.java b/vespaclient-container-plugin/src/test/java/com/yahoo/document/restapi/resource/RestApiMaxThreadTest.java new file mode 100644 index 00000000000..17a36c142ea --- /dev/null +++ b/vespaclient-container-plugin/src/test/java/com/yahoo/document/restapi/resource/RestApiMaxThreadTest.java @@ -0,0 +1,54 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.document.restapi.resource; + +import com.yahoo.container.jdisc.HttpRequest; +import com.yahoo.container.jdisc.HttpResponse; +import com.yahoo.document.restapi.OperationHandler; +import org.junit.Test; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executor; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; + +public class RestApiMaxThreadTest { + final CountDownLatch latch = new CountDownLatch(1); + final AtomicInteger requestsInFlight = new AtomicInteger(0); + private class RestApiMocked extends RestApi { + + public RestApiMocked() { + super(mock(Executor.class), null, (OperationHandler)null); + } + + @Override + protected HttpResponse handleInternal(HttpRequest request) { + requestsInFlight.incrementAndGet(); + try { + latch.await(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return null; + } + } + + @Test + public void testCallsAreThrottled() throws InterruptedException { + RestApiMocked restApiMocked = new RestApiMocked(); + // Fire lots of requests. + for (int x = 0; x < 30; x++) { + new Thread(() -> restApiMocked.handle(null)).start(); + } + // Wait for all threads to be used + while (requestsInFlight.get() != 19) { + Thread.sleep(1); + } + // A new request should be blocked. + final HttpResponse response = restApiMocked.handle(null); + assertThat(response.getStatus(), is(429)); + latch.countDown(); + } +} |