Skip to content

Commit

Permalink
Migrate to Java: AsyncQuickCloseTest
Browse files Browse the repository at this point in the history
  • Loading branch information
marianobarrios committed Apr 21, 2024
1 parent fc0c7c6 commit 32d04a7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 82 deletions.
84 changes: 84 additions & 0 deletions src/test/scala/tlschannel/async/AsyncQuickCloseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package tlschannel.async;

import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import tlschannel.helpers.SocketPairFactory;
import tlschannel.helpers.SslContextFactory;

@TestInstance(Lifecycle.PER_CLASS)
public class AsyncQuickCloseTest implements AsyncTestBase {

private final SslContextFactory sslContextFactory = new SslContextFactory();
private final SocketPairFactory factory = new SocketPairFactory(sslContextFactory.defaultContext());

/*
* Closing sockets registered in an asynchronous channel group is inherently racy, using repetitions to try to catch
* most races.
*/
private final int repetitions = 250;

private final int bufferSize = 10000;

// see https://github.com/marianobarrios/tls-channel/issues/34
// immediate closings after registration
@Test
public void testImmediateClose() throws IOException {
AsynchronousTlsChannelGroup channelGroup = new AsynchronousTlsChannelGroup();
for (int i = 1; i <= repetitions; i++) {
// create (and register) channels and close immediately
tlschannel.helpers.SocketGroups.AsyncSocketPair socketPair = factory.async(null, channelGroup, true, false);
socketPair.server.external.close();
socketPair.client.external.close();

// try read
ByteBuffer readBuffer = ByteBuffer.allocate(bufferSize);
Future<Integer> readFuture = socketPair.server.external.read(readBuffer);
Exception e1 = Assertions.assertThrows(ExecutionException.class, () -> readFuture.get());
assertInstanceOf(ClosedChannelException.class, e1.getCause());

// try write
Future<Integer> writeFuture = socketPair.client.external.write(ByteBuffer.wrap(new byte[] {1}));
Exception e2 = Assertions.assertThrows(ExecutionException.class, () -> writeFuture.get());
assertInstanceOf(ClosedChannelException.class, e2.getCause());
}
assertTrue(channelGroup.isAlive());
channelGroup.shutdown();
assertChannelGroupConsistency(channelGroup);
}

// immediate closings after registration, even if we close the raw channel
@Test
public void testRawImmediateClosing() throws IOException {
AsynchronousTlsChannelGroup channelGroup = new AsynchronousTlsChannelGroup();
for (int i = 1; i <= repetitions; i++) {
// create (and register) channels and close immediately
tlschannel.helpers.SocketGroups.AsyncSocketPair socketPair = factory.async(null, channelGroup, true, false);
socketPair.server.plain.close();
socketPair.client.plain.close();

// try read
ByteBuffer readBuffer = ByteBuffer.allocate(bufferSize);
Future<Integer> readFuture = socketPair.server.external.read(readBuffer);
Exception readEx = Assertions.assertThrows(ExecutionException.class, () -> readFuture.get());
assertInstanceOf(ClosedChannelException.class, readEx.getCause());

// try write
Future<Integer> writeFuture = socketPair.client.external.write(ByteBuffer.wrap(new byte[] {1}));
Exception writeEx = Assertions.assertThrows(ExecutionException.class, () -> writeFuture.get());
assertInstanceOf(ClosedChannelException.class, writeEx.getCause());
}
assertTrue(channelGroup.isAlive());
channelGroup.shutdown();
assertChannelGroupConsistency(channelGroup);
}
}
82 changes: 0 additions & 82 deletions src/test/scala/tlschannel/async/AsyncQuickCloseTest.scala

This file was deleted.

0 comments on commit 32d04a7

Please sign in to comment.