Path: blob/master/test/jdk/com/sun/jndi/dns/ConfigTests/Timeout.java
41155 views
/*1* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.naming.CommunicationException;24import javax.naming.Context;25import javax.naming.directory.InitialDirContext;26import java.net.SocketTimeoutException;27import java.time.Duration;28import java.time.Instant;2930/*31* @test32* @bug 8200151 826530933* @summary Tests that we can set the initial UDP timeout interval and the34* number of retries.35* @library ../lib/36* @modules java.base/sun.security.util37* @run main Timeout38*/3940public class Timeout extends DNSTestBase {41// Host 10.0.0.0 is a bit bucket, used here to simulate a DNS server that42// doesn't respond. 10.0.0.0 server shouldn't be reachable.43// Ping to this address should not give any reply44private static final String HOST = "10.0.0.0";45// Port 9 is a bit bucket, used here to simulate a DNS server that46// doesn't respond.47private static final int PORT = 9;48// initial timeout = 1/4 sec49private static final int TIMEOUT = 250;50// try 5 times per server51private static final int RETRIES = 5;5253private Instant startTime;5455public Timeout() {56setLocalServer(false);57}5859public static void main(String[] args) throws Exception {60new Timeout().run(args);61}6263/*64* Tests that we can set the initial UDP timeout interval and the65* number of retries.66*/67@Override68public void runTest() throws Exception {69String allQuietUrl = "dns://" + HOST + ":" + PORT;70env().put(Context.PROVIDER_URL, allQuietUrl);71env().put("com.sun.jndi.dns.timeout.initial", String.valueOf(TIMEOUT));72env().put("com.sun.jndi.dns.timeout.retries", String.valueOf(RETRIES));73setContext(new InitialDirContext(env()));7475// Any request should fail after timeouts have expired.76startTime = Instant.now();77context().getAttributes("");7879throw new RuntimeException(80"Failed: getAttributes succeeded unexpectedly");81}8283@Override84public boolean handleException(Exception e) {85if (e instanceof CommunicationException) {86Duration elapsedTime = Duration.between(startTime, Instant.now());87if (!(((CommunicationException) e)88.getRootCause() instanceof SocketTimeoutException)) {89return false;90}9192Duration expectedTime = Duration.ofMillis(TIMEOUT)93.multipliedBy((1 << RETRIES) - 1);94DNSTestUtils.debug("Elapsed (ms): " + elapsedTime.toMillis());95DNSTestUtils.debug("Expected (ms): " + expectedTime.toMillis());9697// Check that elapsed time is as long as expected, and98// not more than 50% greater.99if (elapsedTime.compareTo(expectedTime) >= 0 &&100elapsedTime.multipliedBy(2)101.compareTo(expectedTime.multipliedBy(3)) <= 0) {102System.out.println("elapsed time is as long as expected.");103return true;104}105throw new RuntimeException(106"Failed: timeout in " + elapsedTime.toMillis()107+ " ms, expected" + expectedTime.toMillis() + "ms");108}109110return super.handleException(e);111}112}113114115