-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74dca07
commit 67633e4
Showing
3 changed files
with
142 additions
and
5 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
...sions/vertx-http/deployment/src/test/java/io/quarkus/vertx/http/ConnectionLimitsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package io.quarkus.vertx.http; | ||
|
||
import java.io.IOException; | ||
import java.net.Socket; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.event.Observes; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import io.vertx.core.Handler; | ||
import io.vertx.ext.web.Router; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
public class ConnectionLimitsTest { | ||
private static final String APP_PROPS = "" + | ||
"quarkus.http.limits.max-connections=1\n"; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addAsResource(new StringAsset(APP_PROPS), "application.properties") | ||
.addClasses(BeanRegisteringRouteUsingObserves.class)); | ||
|
||
@TestHTTPResource | ||
URL uri; | ||
|
||
@Test | ||
public void testConnectionLimits() throws Exception { | ||
try (Socket one = new Socket(uri.getHost(), uri.getPort())) { | ||
one.getOutputStream().write("GET /hello HTTP/1.1\r\nHost: localhost\r\n\r\n" | ||
.getBytes(StandardCharsets.UTF_8)); | ||
StringBuilder sb = new StringBuilder(); | ||
byte[] data = new byte[1024]; | ||
int j; | ||
while (!sb.toString().endsWith("hello")) { | ||
j = one.getInputStream().read(data); | ||
if (j == -1) { | ||
Assertions.fail("Did not read full HTTP response"); | ||
} | ||
sb.append(new String(data, 0, j, StandardCharsets.US_ASCII)); | ||
} | ||
//we now have one connection, and it has performed a request | ||
//start another one, it should fail | ||
|
||
try (Socket two = new Socket(uri.getHost(), uri.getPort())) { | ||
two.getOutputStream().write("GET /hello HTTP/1.1\r\nHost: localhost\r\n\r\n" | ||
.getBytes(StandardCharsets.UTF_8)); | ||
int res = two.getInputStream().read(data); | ||
if (res > 0) { | ||
Assertions.fail("Expected connection to fail"); | ||
} | ||
} catch (IOException expected) { | ||
|
||
} | ||
//verify the first connection is still fine | ||
sb.setLength(0); | ||
one.getOutputStream().write("GET /hello HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n" | ||
.getBytes(StandardCharsets.UTF_8)); | ||
String result = new String(one.getInputStream().readAllBytes(), StandardCharsets.US_ASCII); | ||
Assertions.assertTrue(result.endsWith("hello")); | ||
|
||
//first connection is closed, try second connection | ||
try (Socket two = new Socket(uri.getHost(), uri.getPort())) { | ||
two.getOutputStream().write("GET /hello HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n" | ||
.getBytes(StandardCharsets.UTF_8)); | ||
int res = two.getInputStream().read(data); | ||
Assertions.assertTrue(res > 0); | ||
} | ||
} | ||
} | ||
|
||
@ApplicationScoped | ||
static class BeanRegisteringRouteUsingObserves { | ||
|
||
public void register(@Observes Router router) { | ||
router.route("/hello").handler(new Handler<RoutingContext>() { | ||
@Override | ||
public void handle(RoutingContext event) { | ||
event.end("hello"); | ||
} | ||
}); | ||
|
||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters