diff --git a/tests/e2e-client/src/test/java/org/glassfish/jersey/tests/e2e/client/ClientDestroyTest.java b/tests/e2e-client/src/test/java/org/glassfish/jersey/tests/e2e/client/ClientDestroyTest.java index 9997a128aa..afb4134d8d 100644 --- a/tests/e2e-client/src/test/java/org/glassfish/jersey/tests/e2e/client/ClientDestroyTest.java +++ b/tests/e2e-client/src/test/java/org/glassfish/jersey/tests/e2e/client/ClientDestroyTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2020 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -172,8 +172,9 @@ public static boolean isInitialized() { } public static class BarListener implements ClientRequestFilter, ClientLifecycleListener { - protected volatile boolean closed = false; + protected volatile boolean closedByClientClose = false; protected volatile boolean initialized = false; + protected volatile boolean closedByFinalize = false; @Override public void filter(final ClientRequestContext requestContext) throws IOException { /* do nothing */ } @@ -185,11 +186,25 @@ public void onInit() { @Override public synchronized void onClose() { - this.closed = true; + // There is the ClientRuntime created twice, each of which has filter "filterOnClient" registered. + // onClose can be called from Client#close or from ClientRuntime#finalize + if (ClientDestroyTest.isCalledFromFinalizer()) { + this.closedByFinalize = true; + } else { + closedByClientClose = true; + } + } + + public boolean isClosedByClientClose() { + return closedByClientClose; + } + + public boolean isClosedByFinalize() { + return closedByFinalize; } public boolean isClosed() { - return closed; + return closedByClientClose || closedByFinalize; } public boolean isInitialized() { @@ -206,29 +221,39 @@ public void testLifecycleListenerProvider() { final JerseyClientBuilder builder = new JerseyClientBuilder(); final JerseyClient client = builder.build(); - final BarListener filter = new BarListener(); - final BarListener filter2 = new BarListener2(); + final BarListener filterOnClient = new BarListener(); + final BarListener filterOnTarget = new BarListener2(); // ClientRuntime initializes lazily, so it is forced by invoking a (dummy) request - client.register(filter2); // instance registered into client - client.target(getBaseUri()).register(filter).request().get(String.class); // instance registration into target + client.register(filterOnTarget); // instance registered into client + client.target(getBaseUri()).register(filterOnClient).request().get(String.class); // instance registration into target - assertTrue("Filter was expected to be already initialized.", filter.isInitialized()); - assertTrue("Filter2 was expected to be already initialized.", filter2.isInitialized()); + assertTrue("Filter registered on Client was expected to be already initialized.", filterOnClient.isInitialized()); + assertTrue("Filter registered on Target was expected to be already initialized.", filterOnTarget.isInitialized()); client.target(getBaseUri()).register(FooListener.class).request().get(String.class); // class registration into target assertTrue("Class-registered filter was expected to be already initialized", FooListener.isInitialized()); assertFalse("Class-registered filter was expected to be still open.", FooListener.isClosed()); - assertFalse("Filter was expected to be still open.", filter.isClosed()); - assertFalse("Filter2 was expected to be still open.", filter2.isClosed()); + assertFalse("Filter registered on Client was expected to be still open.", filterOnClient.isClosedByClientClose()); + assertFalse("Filter registered on Target was expected to be still open.", filterOnTarget.isClosedByClientClose()); client.close(); assertTrue("Class-registered filter was expected to be closed.", FooListener.isClosed()); - assertTrue("Filter was expected to be closed.", filter.isClosed()); - assertTrue("Filter2 was expected to be closed.", filter2.isClosed()); + assertTrue("Filter registered on Client was expected to be closed.", filterOnClient.isClosed()); + assertTrue("Filter registered on Target was expected to be closed.", filterOnTarget.isClosed()); + } + + private static boolean isCalledFromFinalizer() { + StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); + for (StackTraceElement element : stackTraceElements) { + if ("finalize".equals(element.getMethodName())) { + return true; + } + } + return false; } }