Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
Throw ClosedChannelException when FastJ8SocketChannel.read() fails du…
Browse files Browse the repository at this point in the history
…e to underlying being closed. Fixes #214
  • Loading branch information
nicktindall committed Oct 27, 2022
1 parent fa05549 commit 22ee0a2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Expand Up @@ -47,7 +47,7 @@ public int read(ByteBuffer buf) throws IOException {
if (buf == null)
throw new NullPointerException();

if (isBlocking() || isClosed() || !IOTools.isDirectBuffer(buf))
if (isBlocking() || isClosed() || !IOTools.isDirectBuffer(buf) || !super.isOpen())
return super.read(buf);
return read0(buf);
}
Expand Down
@@ -0,0 +1,41 @@
package net.openhft.chronicle.network.tcp;

import org.junit.Test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

import static org.junit.Assert.assertThrows;

public class FastJ8SocketChannelTest {

@Test
public void closedChannelExceptionIsThrownWhenAttemptIsMadeToReadFromClosedChannel() throws IOException {
try (final ServerSocketChannel local = ServerSocketChannel.open().bind(new InetSocketAddress("localhost", 0))) {
local.configureBlocking(false);
try (final SocketChannel remote = SocketChannel.open(local.socket().getLocalSocketAddress())) {
remote.configureBlocking(false);
final FastJ8SocketChannel fastJ8SocketChannel = new FastJ8SocketChannel(remote);
remote.close();
assertThrows(ClosedChannelException.class, () -> fastJ8SocketChannel.read(ByteBuffer.allocateDirect(100)));
}
}
}

@Test
public void closedChannelExceptionIsThrownWhenAttemptIsMadeToWriteToClosedChannel() throws IOException {
try (final ServerSocketChannel local = ServerSocketChannel.open().bind(new InetSocketAddress("localhost", 0))) {
local.configureBlocking(false);
try (final SocketChannel remote = SocketChannel.open(local.socket().getLocalSocketAddress())) {
remote.configureBlocking(false);
final FastJ8SocketChannel fastJ8SocketChannel = new FastJ8SocketChannel(remote);
remote.close();
assertThrows(ClosedChannelException.class, () -> fastJ8SocketChannel.write(ByteBuffer.allocateDirect(100)));
}
}
}
}

0 comments on commit 22ee0a2

Please sign in to comment.