17
package com.google.common.testing;
19
import static com.google.common.truth.Truth.assertThat;
20
import static org.junit.Assert.assertThrows;
22
import com.google.common.testing.GcFinalization.FinalizationPredicate;
23
import com.google.common.util.concurrent.SettableFuture;
24
import java.lang.ref.WeakReference;
25
import java.util.WeakHashMap;
26
import java.util.concurrent.CountDownLatch;
27
import java.util.concurrent.atomic.AtomicBoolean;
28
import junit.framework.TestCase;
29
import org.checkerframework.checker.nullness.qual.Nullable;
39
public class GcFinalizationTest extends TestCase {
45
public void testAwait_countDownLatch() {
46
final CountDownLatch latch = new CountDownLatch(1);
49
@SuppressWarnings({"removal", "Finalize"})
51
protected void finalize() {
56
GcFinalization.await(latch);
57
assertEquals(0, latch.getCount());
60
public void testAwaitDone_future() {
61
final SettableFuture<@Nullable Void> future = SettableFuture.create();
64
@SuppressWarnings({"removal", "Finalize"})
66
protected void finalize() {
71
GcFinalization.awaitDone(future);
72
assertTrue(future.isDone());
73
assertFalse(future.isCancelled());
76
public void testAwaitDone_future_cancel() {
77
final SettableFuture<@Nullable Void> future = SettableFuture.create();
80
@SuppressWarnings({"removal", "Finalize"})
82
protected void finalize() {
87
GcFinalization.awaitDone(future);
88
assertTrue(future.isDone());
89
assertTrue(future.isCancelled());
92
public void testAwaitClear() {
93
final WeakReference<Object> ref = new WeakReference<>(new Object());
94
GcFinalization.awaitClear(ref);
95
assertNull(ref.get());
98
public void testAwaitDone_finalizationPredicate() {
99
final WeakHashMap<Object, Object> map = new WeakHashMap<>();
100
map.put(new Object(), Boolean.TRUE);
101
GcFinalization.awaitDone(
102
new FinalizationPredicate() {
104
public boolean isDone() {
105
return map.isEmpty();
108
assertTrue(map.isEmpty());
116
class Interruptenator extends Thread {
117
final AtomicBoolean shutdown;
119
Interruptenator(final Thread interruptee) {
120
this(interruptee, new AtomicBoolean(false));
123
Interruptenator(final Thread interruptee, final AtomicBoolean shutdown) {
128
while (!shutdown.get()) {
129
interruptee.interrupt();
134
this.shutdown = shutdown;
140
while (this.isAlive()) {
146
void assertWrapsInterruptedException(RuntimeException e) {
147
assertThat(e).hasMessageThat().contains("Unexpected interrupt");
148
assertThat(e).hasCauseThat().isInstanceOf(InterruptedException.class);
151
public void testAwait_countDownLatch_interrupted() {
152
Interruptenator interruptenator = new Interruptenator(Thread.currentThread());
154
final CountDownLatch latch = new CountDownLatch(1);
155
RuntimeException expected =
156
assertThrows(RuntimeException.class, () -> GcFinalization.await(latch));
157
assertWrapsInterruptedException(expected);
159
interruptenator.shutdown();
160
Thread.interrupted();
164
public void testAwaitDone_future_interrupted_interrupted() {
165
Interruptenator interruptenator = new Interruptenator(Thread.currentThread());
167
final SettableFuture<@Nullable Void> future = SettableFuture.create();
168
RuntimeException expected =
169
assertThrows(RuntimeException.class, () -> GcFinalization.awaitDone(future));
170
assertWrapsInterruptedException(expected);
172
interruptenator.shutdown();
173
Thread.interrupted();
177
public void testAwaitClear_interrupted() {
178
Interruptenator interruptenator = new Interruptenator(Thread.currentThread());
180
final WeakReference<Object> ref = new WeakReference<Object>(Boolean.TRUE);
181
RuntimeException expected =
182
assertThrows(RuntimeException.class, () -> GcFinalization.awaitClear(ref));
183
assertWrapsInterruptedException(expected);
185
interruptenator.shutdown();
186
Thread.interrupted();
190
public void testAwaitDone_finalizationPredicate_interrupted() {
191
Interruptenator interruptenator = new Interruptenator(Thread.currentThread());
193
RuntimeException expected =
195
RuntimeException.class,
197
GcFinalization.awaitDone(
198
new FinalizationPredicate() {
200
public boolean isDone() {
204
assertWrapsInterruptedException(expected);
206
interruptenator.shutdown();
207
Thread.interrupted();
216
public void testAwaitFullGc() {
217
final CountDownLatch finalizerRan = new CountDownLatch(1);
218
final WeakReference<Object> ref =
219
new WeakReference<Object>(
221
@SuppressWarnings({"removal", "Finalize"})
223
protected void finalize() {
224
finalizerRan.countDown();
230
GcFinalization.awaitFullGc();
235
assertEquals(0, finalizerRan.getCount());
236
assertNull(ref.get());