Bulletproof AbstractProxyServlet#destory() to make it easier to write (#9938)

unit tests for custom subclasses of AbstractProxyServlet

Co-authored-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Gary Gregory 2023-06-23 03:17:58 -04:00 committed by GitHub
parent 73dfac9732
commit 4e19fac07a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 5 deletions

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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