• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 soon and often enough to reach the timeout and throw the fatal
26  * exception before we time out here.
27  * Largely cloned from 030-bad-finalizer.
28  */
29 public class Main {
main(String[] args)30     public static void main(String[] args) throws Exception {
31         CountDownLatch finalizerWait = new CountDownLatch(1);
32 
33         System.out.println("Finalizer timeout = "
34                 + VMRuntime.getRuntime().getFinalizerTimeoutMs() + " msecs.");
35         // A separate method to ensure no dex register keeps the object alive.
36         createBadFinalizer(finalizerWait);
37 
38         for (int i = 0; i < 2; i++) {
39             Runtime.getRuntime().gc();
40         }
41 
42         // Now wait for the finalizer to start running.
43         if (!finalizerWait.await(1, MINUTES)) {
44             System.out.println("finalizerWait timed out.");
45         }
46 
47         // Sleep for less time than the default finalizer timeout, but significantly more than
48         // the new timeout plus the 5 seconds we wait to dump thread stacks before actually
49         // exiting.
50         snooze(9800);
51         if (System.getenv("ART_TEST_ON_VM") != null) {
52           // Under emulation we sometimes seem to just not shut down fast enough.  If the process is
53           // still running at this point, sleep again for longer. This makes the test much less
54           // useful, but not 100% useless.
55           snooze(20_000);
56         }
57 
58         // We should not get here, since it should only take 5.5 seconds for the timed out
59         // finalizer to kill the process.
60         System.out.println("UNREACHABLE");
61         System.exit(0);
62     }
63 
createBadFinalizer(CountDownLatch finalizerWait)64     private static void createBadFinalizer(CountDownLatch finalizerWait) {
65         BadFinalizer bf = new BadFinalizer(finalizerWait);
66 
67         System.out.println("About to null reference.");
68         bf = null;  // Not that this would make a difference, could be eliminated earlier.
69     }
70 
snooze(int ms)71     public static void snooze(int ms) {
72         try {
73             Thread.sleep(ms);
74         } catch (InterruptedException ie) {
75             System.out.println("Snooze(" + ms + ") interrupted");
76         }
77     }
78 
79     /**
80      * Class with a bad finalizer.
81      */
82     public static class BadFinalizer {
83         private CountDownLatch finalizerWait;
84         private volatile int j = 0;  // Volatile in an effort to curb loop optimization.
85 
BadFinalizer(CountDownLatch finalizerWait)86         public BadFinalizer(CountDownLatch finalizerWait) {
87             this.finalizerWait = finalizerWait;
88         }
89 
finalize()90         protected void finalize() {
91             System.out.println("Finalizer started and snoozing...");
92             finalizerWait.countDown();
93             snooze(200);
94             System.out.println("Finalizer done snoozing.");
95 
96             System.out.println("Finalizer sleeping forever now.");
97             while (true) {
98                 snooze(10000);
99             }
100         }
101     }
102 }
103