-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adjust PublishingTimeoutTest to new framework
- Loading branch information
Showing
14 changed files
with
779 additions
and
488 deletions.
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
...ts/src/common/java/pl/allegro/tech/hermes/integrationtests/client/FrontendSlowClient.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,126 @@ | ||
package pl.allegro.tech.hermes.integrationtests.client; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.Socket; | ||
import java.net.SocketException; | ||
import java.net.SocketTimeoutException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public class FrontendSlowClient { | ||
|
||
public static final String MSG_BODY = "{\"field\": \"value\"}"; | ||
|
||
public String msgHeadWithContentLenght(String topicName) { | ||
return "POST /topics/" + topicName + " HTTP/1.1\n" | ||
+ "Host: " + host + ":" + port + "\n" | ||
+ "Content-Type: application/json\n" | ||
+ "Content-Length: " + MSG_BODY.length() + "\r\n\r\n"; | ||
} | ||
|
||
private String msgHeadWithChunkedEncoding(String topicName) { | ||
return "POST /topics/" + topicName + " HTTP/1.1\n" | ||
+ "Host: " + host + ":" + port + "\n" | ||
+ "Content-Type: application/json\n" | ||
+ "Transfer-Encoding: chunked \r\n\r\n"; | ||
} | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(FrontendSlowClient.class); | ||
|
||
private final String host; | ||
private final int port; | ||
|
||
public FrontendSlowClient(String host, int port) { | ||
this.host = host; | ||
this.port = port; | ||
} | ||
|
||
private final ExecutorService executor = Executors.newSingleThreadExecutor(); | ||
|
||
public String slowEvent(int clientTimeout, int pauseTimeBetweenChunks, int delayBeforeSendingFirstData, String topicName) | ||
throws IOException, InterruptedException { | ||
return slowEvent(clientTimeout, pauseTimeBetweenChunks, delayBeforeSendingFirstData, topicName, false); | ||
} | ||
|
||
public String slowEvent(int clientTimeout, int pauseTimeBetweenChunks, int delayBeforeSendingFirstData, | ||
String topicName, boolean chunkedEncoding) | ||
throws IOException, InterruptedException { | ||
|
||
Socket clientSocket = new Socket(host, port); | ||
|
||
Thread.sleep(delayBeforeSendingFirstData); | ||
|
||
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); | ||
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | ||
|
||
clientSocket.setSoTimeout(clientTimeout); | ||
|
||
if (chunkedEncoding) { | ||
outToServer.writeBytes(msgHeadWithChunkedEncoding(topicName)); | ||
outToServer.flush(); | ||
} else { | ||
outToServer.writeBytes(msgHeadWithContentLenght(topicName)); | ||
outToServer.flush(); | ||
} | ||
sendBodyInChunks(outToServer, MSG_BODY, pauseTimeBetweenChunks, chunkedEncoding); | ||
|
||
String response; | ||
try { | ||
response = readResponse(inFromServer); | ||
LOGGER.info("Response: {}", response); | ||
} catch (SocketTimeoutException e) { | ||
LOGGER.warn("client timeout"); | ||
clientSocket.close(); | ||
throw e; | ||
} | ||
|
||
clientSocket.close(); | ||
|
||
return response; | ||
} | ||
|
||
private void sendBodyInChunks(DataOutputStream outToServer, String msgBody, int pauseTime, boolean encoded) { | ||
executor.execute(() -> { | ||
try { | ||
for (int index = 0; index < msgBody.length(); index++) { | ||
outToServer.writeBytes(prepareChunk(msgBody, index, encoded)); | ||
outToServer.flush(); | ||
LOGGER.info("Sent chunk"); | ||
if (pauseTime > 0) { | ||
Thread.sleep(pauseTime); | ||
} | ||
} | ||
if (encoded) { | ||
outToServer.writeBytes("0\r\n\r\n"); | ||
outToServer.flush(); | ||
LOGGER.info("Finished chunked encoding"); | ||
} | ||
} catch (SocketException e) { | ||
LOGGER.warn("Socket closed"); | ||
} catch (InterruptedException | IOException e) { | ||
LOGGER.error("Something went wrong while sending data", e); | ||
} | ||
}); | ||
} | ||
|
||
private String prepareChunk(String msg, int index, boolean encoded) { | ||
return encoded ? String.format("1\n%c\r\n", msg.charAt(index)) : String.valueOf(msg.charAt(index)); | ||
} | ||
|
||
private String readResponse(BufferedReader bufferedReader) throws IOException { | ||
String line; | ||
StringBuilder response = new StringBuilder(); | ||
|
||
while (!(line = bufferedReader.readLine()).equals("")) { | ||
response.append(line); | ||
} | ||
|
||
return response.toString(); | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
...n-tests/src/common/java/pl/allegro/tech/hermes/integrationtests/helpers/TraceHeaders.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,17 @@ | ||
package pl.allegro.tech.hermes.integrationtests.helpers; | ||
|
||
import pl.allegro.tech.hermes.integrationtests.metadata.TraceContext; | ||
|
||
import java.util.Map; | ||
|
||
public class TraceHeaders { | ||
public static Map<String, String> fromTraceContext(TraceContext traceContext) { | ||
return Map.of( | ||
"Trace-Id", traceContext.getTraceId(), | ||
"Span-Id", traceContext.getSpanId(), | ||
"Parent-Span-Id", traceContext.getParentSpanId(), | ||
"Trace-Sampled", traceContext.getTraceSampled(), | ||
"Trace-Reported", traceContext.getTraceReported() | ||
); | ||
} | ||
} |
Oops, something went wrong.