test/jdk/com/sun/jndi/dns/ConfigTests/Timeout.java
author xyin
Mon, 22 Oct 2018 13:53:39 +0800
changeset 52205 a562c65c3c74
permissions -rw-r--r--
8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/ Reviewed-by: vtewari, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52205
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     1
/*
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     2
 * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     4
 *
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     8
 *
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    13
 * accompanied this code).
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    14
 *
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    18
 *
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    21
 * questions.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    22
 */
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    23
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    24
import javax.naming.CommunicationException;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    25
import javax.naming.Context;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    26
import javax.naming.directory.InitialDirContext;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    27
import java.net.SocketTimeoutException;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    28
import java.time.Duration;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    29
import java.time.Instant;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    30
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    31
/*
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    32
 * @test
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    33
 * @bug 8200151
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    34
 * @summary Tests that we can set the initial UDP timeout interval and the
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    35
 *          number of retries.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    36
 * @library ../lib/
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    37
 * @modules java.base/sun.security.util
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    38
 * @run main Timeout
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    39
 */
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    40
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    41
public class Timeout extends DNSTestBase {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    42
    // Host 10.0.0.0 is a bit bucket, used here to simulate a DNS server that
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    43
    // doesn't respond. 10.0.0.0 server shouldn't be reachable.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    44
    // Ping to this address should not give any reply
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    45
    private static final String HOST = "10.0.0.0";
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    46
    // Port 9 is a bit bucket, used here to simulate a DNS server that
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    47
    // doesn't respond.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    48
    private static final int PORT = 9;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    49
    // initial timeout = 1/4 sec
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    50
    private static final int TIMEOUT = 250;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    51
    // try 5 times per server
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    52
    private static final int RETRIES = 5;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    53
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    54
    private Instant startTime;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    55
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    56
    public Timeout() {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    57
        setLocalServer(false);
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    58
    }
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    59
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    60
    public static void main(String[] args) throws Exception {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    61
        new Timeout().run(args);
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    62
    }
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    63
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    64
    /*
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    65
     * Tests that we can set the initial UDP timeout interval and the
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    66
     * number of retries.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    67
     */
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    68
    @Override
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    69
    public void runTest() throws Exception {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    70
        String allQuietUrl = "dns://" + HOST + ":" + PORT;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    71
        env().put(Context.PROVIDER_URL, allQuietUrl);
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    72
        env().put("com.sun.jndi.dns.timeout.initial", String.valueOf(TIMEOUT));
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    73
        env().put("com.sun.jndi.dns.timeout.retries", String.valueOf(RETRIES));
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    74
        setContext(new InitialDirContext(env()));
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    75
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    76
        // Any request should fail after timeouts have expired.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    77
        startTime = Instant.now();
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    78
        context().getAttributes("");
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    79
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    80
        throw new RuntimeException(
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    81
                "Failed: getAttributes succeeded unexpectedly");
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    82
    }
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    83
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    84
    @Override
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    85
    public boolean handleException(Exception e) {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    86
        if (e instanceof CommunicationException) {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    87
            Duration elapsedTime = Duration.between(startTime, Instant.now());
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    88
            if (!(((CommunicationException) e)
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    89
                    .getRootCause() instanceof SocketTimeoutException)) {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    90
                return false;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    91
            }
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    92
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    93
            Duration expectedTime = Duration.ofMillis(TIMEOUT)
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    94
                    .multipliedBy((1 << RETRIES) - 1);
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    95
            DNSTestUtils.debug("Elapsed (ms):  " + elapsedTime.toMillis());
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    96
            DNSTestUtils.debug("Expected (ms): " + expectedTime.toMillis());
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    97
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    98
            // Check that elapsed time is as long as expected, and
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
    99
            // not more than 50% greater.
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   100
            if (elapsedTime.compareTo(expectedTime) >= 0 &&
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   101
                    elapsedTime.multipliedBy(2)
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   102
                            .compareTo(expectedTime.multipliedBy(3)) <= 0) {
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   103
                System.out.println("elapsed time is as long as expected.");
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   104
                return true;
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   105
            }
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   106
            throw new RuntimeException(
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   107
                    "Failed: timeout in " + elapsedTime.toMillis()
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   108
                            + " ms, expected" + expectedTime.toMillis() + "ms");
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   109
        }
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   110
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   111
        return super.handleException(e);
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   112
    }
a562c65c3c74 8200151: Add 8 JNDI tests to com/sun/jndi/dns/ConfigTests/
xyin
parents:
diff changeset
   113
}