1 package software.amazon.awssdk.crt.test; 2 3 import java.net.URI; 4 import java.util.concurrent.CompletableFuture; 5 6 import org.junit.Assume; 7 import org.junit.Test; 8 import software.amazon.awssdk.crt.http.HttpClientConnection; 9 import software.amazon.awssdk.crt.http.HttpClientConnectionManager; 10 import software.amazon.awssdk.crt.http.HttpClientConnectionManagerOptions; 11 import software.amazon.awssdk.crt.io.ClientBootstrap; 12 import software.amazon.awssdk.crt.io.EventLoopGroup; 13 import software.amazon.awssdk.crt.io.HostResolver; 14 import software.amazon.awssdk.crt.io.SocketOptions; 15 import software.amazon.awssdk.crt.io.TlsContext; 16 import software.amazon.awssdk.crt.io.TlsContextOptions; 17 18 /* 19 A temporary test that sets up an asynchronously-acquired resource (HttpClientConnection) and then immediately 20 exits in order to try and trigger a JVM shutdown while acquisition is still outstanding in native code, checking 21 the safety of the callback if the acquisition completes after JVM shutdown and before process exit. 22 23 This is temporary and only has a chance of hitting the condition if it's the only test ran (which is why 24 we filter it with an environment variable and run it as a single test in a separate CI step). 25 26 Long term, we want to switch this to a mvn execable utility that supports triggering JVM shutdown at controllable 27 points, in the same way that the Python CRT checks sloppy shutdown. 28 */ 29 public class ShutdownTest { 30 31 private static String SHUTDOWN_TEST_ENABLED = System.getenv("AWS_CRT_SHUTDOWN_TESTING"); 32 doShutdownTest()33 private static boolean doShutdownTest() { 34 return SHUTDOWN_TEST_ENABLED != null; 35 } 36 createConnectionManager(URI uri)37 private HttpClientConnectionManager createConnectionManager(URI uri) { 38 try (EventLoopGroup eventLoopGroup = new EventLoopGroup(1); 39 HostResolver resolver = new HostResolver(eventLoopGroup); 40 ClientBootstrap bootstrap = new ClientBootstrap(eventLoopGroup, resolver); 41 SocketOptions sockOpts = new SocketOptions(); 42 TlsContextOptions tlsOpts = TlsContextOptions.createDefaultClient(); 43 TlsContext tlsContext = new TlsContext(tlsOpts)) { 44 45 HttpClientConnectionManagerOptions options = new HttpClientConnectionManagerOptions(); 46 options.withClientBootstrap(bootstrap) 47 .withSocketOptions(sockOpts) 48 .withTlsContext(tlsContext) 49 .withUri(uri) 50 .withMaxConnections(1); 51 52 return HttpClientConnectionManager.create(options); 53 } 54 } 55 56 @Test testShutdownDuringAcquire()57 public void testShutdownDuringAcquire() throws Exception { 58 Assume.assumeTrue(doShutdownTest()); 59 60 HttpClientConnectionManager manager = createConnectionManager(new URI("https://aws-crt-test-stuff.s3.amazonaws.com")); 61 CompletableFuture<HttpClientConnection> connection = manager.acquireConnection(); 62 } 63 } 64