Skip to content

Commit dbb7e97

Browse files
author
Jacob G
committed
Fixed potential bug; added another unit test.
1 parent 089f6a9 commit dbb7e97

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ Maven:
1111
<dependency>
1212
<groupId>com.github.jhg023</groupId>
1313
<artifactId>SimpleNet</artifactId>
14-
<version>1.4.13</version>
14+
<version>1.4.14</version>
1515
</dependency>
1616
```
1717

1818
Gradle:
1919

2020
```groovy
21-
implementation 'com.github.jhg023:SimpleNet:1.4.13'
21+
implementation 'com.github.jhg023:SimpleNet:1.4.14'
2222
```
2323

2424
2. Because SimpleNet is compiled with Java 11, you must first require its module in your `module-info.java`:

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<groupId>com.github.jhg023</groupId>
1414
<artifactId>SimpleNet</artifactId>
15-
<version>1.4.13</version>
15+
<version>1.4.14</version>
1616
<packaging>jar</packaging>
1717

1818
<dependencies>

src/main/java/simplenet/Client.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,12 @@ public void completed(Integer result, Client client) {
137137
queue.pollLast();
138138
}
139139

140-
// TODO: After logging is added, warn the user if wrappedBuffer.hasRemaining() is true.
141-
140+
if (wrappedBuffer.hasRemaining()) {
141+
if (Utility.isDebug()) {
142+
System.err.println(wrappedBuffer.remaining() + " byte(s) still need to be read!");
143+
}
144+
}
145+
142146
while (!stack.isEmpty()) {
143147
queue.offerLast(stack.pop());
144148
}
@@ -152,17 +156,15 @@ public void completed(Integer result, Client client) {
152156
client.inCallback.set(false);
153157

154158
if (client.size.get() > 0) {
155-
// If we are about to call `channel.read`, then the position
156-
// of the buffer must be placed after any remaining, unprocessed
157-
// data. Regardless, it should be compacted as well.
158-
buffer.compact().position(isQueueEmpty ? 0 : client.size.get());
159+
buffer.compact();
159160
} else {
160161
buffer.clear();
161162
}
162163

163164
if (isQueueEmpty) {
164165
// Because the queue is empty, the client should not attempt to read more data until
165166
// more is requested by the user.
167+
buffer.position(0);
166168
client.readInProgress.set(false);
167169
} else {
168170
// Because the queue is NOT empty and we don't have enough data to process the request,
@@ -548,17 +550,23 @@ public void readUntil(int n, Predicate<ByteBuffer> predicate, ByteOrder order) {
548550

549551
var wrappedBuffer = ByteBuffer.wrap(data).order(order);
550552

551-
if (!predicate.test(wrappedBuffer)) {
553+
boolean shouldReturn = !predicate.test(wrappedBuffer);
554+
555+
if (wrappedBuffer.hasRemaining()) {
556+
if (Utility.isDebug()) {
557+
System.err.println(wrappedBuffer.remaining() + " byte(s) still need to be read!");
558+
}
559+
}
560+
561+
if (shouldReturn) {
552562
return;
553563
}
554-
555-
// TODO: After logging is added, warn the user if wrappedBuffer.hasRemaining() is true.
556564
}
557565

558566
queue.offerFirst(pair);
559567

560568
if (!readInProgress.getAndSet(true)) {
561-
channel.read(buffer, this, Listener.INSTANCE);
569+
channel.read(buffer.position(size.get()), this, Listener.INSTANCE);
562570
}
563571
}
564572
}

src/test/java/ReadTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.nio.charset.StandardCharsets;
2828
import java.util.ArrayDeque;
2929
import java.util.concurrent.CountDownLatch;
30+
import java.util.concurrent.ThreadLocalRandom;
3031
import java.util.concurrent.TimeUnit;
3132
import org.junit.jupiter.api.AfterEach;
3233
import org.junit.jupiter.api.BeforeEach;
@@ -340,6 +341,30 @@ void testReadNestedCallbacksExecuteInCorrectOrder() {
340341
});
341342
}
342343

344+
@Test
345+
void readWithSmallBuffer() {
346+
byte b = 42;
347+
long l = ThreadLocalRandom.current().nextLong();
348+
client = new Client(Long.BYTES);
349+
client.onConnect(() -> {
350+
Packet.builder().putByte(b).write(client);
351+
Packet.builder().putLong(l).writeAndFlush(client);
352+
});
353+
server.close();
354+
server = new Server(Long.BYTES);
355+
server.bind(HOST, PORT);
356+
server.onConnect(client -> {
357+
client.readByte(first -> {
358+
assertEquals(b, first);
359+
360+
client.readLong(second -> {
361+
assertEquals(l, second);
362+
latch.countDown();
363+
});
364+
});
365+
});
366+
}
367+
343368
private void readStringHelper(String s, Charset charset, ByteOrder order) {
344369
client.onConnect(() -> Packet.builder().putString(s, charset, order).writeAndFlush(client));
345370
server.onConnect(client -> client.readString(readString -> {

0 commit comments

Comments
 (0)