Skip to content

Commit

Permalink
Truncate messages to not fail ingestion (#17)
Browse files Browse the repository at this point in the history
* Truncate messages to not fail ingestion

* Truncate as we do in CLI AnimatedProgressBar

* Add nullable annotations

* Ensure we properly truncate messages

* Add note with link about UTF-8 handling
  • Loading branch information
timtebeek authored Sep 12, 2023
1 parent 9271cae commit 7400a57
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public ProgressBar setMax(int max) {

private void send(Request.Type type, @Nullable String message) {
try {
if (message != null && message.length() + 1 > MAX_MESSAGE_SIZE) {
throw new IllegalArgumentException("Message size exceeded maximum length: " + message);
}
// UTF-8 encoding is not guaranteed to be 1 byte per character, might handle that in the future as per:
// https://github.com/openrewrite/rewrite-polyglot/pull/17#discussion_r1322841060
message = truncateMessage(message, MAX_MESSAGE_SIZE - 1);
byte[] buf = (type.ordinal() + (message == null ? "" : message)).getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, port);
socket.send(packet);
Expand All @@ -93,6 +93,13 @@ private void send(Request.Type type, @Nullable String message) {
}
}

static @Nullable String truncateMessage(@Nullable String message, int maxLength) {
if (message == null || message.length() <= maxLength) {
return message;
}
return "..." + message.substring(Math.max(message.length() - maxLength + 3, 0));
}

@Value
static class Request {
enum Type {
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/org/openrewrite/polyglot/RemoteProgressBarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
package org.openrewrite.polyglot;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.openrewrite.internal.lang.Nullable;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -75,4 +79,32 @@ public ProgressBar setMax(int max) {
assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue();
}
}

@ParameterizedTest
@MethodSource("longStrings")
void truncate(String input, String output) {
int maxLength = 255;
String actual = RemoteProgressBarSender.truncateMessage(input, maxLength);
assertThat(actual).isEqualTo(output);
if (maxLength <= input.length()) {
assertThat(actual).hasSize(maxLength);
} else {
assertThat(actual).hasSize(input.length());
}
}

private static Stream<Arguments> longStrings() {
String twohunderdfifty = "1234567890".repeat(25);
return Stream.of(
Arguments.of(twohunderdfifty + "123", twohunderdfifty + "123"),
Arguments.of(twohunderdfifty + "1234", twohunderdfifty + "1234"),
Arguments.of(twohunderdfifty + "12345", twohunderdfifty + "12345"),
Arguments.of(twohunderdfifty + "123456", "..." + twohunderdfifty.substring(4) + "123456"),
Arguments.of(twohunderdfifty + "1234567", "..." + twohunderdfifty.substring(5) + "1234567"),
Arguments.of(twohunderdfifty + "12345678", "..." + twohunderdfifty.substring(6) + "12345678"),
Arguments.of(twohunderdfifty + "123456789", "..." + twohunderdfifty.substring(7) + "123456789"),
Arguments.of(twohunderdfifty + "1234567890", "..." + twohunderdfifty.substring(8) + "1234567890")
);
}

}

0 comments on commit 7400a57

Please sign in to comment.