1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 import dalvik.system.VMRuntime;
18 import java.util.concurrent.CountDownLatch;
19 import static java.util.concurrent.TimeUnit.MINUTES;
20 
21 /**
22  * Test a class with a bad finalizer in an environment with a short finalizer timeout.
23  *
24  * This test is inherently flaky. It assumes that the system will schedule the finalizer daemon
25  * and finalizer watchdog daemon enough to reach the timeout and throwing the fatal exception.
26  * Largely cloned from 030-bad-finalizer.
27  */
28 public class Main {
main(String[] args)29     public static void main(String[] args) throws Exception {
30         CountDownLatch finalizerWait = new CountDownLatch(1);
31 
32         System.out.println("Finalizer timeout = "
33                 + VMRuntime.getRuntime().getFinalizerTimeoutMs() + " msecs.");
34         // A separate method to ensure no dex register keeps the object alive.
35         createBadFinalizer(finalizerWait);
36 
37         for (int i = 0; i < 2; i++) {
38             Runtime.getRuntime().gc();
39         }
40 
41         // Now wait for the finalizer to start running.
42         if (!finalizerWait.await(1, MINUTES)) {
43             System.out.println("finalizerWait timed out.");
44         }
45 
46         // Sleep for less time than the default finalizer timeout, but significantly more than
47         // the new timeout plus the 5 seconds we wait to dump thread stacks before actually
48         // exiting.
49         snooze(9800);
50 
51         // We should not get here, since it should only take 5.5 seconds for the timed out
52         // fiinalizer to kill the process.
53         System.out.println("UNREACHABLE");
54         System.exit(0);
55     }
56 
createBadFinalizer(CountDownLatch finalizerWait)57     private static void createBadFinalizer(CountDownLatch finalizerWait) {
58         BadFinalizer bf = new BadFinalizer(finalizerWait);
59 
60         System.out.println("About to null reference.");
61         bf = null;  // Not that this would make a difference, could be eliminated earlier.
62     }
63 
snooze(int ms)64     public static void snooze(int ms) {
65         try {
66             Thread.sleep(ms);
67         } catch (InterruptedException ie) {
68             System.out.println("Snooze(" + ms + ") interrupted");
69         }
70     }
71 
72     /**
73      * Class with a bad finalizer.
74      */
75     public static class BadFinalizer {
76         private CountDownLatch finalizerWait;
77         private volatile int j = 0;  // Volatile in an effort to curb loop optimization.
78 
BadFinalizer(CountDownLatch finalizerWait)79         public BadFinalizer(CountDownLatch finalizerWait) {
80             this.finalizerWait = finalizerWait;
81         }
82 
finalize()83         protected void finalize() {
84             System.out.println("Finalizer started and snoozing...");
85             finalizerWait.countDown();
86             snooze(200);
87             System.out.println("Finalizer done snoozing.");
88 
89             System.out.println("Finalizer sleeping forever now.");
90             while (true) {
91                 snooze(10000);
92             }
93         }
94     }
95 }
96