< prev index next >

test/jdk/java/net/httpclient/MaxStreams.java

Print this page
*** 25,11 ***
   * @test
   * @bug 8196389
   * @summary Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
   * @library /test/lib /test/jdk/java/net/httpclient/lib
   * @build jdk.httpclient.test.lib.http2.Http2TestServer jdk.test.lib.net.SimpleSSLContext
!  * @run testng/othervm -ea -esa MaxStreams
   */
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
--- 25,11 ---
   * @test
   * @bug 8196389
   * @summary Should HttpClient support SETTINGS_MAX_CONCURRENT_STREAMS from the server
   * @library /test/lib /test/jdk/java/net/httpclient/lib
   * @build jdk.httpclient.test.lib.http2.Http2TestServer jdk.test.lib.net.SimpleSSLContext
!  * @run testng/othervm MaxStreams
   */
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;

*** 43,11 ***
  import java.util.concurrent.CompletableFuture;
  import java.util.concurrent.CompletionException;
  import java.util.concurrent.CountDownLatch;
  import java.util.concurrent.Executors;
  import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Semaphore;
  import javax.net.ssl.SSLContext;
  import java.net.http.HttpClient;
  import java.net.http.HttpRequest;
  import java.net.http.HttpResponse;
  import java.net.http.HttpResponse.BodyHandler;
--- 43,10 ---

*** 72,13 ***
      Http2TestServer https2TestServer;   // HTTP/2 ( h2 )
      final Http2FixedHandler handler = new Http2FixedHandler();
      SSLContext ctx;
      String http2FixedURI;
      String https2FixedURI;
-     volatile CountDownLatch latch;
      ExecutorService exec;
-     final Semaphore canStartTestRun = new Semaphore(1);
  
      // we send an initial warm up request, then MAX_STREAMS+1 requests
      // in parallel. The last of them should hit the limit.
      // Then we wait for all the responses and send a further request
      // which should succeed. The server should see (and respond to)
--- 71,11 ---

*** 96,15 ***
                  {https2FixedURI}
          };
      }
  
  
!     @Test(dataProvider = "uris", timeOut=20000)
      void testAsString(String uri) throws Exception {
!         System.err.println("Semaphore acquire");
-         canStartTestRun.acquire();
-         latch = new CountDownLatch(1);
          handler.setLatch(latch);
          HttpClient client = HttpClient.newBuilder().sslContext(ctx).build();
          List<CompletableFuture<HttpResponse<String>>> responses = new LinkedList<>();
  
          HttpRequest request = HttpRequest.newBuilder(URI.create(uri))
--- 93,13 ---
                  {https2FixedURI}
          };
      }
  
  
!     @Test(dataProvider = "uris")
      void testAsString(String uri) throws Exception {
!         CountDownLatch latch = new CountDownLatch(1);
          handler.setLatch(latch);
          HttpClient client = HttpClient.newBuilder().sslContext(ctx).build();
          List<CompletableFuture<HttpResponse<String>>> responses = new LinkedList<>();
  
          HttpRequest request = HttpRequest.newBuilder(URI.create(uri))

*** 207,35 ***
              return latch;
          }
  
          @Override
          public void handle(Http2TestExchange t) throws IOException {
-             int c = -1;
              try (InputStream is = t.getRequestBody();
                   OutputStream os = t.getResponseBody()) {
  
                  is.readAllBytes();
!                 c = counter.getAndIncrement();
                  if (c > 0 && c <= MAX_STREAMS) {
                      // Wait for latch.
                      try {
                          // don't send any replies until all requests are sent
                          System.err.println("Latch await");
                          getLatch().await();
                          System.err.println("Latch resume");
                      } catch (InterruptedException ee) {}
                  }
                  t.sendResponseHeaders(200, RESPONSE.length());
                  os.write(RESPONSE.getBytes());
-             } finally {
-                 // client issues MAX_STREAMS + 3 requests in total
-                 // but server should only see MAX_STREAMS + 2 in total. One is rejected by client
-                 // counter c captured before increment so final value is MAX_STREAMS + 1
-                 if (c == MAX_STREAMS + 1) {
-                     System.err.println("Semaphore release");
-                     counter.set(0);
-                     canStartTestRun.release();
-                 }
              }
          }
      }
  }
--- 202,31 ---
              return latch;
          }
  
          @Override
          public void handle(Http2TestExchange t) throws IOException {
              try (InputStream is = t.getRequestBody();
                   OutputStream os = t.getResponseBody()) {
  
                  is.readAllBytes();
!                 int c = counter.getAndIncrement();
                  if (c > 0 && c <= MAX_STREAMS) {
                      // Wait for latch.
                      try {
                          // don't send any replies until all requests are sent
                          System.err.println("Latch await");
                          getLatch().await();
                          System.err.println("Latch resume");
                      } catch (InterruptedException ee) {}
+                 } else if (c == MAX_STREAMS + 1) {
+                     // client issues MAX_STREAMS + 3 requests in total
+                     // but server should only see MAX_STREAMS + 2 in total. One is rejected by client
+                     // counter c captured before increment so final value is MAX_STREAMS + 1
+                     System.err.println("Counter reset");
+                     counter.set(0);
                  }
                  t.sendResponseHeaders(200, RESPONSE.length());
                  os.write(RESPONSE.getBytes());
              }
          }
      }
  }
< prev index next >