-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improving v1 connector socket close, and improving test cases for v3 …
…coverage (#8)
- Loading branch information
Showing
15 changed files
with
1,047 additions
and
226 deletions.
There are no files selected for viewing
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
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
98 changes: 98 additions & 0 deletions
98
src/test/java/com/hsbc/cranker/connector/ConcurrentUploadTest.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,98 @@ | ||
package com.hsbc.cranker.connector; | ||
|
||
import io.muserver.MuHandler; | ||
import io.muserver.MuServer; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.RepeatedTest; | ||
import org.junit.jupiter.api.RepetitionInfo; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static io.muserver.MuServerBuilder.httpServer; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class ConcurrentUploadTest extends BaseEndToEndTest { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ConcurrentUploadTest.class); | ||
|
||
private volatile MuHandler handler = (request, response) -> false; | ||
|
||
protected MuServer targetServer = httpServer() | ||
.addHandler((request, response) -> handler.handle(request, response)) | ||
.start(); | ||
|
||
private CrankerConnector connector; | ||
|
||
@BeforeEach | ||
void setUp(RepetitionInfo repetitionInfo) { | ||
connector = CrankerConnectorBuilder.connector() | ||
.withPreferredProtocols(preferredProtocols(repetitionInfo)) | ||
.withHttpClient(CrankerConnectorBuilder.createHttpClient(true).build()) | ||
.withRouterUris(RegistrationUriSuppliers.fixedUris(registrationUri(registrationServer.uri()))) | ||
.withRoute("*") | ||
.withTarget(targetServer.uri()) | ||
.withProxyEventListener(new ProxyEventListener() { | ||
@Override | ||
public void onProxyError(HttpRequest request, Throwable error) { | ||
log.warn("onProxyError, request=" + request, error); | ||
} | ||
}) | ||
.withComponentName("cranker-connector-unit-test") | ||
.start(); | ||
|
||
waitForRegistration("*", connector.connectorId(),2, crankerRouter); | ||
} | ||
|
||
@AfterEach | ||
public void stop() throws Exception { | ||
if (connector != null) assertTrue(connector.stop(10, TimeUnit.SECONDS)); | ||
if (targetServer != null) targetServer.stop(); | ||
} | ||
|
||
@RepeatedTest(3) | ||
public void postLargeBody() throws InterruptedException { | ||
|
||
handler = (request, response) -> { | ||
response.status(200); | ||
response.write(request.readBodyAsString()); | ||
return true; | ||
}; | ||
|
||
Queue<HttpResponse<String>> responses = new ConcurrentLinkedQueue<>(); | ||
CountDownLatch countDownLatch = new CountDownLatch(10); | ||
|
||
final String body = "c".repeat(10 * 1000); | ||
for(int i = 0; i < 10; i++) { | ||
final int finalI = i; | ||
new Thread(() -> { | ||
try { | ||
HttpResponse<String> resp = testClient.send(HttpRequest.newBuilder() | ||
.method("POST", HttpRequest.BodyPublishers.ofString(body)) | ||
.uri(crankerServer.uri().resolve("/?task=" + finalI)) | ||
.build(), HttpResponse.BodyHandlers.ofString()); | ||
responses.add(resp); | ||
countDownLatch.countDown(); | ||
} catch (Exception e) { | ||
log.error("Concurrent request error", e); | ||
responses.add(null); | ||
} | ||
}).start(); | ||
} | ||
|
||
assertTrue(countDownLatch.await(10, TimeUnit.SECONDS)); | ||
assertEquals(10, responses.size()); | ||
for (HttpResponse<String> response: responses) { | ||
assertNotNull(response); | ||
assertEquals(200, response.statusCode()); | ||
assertEquals(body, response.body()); | ||
} | ||
} | ||
} |
Oops, something went wrong.