Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 13 additions & 7 deletions src/test/java/org/scalasbt/ipcsocket/BlockingEchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ public void run() throws IOException {
() -> {
try {
clientChannel.configureBlocking(true);
ByteBuffer inBytes = SocketChannels.readAll(clientChannel);
final String line =
new String(inBytes.array(), "UTF-8").replace("\n", "").replace("\r", "");
System.out.println("server: " + line);
Thread.sleep(500);
clientChannel.write(inBytes.duplicate());
String rawLine;
do {
ByteBuffer inBytes = SocketChannels.readAll(clientChannel);
rawLine = new String(inBytes.array(), "UTF-8");
final String line =
rawLine.replace("\n", "").replace("\r", "").replace("\u001a", "");
// System.out.println("server: " + line);
ByteBuffer outBytes = inBytes.duplicate();
do {
clientChannel.write(outBytes);
// System.out.println("server: wrote " + writtenBytes + " bytes");
} while (outBytes.remaining() > 0);
} while (!rawLine.contains("\u001a") && !rawLine.contains("\n"));
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
}
return true;
});
Expand Down
19 changes: 12 additions & 7 deletions src/test/java/org/scalasbt/ipcsocket/NonBlockingEchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ public void run() throws IOException {
try {
clientChannel.configureBlocking(false);
try {
ByteBuffer inBytes = SocketChannels.readAll(clientChannel, READ_TIMEOUT_MILI);
final String line =
new String(inBytes.array(), "UTF-8").replace("\n", "").replace("\r", "");
System.out.println("server: " + line);
Thread.sleep(500);
clientChannel.write(inBytes.duplicate());
String rawLine;
do {
ByteBuffer inBytes = SocketChannels.readAll(clientChannel, READ_TIMEOUT_MILI);
rawLine = new String(inBytes.array(), "UTF-8");
final String line =
rawLine.replace("\n", "").replace("\r", "").replace("\u001a", "");
// System.out.println("server: " + line);
ByteBuffer outBytes = inBytes.duplicate();
do {
clientChannel.write(outBytes);
} while (outBytes.remaining() > 0);
} while (!rawLine.contains("\u001a") && !rawLine.contains("\n"));
} catch (SocketTimeoutException e) {
// if readAll doesn't complete in READ_TIMEOUT_MILI,
// SocketTimeoutException is thrown
Expand All @@ -38,7 +44,6 @@ public void run() throws IOException {
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
}
return true;
});
Expand Down
100 changes: 86 additions & 14 deletions src/test/java/org/scalasbt/ipcsocket/SocketChannelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void testNonBlockingEchoServer() throws IOException, InterruptedException
withSocket(
sock -> {
if (isJava17Plus() || ServerSocketChannels.isWin) {
String line = nonBlockingEchoServerTest(sock, 100, 600);
String line = nonBlockingEchoServerTest(sock, "hello", 100, 600);
assertEquals("echo did not return the content", "hello", line);
}
});
Expand All @@ -35,25 +35,60 @@ public void testTimeout() throws IOException, InterruptedException {
withSocket(
sock -> {
if (isJava17Plus() || ServerSocketChannels.isWin) {
String line = nonBlockingEchoServerTest(sock, 6000, 600);
String line = nonBlockingEchoServerTest(sock, "hello", 6000, 600);
assertEquals("echo did not timeout", "<unavailable>", line);
}
});
}

/*
@Test
public void testNonBlockingLargeMessage() throws IOException, InterruptedException {
System.out.println(
"SocketChannelTest#testNonBlockingLargeMessage(" + Boolean.toString(useJNI()) + ")");
withSocket(
sock -> {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1024 * 1024; i++) {
sb.append("a");
}
String message = sb.toString();
String line = nonBlockingEchoServerTest(sock, message, 100, 600);
assertEquals("echo did not return the content", message, line);
});
}
*/

/** Test the blocking echo server. */
@Test
public void testBlockingEchoServer() throws IOException, InterruptedException {
System.out.println(
"SocketChannelTest#testBlockingEchoServer(" + Boolean.toString(useJNI()) + ")");
withSocket(
sock -> {
String line = blockingEchoServerTest(sock);
String line = blockingEchoServerTest(sock, "hello");
assertEquals("echo did not return the content", "hello", line);
});
}

private String nonBlockingEchoServerTest(String sock, int sleepBeforeSend, int sleepBeforeReceive)
@Test
public void testBlockingLargeMessage() throws IOException, InterruptedException {
System.out.println(
"SocketChannelTest#testBlockingLargeMessage(" + Boolean.toString(useJNI()) + ")");
withSocket(
sock -> {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1024 * 1024; i++) {
sb.append("a");
}
String message = sb.toString();
String line = blockingEchoServerTest(sock, message);
assertEquals("echo did not return the content", message, line);
});
}

