Skip to content

Commit

Permalink
Fixed potential bug; added another unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
jhg023 committed May 25, 2019
1 parent 089f6a9 commit dbb7e97
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ Maven:
<dependency>
<groupId>com.github.jhg023</groupId>
<artifactId>SimpleNet</artifactId>
<version>1.4.13</version>
<version>1.4.14</version>
</dependency>
```

Gradle:

```groovy
implementation 'com.github.jhg023:SimpleNet:1.4.13'
implementation 'com.github.jhg023:SimpleNet:1.4.14'
```

2. Because SimpleNet is compiled with Java 11, you must first require its module in your `module-info.java`:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>com.github.jhg023</groupId>
<artifactId>SimpleNet</artifactId>
<version>1.4.13</version>
<version>1.4.14</version>
<packaging>jar</packaging>

<dependencies>
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/simplenet/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,12 @@ public void completed(Integer result, Client client) {
queue.pollLast();
}

// TODO: After logging is added, warn the user if wrappedBuffer.hasRemaining() is true.

if (wrappedBuffer.hasRemaining()) {
if (Utility.isDebug()) {
System.err.println(wrappedBuffer.remaining() + " byte(s) still need to be read!");
}
}

while (!stack.isEmpty()) {
queue.offerLast(stack.pop());
}
Expand All @@ -152,17 +156,15 @@ public void completed(Integer result, Client client) {
client.inCallback.set(false);

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

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

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

if (!predicate.test(wrappedBuffer)) {
boolean shouldReturn = !predicate.test(wrappedBuffer);

if (wrappedBuffer.hasRemaining()) {
if (Utility.isDebug()) {
System.err.println(wrappedBuffer.remaining() + " byte(s) still need to be read!");
}
}

if (shouldReturn) {
return;
}

// TODO: After logging is added, warn the user if wrappedBuffer.hasRemaining() is true.
}

queue.offerFirst(pair);

if (!readInProgress.getAndSet(true)) {
channel.read(buffer, this, Listener.INSTANCE);
channel.read(buffer.position(size.get()), this, Listener.INSTANCE);
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/ReadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -340,6 +341,30 @@ void testReadNestedCallbacksExecuteInCorrectOrder() {
});
}

@Test
void readWithSmallBuffer() {
byte b = 42;
long l = ThreadLocalRandom.current().nextLong();
client = new Client(Long.BYTES);
client.onConnect(() -> {
Packet.builder().putByte(b).write(client);
Packet.builder().putLong(l).writeAndFlush(client);
});
server.close();
server = new Server(Long.BYTES);
server.bind(HOST, PORT);
server.onConnect(client -> {
client.readByte(first -> {
assertEquals(b, first);

client.readLong(second -> {
assertEquals(l, second);
latch.countDown();
});
});
});
}

private void readStringHelper(String s, Charset charset, ByteOrder order) {
client.onConnect(() -> Packet.builder().putString(s, charset, order).writeAndFlush(client));
server.onConnect(client -> client.readString(readString -> {
Expand Down

0 comments on commit dbb7e97

Please sign in to comment.