Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 3e9fccb

Browse files
committed
JERSEY-2291: Another attempt at fixing the ShutdownHookLeakTest.
1 parent d56d37a commit 3e9fccb

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

core-client/src/test/java/org/glassfish/jersey/client/ShutdownHookLeakTest.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,20 @@
3939
*/
4040
package org.glassfish.jersey.client;
4141

42+
import static org.hamcrest.CoreMatchers.is;
43+
import static org.hamcrest.number.OrderingComparison.lessThan;
44+
import static org.junit.Assert.assertThat;
45+
46+
import java.lang.ref.WeakReference;
4247
import java.lang.reflect.Field;
4348
import java.util.Collection;
4449

4550
import javax.ws.rs.client.Client;
4651
import javax.ws.rs.client.ClientBuilder;
47-
import javax.ws.rs.client.Invocation.Builder;
4852
import javax.ws.rs.client.WebTarget;
4953

5054
import org.junit.Test;
5155

52-
import static org.hamcrest.CoreMatchers.equalTo;
53-
import static org.hamcrest.CoreMatchers.is;
54-
import static org.hamcrest.number.OrderingComparison.lessThan;
55-
import static org.junit.Assert.assertThat;
56-
5756
/**
5857
* Reproducer for JERSEY-2786.
5958
*
@@ -62,6 +61,7 @@
6261
public class ShutdownHookLeakTest {
6362

6463
private static final int ITERATIONS = 4000;
64+
private static final int THRESHOLD = ITERATIONS * 2 / 3;
6565

6666
@Test
6767
public void testShutdownHookDoesNotLeak() throws Exception {
@@ -79,10 +79,28 @@ public void testShutdownHookDoesNotLeak() throws Exception {
7979
.property("Irving", "Washington");
8080
}
8181

82+
System.gc();
83+
84+
int notEnqueued = 0;
85+
int notNull = 0;
86+
for (Object o : shutdownHooks) {
87+
if (((WeakReference<JerseyClient.ShutdownHook>) o).get() != null) {
88+
notNull++;
89+
}
90+
if (!((WeakReference<JerseyClient.ShutdownHook>) o).isEnqueued()) {
91+
notEnqueued++;
92+
}
93+
}
94+
95+
assertThat(
96+
"Non-null shutdown hook references count should not copy number of property invocation",
97+
// 66 % seems like a reasonable threshold for this test to keep it stable
98+
notNull, is(lessThan(THRESHOLD)));
99+
82100
assertThat(
83-
"shutdown hook deque size should not copy number of property invocation",
101+
"Shutdown hook references count not enqueued in the ReferenceQueue should not copy number of property invocation",
84102
// 66 % seems like a reasonable threshold for this test to keep it stable
85-
shutdownHooks.size(), is(lessThan(ITERATIONS * 2 / 3)));
103+
notEnqueued, is(lessThan(THRESHOLD)));
86104
}
87105

88106
private Collection getShutdownHooks(final Client client) throws NoSuchFieldException, IllegalAccessException {

0 commit comments

Comments
 (0)