private String nonBlockingEchoServerTest(
String sock, String message, int sleepBeforeSend, int sleepBeforeReceive)
throws IOException, InterruptedException {
ServerSocketChannel serverSocket = ServerSocketChannels.newServerSocketChannel(sock, useJNI());
CompletableFuture<Boolean> server =
Expand All @@ -71,19 +106,36 @@ private String nonBlockingEchoServerTest(String sock, int sleepBeforeSend, int s
SocketChannel client = SocketChannels.newSocketChannel(sock.toString(), useJNI());
System.out.println("client: " + client.toString());
Thread.sleep(sleepBeforeSend);
client.write(ByteBuffer.wrap("hello\n".getBytes("UTF-8")));
Thread.sleep(100);
ByteBuffer buf = ByteBuffer.wrap((message + "\n").getBytes("UTF-8"));
client.configureBlocking(false);
CompletableFuture.supplyAsync(
() -> {
try {
do {
int written = client.write(buf);
// System.out.println("client: wrote " + Integer.toString(written) + " bytes");
} while (buf.remaining() > 0);
} catch (IOException e) {
e.printStackTrace();
}
return true;
});
Thread.sleep(100);

String line;
int ready = 0;
ready = SocketChannels.available(client);
System.out.println("client: " + Integer.toString(ready) + " bytes ready");
Thread.sleep(100);
ready = SocketChannels.available(client);
System.out.println("client: " + Integer.toString(ready) + " bytes ready");
Thread.sleep(sleepBeforeReceive);
ready = SocketChannels.available(client);
System.out.println("client: " + Integer.toString(ready) + " bytes ready");
if (ready <= 0) {
Thread.sleep(100);
ready = SocketChannels.available(client);
System.out.println("client: " + Integer.toString(ready) + " bytes ready");
}
if (ready <= 0) {
Thread.sleep(sleepBeforeReceive);
ready = SocketChannels.available(client);
System.out.println("client: " + Integer.toString(ready) + " bytes ready");
}
if (ready > 0) {
try {
line = SocketChannels.readLine(client, 1000);
Expand All @@ -99,7 +151,8 @@ private String nonBlockingEchoServerTest(String sock, int sleepBeforeSend, int s
return line;
}

private String blockingEchoServerTest(String sock) throws IOException, InterruptedException {
private String blockingEchoServerTest(String sock, String message)
throws IOException, InterruptedException {
ServerSocketChannel serverSocket = ServerSocketChannels.newServerSocketChannel(sock, useJNI());
CompletableFuture<Boolean> server =
CompletableFuture.supplyAsync(
Expand All @@ -116,7 +169,26 @@ private String blockingEchoServerTest(String sock) throws IOException, Interrupt
SocketChannel client = SocketChannels.newSocketChannel(sock.toString(), useJNI());
client.configureBlocking(false);
System.out.println("client: " + client.toString());
client.write(ByteBuffer.wrap("hello\n".getBytes("UTF-8")));
ByteBuffer buf = ByteBuffer.wrap((message + "\n").getBytes("UTF-8"));
CompletableFuture.supplyAsync(
() -> {
do {
try {
int written = client.write(buf);
if (written > 0) {
System.out.println("client: wrote " + Integer.toString(written) + " bytes");
} else {
try {
Thread.sleep(1);
} catch (Exception e2) {
}
}
} catch (IOException e) {
e.printStackTrace();
}
} while (buf.remaining() > 0);
return true;
});
Thread.sleep(600);
int readyBytes = 0;
int attempt = 0;
Expand Down
Loading