From 4e19fac07a8d833e66ece900d28a6bd550524d6b Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Fri, 23 Jun 2023 03:17:58 -0400 Subject: [PATCH] Bulletproof AbstractProxyServlet#destory() to make it easier to write (#9938) unit tests for custom subclasses of AbstractProxyServlet Co-authored-by: Greg Wilkins --- .../jetty/proxy/AbstractProxyServlet.java | 7 +++- .../jetty/proxy/AbstractProxyServletTest.java | 39 +++++++++++++++++++ .../proxy/AsyncMiddleManServletTest.java | 13 +++++-- 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractProxyServletTest.java diff --git a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java index 34a2529e799..eb8901706d7 100644 --- a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java +++ b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java @@ -49,6 +49,7 @@ import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.io.ClientConnector; import org.eclipse.jetty.util.HttpCookieStore; import org.eclipse.jetty.util.StringUtil; +import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.slf4j.Logger; @@ -145,11 +146,13 @@ public abstract class AbstractProxyServlet extends HttpServlet { try { - _client.stop(); + LifeCycle.stop(_client); } catch (Exception x) { - if (_log.isDebugEnabled()) + if (_log == null) + x.printStackTrace(); + else if (_log.isDebugEnabled()) _log.debug("Failed to stop client", x); } } diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractProxyServletTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractProxyServletTest.java new file mode 100644 index 00000000000..f9c5f5af220 --- /dev/null +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractProxyServletTest.java @@ -0,0 +1,39 @@ +// +// ======================================================================== +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License v. 2.0 which is available at +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// ======================================================================== +// + +package org.eclipse.jetty.proxy; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.eclipse.jetty.client.api.Response.CompleteListener; +import org.junit.jupiter.api.Test; + +public class AbstractProxyServletTest +{ + + @Test + public void testNewDestroy() throws Exception + { + new AbstractProxyServlet() + { + private static final long serialVersionUID = 1L; + + @Override + protected CompleteListener newProxyResponseListener(HttpServletRequest clientRequest, HttpServletResponse proxyResponse) + { + return null; + } + }.destroy(); + } +} diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AsyncMiddleManServletTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AsyncMiddleManServletTest.java index 365ece15cdd..ffe2bf51ecc 100644 --- a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AsyncMiddleManServletTest.java +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AsyncMiddleManServletTest.java @@ -73,6 +73,7 @@ import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.Utf8StringBuilder; import org.eclipse.jetty.util.ajax.JSON; +import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; @@ -159,9 +160,15 @@ public class AsyncMiddleManServletTest @AfterEach public void dispose() throws Exception { - client.stop(); - proxy.stop(); - server.stop(); + LifeCycle.stop(client); + LifeCycle.stop(proxy); + LifeCycle.stop(proxy); + } + + @Test + public void testNewDestroy() throws Exception + { + new AsyncMiddleManServlet().destroy(); } @Test