3939 */
4040package 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 ;
4247import java .lang .reflect .Field ;
4348import java .util .Collection ;
4449
4550import javax .ws .rs .client .Client ;
4651import javax .ws .rs .client .ClientBuilder ;
47- import javax .ws .rs .client .Invocation .Builder ;
4852import javax .ws .rs .client .WebTarget ;
4953
5054import 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 *
6261public 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