Skip to content
Permalink
Browse files
8266897: com/sun/net/httpserver/FilterTest.java fails intermittently …
…with AssertionError

Reviewed-by: chegar, dfuchs, michaelm
  • Loading branch information
FrauBoes committed May 18, 2021
1 parent da7c846 commit 894547d2c102dcbe1f9ec8a1edb11c6b31e4270e
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -24,11 +24,11 @@
*/

package sun.net.httpserver;
import java.io.*;
import java.util.*;
import java.lang.System.Logger;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import com.sun.net.httpserver.*;
import com.sun.net.httpserver.spi.*;

/**
* HttpContext represents a mapping between a protocol (http or https) together with a root URI path
@@ -40,17 +40,17 @@
*/
class HttpContextImpl extends HttpContext {

private String path;
private String protocol;
private HttpHandler handler;
private Map<String,Object> attributes = new HashMap<String,Object>();
private ServerImpl server;
private final String path;
private final String protocol;
private final ServerImpl server;
private final AuthFilter authfilter;
private final Map<String,Object> attributes = new ConcurrentHashMap<>();
/* system filters, not visible to applications */
private LinkedList<Filter> sfilters = new LinkedList<Filter>();
private final List<Filter> sfilters = new CopyOnWriteArrayList<>();
/* user filters, set by applications */
private LinkedList<Filter> ufilters = new LinkedList<Filter>();
private final List<Filter> ufilters = new CopyOnWriteArrayList<>();
private Authenticator authenticator;
private AuthFilter authfilter;
private HttpHandler handler;

/**
* constructor is package private.
@@ -654,11 +654,11 @@ public void run () {
* They are linked together by a LinkHandler
* so that they can both be invoked in one call.
*/
List<Filter> sf = ctx.getSystemFilters();
List<Filter> uf = ctx.getFilters();
final List<Filter> sf = ctx.getSystemFilters();
final List<Filter> uf = ctx.getFilters();

Filter.Chain sc = new Filter.Chain(sf, ctx.getHandler());
Filter.Chain uc = new Filter.Chain(uf, new LinkHandler (sc));
final Filter.Chain sc = new Filter.Chain(sf, ctx.getHandler());
final Filter.Chain uc = new Filter.Chain(uf, new LinkHandler (sc));

/* set up the two stream references */
tx.getRequestBody();
@@ -38,8 +38,7 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.CompletableFuture;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -165,9 +164,9 @@ public void testBeforeHandlerSendResponse() throws Exception {
@Test
public void testAfterHandler() throws Exception {
var handler = new EchoHandler();
var respCode = new AtomicInteger();
var respCode = new CompletableFuture<Integer>();
var filter = Filter.afterHandler("Log response code",
e -> respCode.set(e.getResponseCode()));
e -> respCode.complete(e.getResponseCode()));
var server = HttpServer.create(new InetSocketAddress(LOOPBACK_ADDR, 0), 10);
server.createContext("/", handler).getFilters().add(filter);
server.start();
@@ -176,7 +175,7 @@ public void testAfterHandler() throws Exception {
var request = HttpRequest.newBuilder(uri(server, "")).build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals(response.statusCode(), 200);
assertEquals(response.statusCode(), respCode.get());
assertEquals(response.statusCode(), (int)respCode.get());
} finally {
server.stop(0);
}
@@ -185,12 +184,12 @@ public void testAfterHandler() throws Exception {
@Test
public void testAfterHandlerRepeated() throws Exception {
var handler = new EchoHandler();
var attr = new AtomicReference<String>();
var attr = new CompletableFuture<String>();
final var value = "some value";
var filter1 = Filter.afterHandler("Set attribute",
e -> e.setAttribute("test-attr", value));
var filter2 = Filter.afterHandler("Read attribute",
e -> attr.set((String) e.getAttribute("test-attr")));
e -> attr.complete((String) e.getAttribute("test-attr")));
var server = HttpServer.create(new InetSocketAddress(LOOPBACK_ADDR, 0), 10);
var context = server.createContext("/", handler);
context.getFilters().add(filter2);
@@ -210,7 +209,7 @@ public void testAfterHandlerRepeated() throws Exception {
@Test
public void testAfterHandlerSendResponse() throws Exception {
var handler = new NoResponseHandler();
var respCode = new AtomicInteger();
var respCode = new CompletableFuture<Integer>();
var filter = Filter.afterHandler("Log response code and send response",
e -> {
try (InputStream is = e.getRequestBody();
@@ -219,7 +218,7 @@ public void testAfterHandlerSendResponse() throws Exception {
var resp = "hello world".getBytes(StandardCharsets.UTF_8);
e.sendResponseHeaders(200, resp.length);
os.write(resp);
respCode.set(e.getResponseCode());
respCode.complete(e.getResponseCode());
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
throw new UncheckedIOException(ioe);
@@ -233,7 +232,7 @@ public void testAfterHandlerSendResponse() throws Exception {
var request = HttpRequest.newBuilder(uri(server, "")).build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals(response.statusCode(), 200);
assertEquals(response.statusCode(), respCode.get());
assertEquals(response.statusCode(), (int)respCode.get());
} finally {
server.stop(0);
}
@@ -242,11 +241,11 @@ public void testAfterHandlerSendResponse() throws Exception {
@Test
public void testBeforeAndAfterHandler() throws Exception {
var handler = new EchoHandler();
var respCode = new AtomicInteger();
var respCode = new CompletableFuture<Integer>();
var beforeFilter = Filter.beforeHandler("Add x-foo response header",
e -> e.getResponseHeaders().set("x-foo", "bar"));
var afterFilter = Filter.afterHandler("Log response code",
e -> respCode.set(e.getResponseCode()));
e -> respCode.complete(e.getResponseCode()));
var server = HttpServer.create(new InetSocketAddress(LOOPBACK_ADDR, 0), 10);
var context = server.createContext("/", handler);
context.getFilters().add(beforeFilter);
@@ -257,9 +256,9 @@ public void testBeforeAndAfterHandler() throws Exception {
var request = HttpRequest.newBuilder(uri(server, "")).build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals(response.statusCode(), 200);
assertEquals(response.statusCode(), respCode.get());
assertEquals(response.headers().map().size(), 3);
assertEquals(response.headers().firstValue("x-foo").orElseThrow(), "bar");
assertEquals(response.statusCode(), (int)respCode.get());
} finally {
server.stop(0);
}

1 comment on commit 894547d

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 894547d May 18, 2021

Please sign in to comment.