jdk/test/java/net/Socket/AccurateTimeout.java
changeset 6286 67584b95a0f0
parent 6285 96a57de47def
parent 6166 1ce7938efb03
child 6287 62af7bc0a66a
equal deleted inserted replaced
6285:96a57de47def 6286:67584b95a0f0
     1 /*
       
     2  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25  * @bug 4512028
       
    26  * @summary Check the tolerance on read timeouts.
       
    27  */
       
    28 import java.net.*;
       
    29 import java.io.*;
       
    30 
       
    31 public class AccurateTimeout {
       
    32 
       
    33     static final int TOLERANCE = 100;
       
    34 
       
    35     static boolean skipTest() {
       
    36         String os = System.getProperty("os.name");
       
    37         if (os.equals("Windows 95") ||
       
    38             os.equals("Windows 98") ||
       
    39             os.equals("Windows Me")) {
       
    40 
       
    41             System.out.println("Due to an OS bug timeout tolerance cannot be tested on this OS");
       
    42             return true;
       
    43         }
       
    44         return false;
       
    45     }
       
    46 
       
    47     public static void main(String args[]) throws Exception {
       
    48 
       
    49         if (skipTest()) {
       
    50             return;
       
    51         }
       
    52 
       
    53         int failures = 0;
       
    54         int timeout;
       
    55 
       
    56         System.out.println("");
       
    57         System.out.println("Testing Socket.getInputStream().read() ...");
       
    58         System.out.println("");
       
    59 
       
    60         ServerSocket ss = new ServerSocket(0);
       
    61         Socket s1 = new Socket(InetAddress.getLocalHost(), ss.getLocalPort());
       
    62         Socket s2 = ss.accept();
       
    63 
       
    64         InputStream in = s1.getInputStream();
       
    65 
       
    66         timeout = 100;
       
    67         while (timeout < 2500) {
       
    68             s1.setSoTimeout(timeout);
       
    69 
       
    70             long startTime = System.currentTimeMillis();
       
    71             try {
       
    72                 in.read();
       
    73             } catch (SocketTimeoutException e) {
       
    74             }
       
    75             long actual = System.currentTimeMillis() - startTime;
       
    76 
       
    77             System.out.print("excepted: " + timeout + " actual: " + actual);
       
    78 
       
    79             if (Math.abs(actual-timeout) > TOLERANCE) {
       
    80                 System.out.print(" *** FAIL: outside tolerance");
       
    81                 failures++;
       
    82             } else {
       
    83                 System.out.print(" PASS.");
       
    84             }
       
    85 
       
    86             System.out.println("");
       
    87             timeout += 200;
       
    88         }
       
    89 
       
    90         s1.close();
       
    91         s2.close();
       
    92         ss.close();
       
    93 
       
    94 
       
    95         // ----------
       
    96 
       
    97 
       
    98         System.out.println("");
       
    99         System.out.println("Testing DatagramSocket.receive ...");
       
   100         System.out.println("");
       
   101 
       
   102         byte b[] = new byte[8];
       
   103         DatagramPacket p = new DatagramPacket(b, b.length);
       
   104 
       
   105         DatagramSocket ds = new DatagramSocket();
       
   106 
       
   107         timeout = 100;
       
   108         while (timeout < 2500) {
       
   109             ds.setSoTimeout(timeout);
       
   110 
       
   111             long startTime = System.currentTimeMillis();
       
   112             try {
       
   113                 ds.receive(p);
       
   114             } catch (SocketTimeoutException e) {
       
   115             }
       
   116             long actual = System.currentTimeMillis() - startTime;
       
   117 
       
   118             System.out.print("excepted: " + timeout + " actual: " + actual);
       
   119 
       
   120             if (Math.abs(actual-timeout) > TOLERANCE) {
       
   121                 System.out.print(" *** FAIL: outside tolerance");
       
   122                 failures++;
       
   123             } else {
       
   124                 System.out.print(" PASS.");
       
   125             }
       
   126 
       
   127             System.out.println("");
       
   128             timeout += 200;
       
   129         }
       
   130 
       
   131         ds.close();
       
   132 
       
   133         System.out.println("");
       
   134 
       
   135         // ---------
       
   136 
       
   137         if (failures > 0) {
       
   138             throw new Exception("Test failed: " + failures +
       
   139                 " test(s) outside tolerance");
       
   140         }
       
   141 
       
   142     }
       
   143 
       
   144 }