< prev index next >

test/jdk/java/net/httpclient/ResponsePublisher.java

Print this page
*** 34,10 ***
--- 34,11 ---
  import com.sun.net.httpserver.HttpExchange;
  import com.sun.net.httpserver.HttpHandler;
  import com.sun.net.httpserver.HttpServer;
  import com.sun.net.httpserver.HttpsConfigurator;
  import com.sun.net.httpserver.HttpsServer;
+ import jdk.internal.net.http.common.OperationTrackers;
  import jdk.test.lib.net.SimpleSSLContext;
  import org.testng.annotations.AfterTest;
  import org.testng.annotations.BeforeTest;
  import org.testng.annotations.DataProvider;
  import org.testng.annotations.Test;

*** 98,10 ***
--- 99,20 ---
  
      static final int ITERATION_COUNT = 3;
      // a shared executor helps reduce the amount of threads created by the test
      static final Executor executor = Executors.newCachedThreadPool();
  
+     static final long start = System.nanoTime();
+ 
+     public static String now() {
+         long now = System.nanoTime() - start;
+         long secs = now / 1000_000_000;
+         long mill = (now % 1000_000_000) / 1000_000;
+         long nan = now % 1000_000;
+         return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
+     }
+ 
      interface BHS extends Supplier<BodyHandler<Publisher<List<ByteBuffer>>>> {
          static BHS of(BHS impl, String name) {
              return new BHSImpl(impl, name);
          }
      }

*** 214,10 ***
--- 225,16 ---
                  }
              }
              // Get the final result and compare it with the expected body
              String body = ofString.getBody().toCompletableFuture().get();
              assertEquals(body, "");
+             // ensure client closes before next iteration
+             if (!sameClient) {
+                 var tracker = TRACKER.getTracker(client);
+                 client = null;
+                 clientCleanup(tracker);
+             }
          }
      }
  
      @Test(dataProvider = "variants")
      public void testNoBody(String uri, boolean sameClient, BHS handlers) throws Exception {

*** 237,10 ***
--- 254,16 ---
              // subscribe to it.
              response.body().subscribe(ofString);
              // Get the final result and compare it with the expected body
              String body = ofString.getBody().toCompletableFuture().get();
              assertEquals(body, "");
+             // ensure client closes before next iteration
+             if (!sameClient) {
+                 var tracker = TRACKER.getTracker(client);
+                 client = null;
+                 clientCleanup(tracker);
+             }
          }
      }
  
      @Test(dataProvider = "variants")
      public void testNoBodyAsync(String uri, boolean sameClient, BHS handlers) throws Exception {

*** 263,10 ***
--- 286,16 ---
                                  responsePublisher.body().subscribe(ofString);
                                  return ofString.getBody();
                              });
              // Get the final result and compare it with the expected body
              assertEquals(result.get(), "");
+             // ensure client closes before next iteration
+             if (!sameClient) {
+                 var tracker = TRACKER.getTracker(client);
+                 client = null;
+                 clientCleanup(tracker);
+             }
          }
      }
  
      @Test(dataProvider = "variants")
      public void testAsString(String uri, boolean sameClient, BHS handlers) throws Exception {

*** 286,10 ***
--- 315,16 ---
              // subscribe to it.
              response.body().subscribe(ofString);
              // Get the final result and compare it with the expected body
              String body = ofString.getBody().toCompletableFuture().get();
              assertEquals(body, WITH_BODY);
+             // ensure client closes before next iteration
+             if (!sameClient) {
+                 var tracker = TRACKER.getTracker(client);
+                 client = null;
+                 clientCleanup(tracker);
+             }
          }
      }
  
      @Test(dataProvider = "variants")
      public void testAsStringAsync(String uri, boolean sameClient, BHS handlers) throws Exception {

*** 312,10 ***
--- 347,16 ---
                          return ofString.getBody();
                      });
              // Get the final result and compare it with the expected body
              String body = result.get();
              assertEquals(body, WITH_BODY);
+             // ensure client closes before next iteration
+             if (!sameClient) {
+                 var tracker = TRACKER.getTracker(client);
+                 client = null;
+                 clientCleanup(tracker);
+             }
          }
      }
  
      // A BodyHandler that returns PublishingBodySubscriber instances
      static class PublishingBodyHandler implements BodyHandler<Publisher<List<ByteBuffer>>> {

*** 449,10 ***
--- 490,27 ---
                  throw fail;
              }
          }
      }
  
+     // Wait for the client to be garbage collected.
+     // we use the ReferenceTracker API rather than HttpClient::close here,
+     // because we want to get some diagnosis if a client doesn't release
+     // its resources and terminates as expected
+     // By using the ReferenceTracker, we will get some diagnosis about what
+     // is keeping the client alive if it doesn't get GC'ed within the
+     // expected time frame.
+     public void clientCleanup(OperationTrackers.Tracker tracker){
+         System.gc();
+         System.out.println(now() + "waiting for client to shutdown: " + tracker.getName());
+         System.err.println(now() + "waiting for client to shutdown: " + tracker.getName());
+         var error = TRACKER.check(tracker, 10000);
+         if (error != null) throw error;
+         System.out.println(now() + "client shutdown normally: " + tracker.getName());
+         System.err.println(now() + "client shutdown normally: " + tracker.getName());
+     }
+ 
      static final String WITH_BODY = "Lorem ipsum dolor sit amet, consectetur" +
              " adipiscing elit, sed do eiusmod tempor incididunt ut labore et" +
              " dolore magna aliqua. Ut enim ad minim veniam, quis nostrud" +
              " exercitation ullamco laboris nisi ut aliquip ex ea" +
              " commodo consequat. Duis aute irure dolor in reprehenderit in " +
< prev index next >