Skip to content
Permalink
Browse files
Merge branch 'fix_gc_bug_in_test' into 'ibm-trunk'
Fix gc bug in test.

See merge request !62
  • Loading branch information
joe-chacko committed Nov 11, 2015
2 parents e4ca6e1 + aef2d86 commit 788192ad6f53874c0e424b6975dd5c3b650bf7da
Showing 1 changed file with 5 additions and 9 deletions.
@@ -23,6 +23,7 @@ public class WeakConcurrentFifoTest extends ConcurrentFifoTest {
// must not use the @Mock annotation and Mockito's injection
// because it intermittently fails to count invocations correctly
private Runnable cleanup;
private ReferenceQueue<?> referenceQueue = new ReferenceQueue<>();

@Before
@Override
@@ -88,29 +89,24 @@ private WeakReference[] enqueueStringsCollectably(String... strings) {
WeakReference[] refs = new WeakReference[strings.length];
for (int i = 0 ; i < strings.length; i++) {
String s = new String(strings[i]);
refs[i] = new WeakReference(s);
refs[i] = new WeakReference(s, referenceQueue);
fifo.put(s);
}
return refs;
}

public static void gcUntilCleared(WeakReference<?>... refs) throws Exception {
public void gcUntilCleared(WeakReference<?>... refs) throws Exception {
for (WeakReference<?> ref : refs) {
gcUntilCollected(ref);
System.out.println();
referenceQueue.remove();
}
// now use a dummy object and a new ref queue to wait for enqueuing to happen
// (hopefully once the dummy ref is enqueued on the new ref q, the enqueueing inside the fifo has happened)
ReferenceQueue<String> q = new ReferenceQueue<String>();
WeakReference<String> r = new WeakReference<String>(new String("Hello"), q);
gcUntilCollected(r);
q.remove();
}

private static void gcUntilCollected(WeakReference<?> ref) {
while (ref.get() != null) {
System.out.print("gc ");
System.gc();
}
System.out.println();
}
}

0 comments on commit 788192a

Please sign in to comment.