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 /** 18 * Exercise Object.wait(), comparing results against wall clock time. 19 */ 20 public class Main { 21 /* delays, in milliseconds */ 22 private final static long[] DELAYS = { 23 200, 500, 1000, 2000, 3500, 8000 24 }; 25 main(String[] args)26 public static void main(String[] args) { 27 boolean timing = (args.length >= 1) && args[0].equals("--timing"); 28 doit(timing); 29 } 30 doit(boolean timing)31 public static void doit(boolean timing) { 32 Object sleepy = new Object(); 33 long start, end; 34 35 synchronized (sleepy) { 36 try { 37 sleepy.wait(-500); 38 System.out.println("HEY: didn't throw on negative arg"); 39 } catch (IllegalArgumentException iae) { 40 System.out.println("Caught expected exception on neg arg"); 41 } catch (InterruptedException ie) { 42 ie.printStackTrace(System.out); 43 } 44 45 long total_extra_delay = 0; 46 for (long delay : DELAYS) { 47 System.out.println("Waiting for " + delay + "ms..."); 48 49 start = System.currentTimeMillis(); 50 try { 51 sleepy.wait(delay); 52 } catch (InterruptedException ie) { 53 ie.printStackTrace(System.out); 54 } 55 end = System.currentTimeMillis(); 56 57 long elapsed = end - start; 58 boolean showTime = timing; 59 60 if (! timing) { 61 // Allow a random scheduling delay of up to 600 msecs. 62 // That value is empirically determined from failure logs. 63 // We seem to get very occasional very long delays on host, perhaps due 64 // to getting paged out. 65 final long epsilon = 600; 66 long min = delay - 1; 67 long max = delay + epsilon; 68 total_extra_delay += elapsed - delay; 69 70 if (elapsed < min) { 71 // This can legitimately happen due to premature wake-ups. 72 // This seems rare and unexpected enough in practice that we should 73 // still report. 74 System.out.println(" Elapsed time was too short"); 75 showTime = true; 76 } else if (elapsed > max) { 77 System.out.println(" Elapsed time was too long: " 78 + "elapsed=" + elapsed + " max=" + max); 79 showTime = true; 80 } 81 } 82 83 if (showTime) { 84 System.out.println(" Wall clock elapsed " 85 + elapsed + "ms"); 86 } 87 } 88 if (total_extra_delay > 1000) { 89 System.out.println(" Total unrequested delay of " + total_extra_delay 90 + "msecs was too long"); 91 } 92 } 93 } 94 } 95