Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
104 changes: 104 additions & 0 deletions src/test/java/com/timgroup/statsd/StatsDProcessorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.timgroup.statsd;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Test;

public class StatsDProcessorTest {

private static final int MAX_PACKET_SIZE = 64;

private final RecordingErrorHandler errors = new RecordingErrorHandler();
private StatsDProcessor processor;

@After
public void stop() throws InterruptedException {
if (processor != null) {
processor.shutdown(false);
}
}

@Test(timeout = 5000L)
public void writes_message_that_exactly_fills_the_packet() throws Exception {
String packet = packet("", MAX_PACKET_SIZE);

assertEquals(Arrays.asList(packet), process(new ChunkedMessage(packet), 1));
}

@Test(timeout = 5000L)
public void writes_multibyte_message_that_exactly_fills_the_packet() throws Exception {
// One character short of the limit, but 'é' takes two bytes in UTF-8.
String packet = packet("é", MAX_PACKET_SIZE - 1);

assertEquals(Arrays.asList(packet), process(new ChunkedMessage(packet), 1));
}

@Test(timeout = 5000L)
public void writes_every_chunk_when_one_exactly_fills_the_packet() throws Exception {
String first = packet("", MAX_PACKET_SIZE);
String second = packet("", 10);

assertEquals(Arrays.asList(first, second), process(new ChunkedMessage(first, second), 2));
}

/** A packet of {@code length} chars that starts with {@code head} and ends with a newline. */
private static String packet(String head, int length) {
char[] filler = new char[length - head.length() - 1];
Arrays.fill(filler, 'x');
return head + new String(filler) + "\n";
}

private List<String> process(Message message, int expectedPackets) throws Exception {

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.

These tests need to touch every aspect of the processor just to test one condition. Would it be possible to test just the fixed method directly? We can isolate it in a new class (for example StatsDProcessor.Encoder) to make testing it easier.

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.

Done in 4c35740. The UTF-8 loop now lives in StatsDProcessor.Encoder, unchanged apart from the fix, and StatsDProcessorEncoderTest calls it directly with a plain ByteBuffer. It covers exact fits made of 1-, 2-, 3- and 4-byte chars, which all fail with >=, plus three overflow cases. StatsDProcessorTest is gone.

🤖 Generated by Claude Opus 5.5 with Claude Code

processor =
new StatsDNonBlockingProcessor(
16, errors, MAX_PACKET_SIZE, 8, 1, 0, 1, Executors.defaultThreadFactory());
processor.startWorkers("StatsDProcessorTest-");
assertTrue(processor.send(message));

List<String> packets = new ArrayList<>();
while (packets.size() < expectedPackets) {
List<Exception> exceptions = errors.getExceptions();
if (!exceptions.isEmpty()) {
throw new AssertionError("processor reported errors: " + exceptions);
}
ByteBuffer buffer = processor.getOutboundQueue().poll(10, TimeUnit.MILLISECONDS);
// An exact fit makes the processor flush its current buffer first, even when it is
// still empty.
if (buffer == null || buffer.position() == 0) {
continue;
}
buffer.flip();
packets.add(StandardCharsets.UTF_8.decode(buffer).toString());
}
return packets;
}

/** Writes one prepared chunk per call, like a multi-valued message split across packets. */
private static final class ChunkedMessage extends Message {
private final String[] chunks;
private int next;

ChunkedMessage(String... chunks) {
super(Message.Type.DISTRIBUTION, TagsCardinality.DEFAULT);
this.chunks = chunks;
}

@Override
boolean writeTo(StringBuilder builder, int capacity) {
builder.append(chunks[next++]);
return next < chunks.length;
}

@Override
public void aggregate(Message message) {}
}
}
Loading