Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/timgroup/statsd/StatsDProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected void writeBuilderToSendBuffer(ByteBuffer sendBuffer) {
byteBuffer[blen++] = (byte) (128 | (decoded & 63));
}

if (blen >= maxPacketSizeBytes) {
if (blen > maxPacketSizeBytes) {
throw new BufferOverflowException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import static org.hamcrest.Matchers.hasItem;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.AfterClass;
Expand Down Expand Up @@ -150,4 +152,65 @@ public void sends_too_long_multivalued_distribution_to_statsd() {
server.messagesReceived(),
hasItem(comparesEqualTo("my.prefix.mydistribution:667|d|@0.400000|#baz,foo:bar")));
}

@Test(timeout = 5000L)
public void sends_multivalued_distribution_chunk_that_exactly_fills_the_packet()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to test this directly with a StatsDProcessor unit test instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, done in 5e8e788. The client-level tests are gone; StatsDProcessorTest reads packets from the outbound queue instead. It covers an exact fit, a multibyte exact fit (bytes reach the limit, chars don't) and a two-chunk message whose first chunk is exactly full. All three fail on master with BufferOverflowException.

Side note: an exact fit still flushes an empty buffer first, because of the older remaining() < lowerBoundSize + 1 check. Happy to tighten that separately.

🤖 Generated by Claude Opus 5.5 with Claude Code

throws Exception {
// The first chunk, including its trailing newline, is exactly MAX_PACKET_SIZE bytes.
String first = "my.prefix.mydistribution:4234:2345:678|d|@0.400000|#baz,foo:bar";
String second = "my.prefix.mydistribution:1|d|@0.400000|#baz,foo:bar";
assertThat(first.length() + 1, comparesEqualTo(MAX_PACKET_SIZE));

RecordingErrorHandler errors = new RecordingErrorHandler();
DirectStatsDClient exactFitClient = buildClient(errors);
try {
exactFitClient.recordDistributionValues(
"mydistribution", new long[] {4234L, 2345L, 678L, 1L}, 0.4, "foo:bar", "baz");
waitForMessages(errors, first, second);
} finally {
exactFitClient.stop();
}
}

@Test(timeout = 5000L)
public void sends_message_that_exactly_fills_the_packet() throws Exception {
String prefixAndSuffix = "my.prefix.:1|d\n";
char[] aspect = new char[MAX_PACKET_SIZE - prefixAndSuffix.length()];
Arrays.fill(aspect, 'x');
String expected = "my.prefix." + new String(aspect) + ":1|d";

RecordingErrorHandler errors = new RecordingErrorHandler();
DirectStatsDClient exactFitClient = buildClient(errors);
try {
exactFitClient.distribution(new String(aspect), 1L);
waitForMessages(errors, expected);
} finally {
exactFitClient.stop();
}
}

private static DirectStatsDClient buildClient(StatsDClientErrorHandler errorHandler) {
return new NonBlockingStatsDClientBuilder()
.withEnvironmentVariables(CLIENT_ENV)
.prefix("my.prefix")
.hostname("localhost")
.port(STATSD_SERVER_PORT)
.enableTelemetry(false)
.originDetectionEnabled(false)
.maxPacketSizeBytes(MAX_PACKET_SIZE)
.errorHandler(errorHandler)
.buildDirectStatsDClient();
}

private static void waitForMessages(RecordingErrorHandler errors, String... expected)
throws InterruptedException {
List<String> expectedMessages = Arrays.asList(expected);
while (!server.messagesReceived().containsAll(expectedMessages)) {
List<Exception> exceptions = errors.getExceptions();
if (!exceptions.isEmpty()) {
throw new AssertionError("client reported errors: " + exceptions);
}
Thread.sleep(10L);
}
}
}
Loading