Skip to content

Commit

Permalink
Migrate to Java: TestUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
marianobarrios committed May 9, 2024
1 parent 90ebd6d commit e519adf
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 57 deletions.
32 changes: 16 additions & 16 deletions src/test/scala/tlschannel/CloseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ void testTcpImmediateClose() throws InterruptedException, IOException {
SocketGroups.SocketGroup serverGroup = socketPair.server;
ByteChannel client = clientGroup.external;
ByteChannel server = serverGroup.external;
Runnable clientFn = TestJavaUtil.cannotFailRunnable(() -> {
Runnable clientFn = TestUtil.cannotFailRunnable(() -> {
clientGroup.plain.close();
assertFalse(clientGroup.tls.shutdownSent());
assertFalse(clientGroup.tls.shutdownReceived());
Assertions.assertThrows(ClosedChannelException.class, () -> client.write(ByteBuffer.wrap(data)));
});
Runnable serverFn = TestJavaUtil.cannotFailRunnable(() -> {
Runnable serverFn = TestUtil.cannotFailRunnable(() -> {
ByteBuffer buffer = ByteBuffer.allocate(1);
assertEquals(-1, server.read(buffer));
assertFalse(serverGroup.tls.shutdownReceived());
Expand Down Expand Up @@ -84,14 +84,14 @@ void testTcpClose() throws InterruptedException, IOException {
SocketGroups.SocketGroup serverGroup = socketPair.server;
ByteChannel client = clientGroup.external;
ByteChannel server = serverGroup.external;
Runnable clientFn = TestJavaUtil.cannotFailRunnable(() -> {
Runnable clientFn = TestUtil.cannotFailRunnable(() -> {
client.write(ByteBuffer.wrap(data));
clientGroup.plain.close();
assertFalse(clientGroup.tls.shutdownSent());
assertFalse(clientGroup.tls.shutdownReceived());
Assertions.assertThrows(ClosedChannelException.class, () -> client.write(ByteBuffer.wrap(data)));
});
Runnable serverFn = TestJavaUtil.cannotFailRunnable(() -> {
Runnable serverFn = TestUtil.cannotFailRunnable(() -> {
ByteBuffer buffer = ByteBuffer.allocate(1);
assertEquals(1, server.read(buffer));
buffer.flip();
Expand Down Expand Up @@ -128,14 +128,14 @@ void testClose() throws InterruptedException {
SocketGroups.SocketGroup serverGroup = socketPair.server;
ByteChannel client = clientGroup.external;
ByteChannel server = serverGroup.external;
Runnable clientFn = TestJavaUtil.cannotFailRunnable(() -> {
Runnable clientFn = TestUtil.cannotFailRunnable(() -> {
client.write(ByteBuffer.wrap(data));
client.close();
assertTrue(clientGroup.tls.shutdownSent());
assertFalse(clientGroup.tls.shutdownReceived());
Assertions.assertThrows(ClosedChannelException.class, () -> client.write(ByteBuffer.wrap(data)));
});
Runnable serverFn = TestJavaUtil.cannotFailRunnable(() -> {
Runnable serverFn = TestUtil.cannotFailRunnable(() -> {
ByteBuffer buffer = ByteBuffer.allocate(1);
assertEquals(1, server.read(buffer));
buffer.flip();
Expand Down Expand Up @@ -172,14 +172,14 @@ void testCloseAndWait() throws InterruptedException {
SocketGroups.SocketGroup serverGroup = socketPair.server;
ByteChannel client = clientGroup.external;
ByteChannel server = serverGroup.external;
Runnable clientFn = TestJavaUtil.cannotFailRunnable(() -> {
Runnable clientFn = TestUtil.cannotFailRunnable(() -> {
client.write(ByteBuffer.wrap(data));
client.close();
assertTrue(clientGroup.tls.shutdownReceived());
assertTrue(clientGroup.tls.shutdownSent());
Assertions.assertThrows(ClosedChannelException.class, () -> client.write(ByteBuffer.wrap(data)));
});
Runnable serverFn = TestJavaUtil.cannotFailRunnable(() -> {
Runnable serverFn = TestUtil.cannotFailRunnable(() -> {
ByteBuffer buffer = ByteBuffer.allocate(1);
assertEquals(1, server.read(buffer));
buffer.flip();
Expand Down Expand Up @@ -216,11 +216,11 @@ void testCloseAndWaitForever() throws IOException, InterruptedException {
SocketGroups.SocketGroup serverGroup = socketPair.server;
ByteChannel client = clientGroup.external;
ByteChannel server = serverGroup.external;
Runnable clientFn = TestJavaUtil.cannotFailRunnable(() -> {
Runnable clientFn = TestUtil.cannotFailRunnable(() -> {
client.write(ByteBuffer.wrap(data));
client.close();
});
Runnable serverFn = TestJavaUtil.cannotFailRunnable(() -> {
Runnable serverFn = TestUtil.cannotFailRunnable(() -> {
ByteBuffer buffer = ByteBuffer.allocate(1);
assertEquals(1, server.read(buffer));
buffer.flip();
Expand Down Expand Up @@ -258,14 +258,14 @@ void testShutdownAndForget() throws InterruptedException, IOException {
SocketGroups.SocketGroup serverGroup = socketPair.server;
ByteChannel client = clientGroup.external;
ByteChannel server = serverGroup.external;
Runnable clientFn = TestJavaUtil.cannotFailRunnable(() -> {
Runnable clientFn = TestUtil.cannotFailRunnable(() -> {
client.write(ByteBuffer.wrap(data));
assertFalse(clientGroup.tls.shutdown());
assertFalse(clientGroup.tls.shutdownReceived());
assertTrue(clientGroup.tls.shutdownSent());
Assertions.assertThrows(ClosedChannelException.class, () -> client.write(ByteBuffer.wrap(data)));
});
Runnable serverFn = TestJavaUtil.cannotFailRunnable(() -> {
Runnable serverFn = TestUtil.cannotFailRunnable(() -> {
ByteBuffer buffer = ByteBuffer.allocate(1);
assertEquals(1, server.read(buffer));
buffer.flip();
Expand Down Expand Up @@ -300,7 +300,7 @@ void testShutdownAndWait() throws IOException, InterruptedException {
SocketGroups.SocketGroup serverGroup = socketPair.server;
ByteChannel client = clientGroup.external;
ByteChannel server = serverGroup.external;
Runnable clientFn = TestJavaUtil.cannotFailRunnable(() -> {
Runnable clientFn = TestUtil.cannotFailRunnable(() -> {
client.write(ByteBuffer.wrap(data));
// send first close_notify
assertFalse(clientGroup.tls.shutdown());
Expand All @@ -312,7 +312,7 @@ void testShutdownAndWait() throws IOException, InterruptedException {
assertTrue(clientGroup.tls.shutdownReceived());
assertTrue(clientGroup.tls.shutdownSent());
});
Runnable serverFn = TestJavaUtil.cannotFailRunnable(() -> {
Runnable serverFn = TestUtil.cannotFailRunnable(() -> {
ByteBuffer buffer = ByteBuffer.allocate(1);
assertEquals(1, server.read(buffer));
buffer.flip();
Expand Down Expand Up @@ -352,7 +352,7 @@ void testShutdownAndWaitForever() throws InterruptedException, IOException {
SocketGroups.SocketGroup serverGroup = socketPair.server;
ByteChannel client = clientGroup.external;
ByteChannel server = serverGroup.external;
Runnable clientFn = TestJavaUtil.cannotFailRunnable(() -> {
Runnable clientFn = TestUtil.cannotFailRunnable(() -> {
client.write(ByteBuffer.wrap(data));
// send first close_notify
assertFalse(clientGroup.tls.shutdown());
Expand All @@ -362,7 +362,7 @@ void testShutdownAndWaitForever() throws InterruptedException, IOException {
// wait for second close_notify
Assertions.assertThrows(AsynchronousCloseException.class, () -> clientGroup.tls.shutdown());
});
Runnable serverFn = TestJavaUtil.cannotFailRunnable(() -> {
Runnable serverFn = TestUtil.cannotFailRunnable(() -> {
ByteBuffer buffer = ByteBuffer.allocate(1);
assertEquals(1, server.read(buffer));
buffer.flip();
Expand Down
6 changes: 3 additions & 3 deletions src/test/scala/tlschannel/ConcurrentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void testReadSide() throws IOException {
}

private void writerLoop(int size, char ch, SocketGroup socketGroup) {
TestJavaUtil.cannotFail(() -> {
TestUtil.cannotFail(() -> {
logger.fine(() -> String.format("Starting writer loop, size: %s", size));
int bytesRemaining = size;
byte[] bufferArray = new byte[bufferSize];
Expand All @@ -89,7 +89,7 @@ private void writerLoop(int size, char ch, SocketGroup socketGroup) {
}

private void readerLoop(int size, SocketGroup socketGroup) {
TestJavaUtil.cannotFail(() -> {
TestUtil.cannotFail(() -> {
logger.fine(() -> String.format("Starting reader loop, size: %s", size));
byte[] readArray = new byte[bufferSize];
int bytesRemaining = size;
Expand All @@ -105,7 +105,7 @@ private void readerLoop(int size, SocketGroup socketGroup) {
}

private void readerLoopUntilEof(SocketGroup socketGroup, AtomicLong accumulator) {
TestJavaUtil.cannotFail(() -> {
TestUtil.cannotFail(() -> {
logger.fine("Starting reader loop");
byte[] readArray = new byte[bufferSize];
while (true) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/tlschannel/FailTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.junit.jupiter.api.TestInstance.Lifecycle;
import tlschannel.helpers.SocketPairFactory;
import tlschannel.helpers.SslContextFactory;
import tlschannel.helpers.TestJavaUtil;
import tlschannel.helpers.TestUtil;

@TestInstance(Lifecycle.PER_CLASS)
public class FailTest {
Expand All @@ -41,7 +41,7 @@ public void testPlanToTls() throws IOException, InterruptedException {
ServerTlsChannel serverChannel = serverChannelBuilder.build();

Runnable serverFn = () -> {
TestJavaUtil.cannotFail(() -> {
TestUtil.cannotFail(() -> {
ByteBuffer buffer = ByteBuffer.allocate(10000);
assertThrows(SSLException.class, () -> serverChannel.read(buffer));
try {
Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/tlschannel/InteroperabilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class InteroperabilityTest {
private final int margin = random.nextInt(100);

private void writerLoop(Writer writer, boolean renegotiate) {
TestJavaUtil.cannotFail(() -> {
TestUtil.cannotFail(() -> {
int remaining = dataSize;
while (remaining > 0) {
if (renegotiate) writer.renegotiate();
Expand All @@ -47,7 +47,7 @@ private void writerLoop(Writer writer, boolean renegotiate) {
}

private void readerLoop(Reader reader) {
TestJavaUtil.cannotFail(() -> {
TestUtil.cannotFail(() -> {
byte[] receivedData = new byte[dataSize + margin];
int remaining = dataSize;
while (remaining > 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/test/scala/tlschannel/helpers/Loops.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static void writerLoop(
boolean scattering,
boolean shutdown,
boolean close) {
TestJavaUtil.cannotFail(() -> {
TestUtil.cannotFail(() -> {
logger.fine(() -> String.format(
"Starting writer loop, size: %s, scattering: %s, renegotiate: %s", size, scattering, renegotiate));
SplittableRandom random = new SplittableRandom(seed);
Expand Down Expand Up @@ -138,7 +138,7 @@ public static void writerLoop(
public static void readerLoop(
int size, SocketGroup socketGroup, boolean gathering, boolean readEof, boolean close) {

TestJavaUtil.cannotFail(() -> {
TestUtil.cannotFail(() -> {
logger.fine(() -> String.format("Starting reader loop. Size: $size, gathering: %s", gathering));
byte[] readArray = new byte[bufferSize];
int bytesRemaining = size;
Expand Down Expand Up @@ -183,7 +183,7 @@ private static byte[] hash(int size) {
}
}

public static final Function<Integer, byte[]> expectedBytesHash = new TestJavaUtil.Memo<>(Loops::hash)::apply;
public static final Function<Integer, byte[]> expectedBytesHash = new TestUtil.Memo<>(Loops::hash)::apply;

private static ByteBuffer[] multiWrap(ByteBuffer buffer) {
return new ByteBuffer[] {ByteBuffer.allocate(0), buffer, ByteBuffer.allocate(0)};
Expand Down
3 changes: 1 addition & 2 deletions src/test/scala/tlschannel/helpers/NonBlockingLoops.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ public static Report loop(List<SocketPair> socketPairs, int dataSize, boolean re
selector.select(); // block

for (Endpoint endpoint : Stream.concat(
getSelectedEndpoints(selector),
TestJavaUtil.removeAndCollect(readyTaskSockets.iterator()))
getSelectedEndpoints(selector), TestUtil.removeAndCollect(readyTaskSockets.iterator()))
.collect(Collectors.toList())) {
try {
if (endpoint instanceof WriterEndpoint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.SplittableRandom;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

public class TestJavaUtil {
public class TestUtil {

public static <A> Stream<A> removeAndCollect(Iterator<A> iterator) {
List<A> builder = new ArrayList<>();
Expand All @@ -25,7 +26,7 @@ public interface ExceptionalRunnable {
void run() throws Exception;
}

private static final Logger logger = Logger.getLogger(TestJavaUtil.class.getName());
private static final Logger logger = Logger.getLogger(TestUtil.class.getName());

public static void cannotFail(ExceptionalRunnable exceptionalRunnable) {
cannotFailRunnable(exceptionalRunnable).run();
Expand Down Expand Up @@ -64,4 +65,22 @@ public O apply(I i) {
return cache.computeIfAbsent(i, f);
}
}

public static void nextBytes(SplittableRandom random, byte[] bytes) {
nextBytes(random, bytes, bytes.length);
}

public static void nextBytes(SplittableRandom random, byte[] bytes, int len) {
int i = 0;
while (i < len) {
int rnd = random.nextInt();
int n = Math.min(len - i, Integer.SIZE / java.lang.Byte.SIZE);
while (n > 0) {
bytes[i] = (byte) rnd;
rnd >>= java.lang.Byte.SIZE;
n -= 1;
i += 1;
}
}
}
}
27 changes: 0 additions & 27 deletions src/test/scala/tlschannel/helpers/TestUtil.scala

This file was deleted.

0 comments on commit e519adf

Please sign in to comment.