Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClientDestroyTest fix #4469

Merged
merged 1 commit into from
May 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 */ }
Expand All @@ -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() {
Expand All @@ -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;
}

}