8281511: java/net/ipv6tests/UdpTest.java fails with checkTime failed

Reviewed-by: dfuchs
This commit is contained in:
Serhiy Sachkov 2025-03-05 16:16:58 +00:00 committed by Mark Sheppard
parent caaf409845
commit ea9e3cfe03
3 changed files with 24 additions and 24 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -36,6 +36,7 @@
import java.net.*; import java.net.*;
import java.io.*; import java.io.*;
import java.util.concurrent.TimeUnit;
public class TcpTest extends Tests { public class TcpTest extends Tests {
static ServerSocket server, server1, server2; static ServerSocket server, server1, server2;
@ -193,13 +194,14 @@ public class TcpTest extends Tests {
server = new ServerSocket (0); server = new ServerSocket (0);
server.setSoTimeout (5000); server.setSoTimeout (5000);
int port = server.getLocalPort(); int port = server.getLocalPort();
long t1 = System.currentTimeMillis(); long t1 = System.nanoTime();
try { try {
server.accept (); server.accept ();
throw new RuntimeException ("accept should not have returned"); throw new RuntimeException ("accept should not have returned");
} catch (SocketTimeoutException e) {} } catch (SocketTimeoutException e) {}
t1 = System.currentTimeMillis() - t1; t1 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t1);
checkTime (t1, 5000); final long expectedTime = TimeUnit.SECONDS.toMillis(5);
checkIfTimeOut(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t1), expectedTime);
c1 = new Socket (); c1 = new Socket ();
c1.connect (new InetSocketAddress (ia4addr, port), 1000); c1.connect (new InetSocketAddress (ia4addr, port), 1000);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -151,19 +151,14 @@ public class Tests {
} }
} }
/* check the time got is within 50% of the time expected */
public static void checkTime (long got, long expected) {
checkTime(got, expected, expected);
}
/* check the time got is between start and end, given 50% tolerance */ /* check the timeout breached lower bound time rule */
public static void checkTime(long got, long start, long end) { public static void checkIfTimeOut(long got, long expected) {
dprintln("checkTime: got = " + got + " start = " + start + " end = " + end); dprintln("checkIfTimeOut: got = " + got + " lower bound = " + expected);
long upper = end + (end / 2);
long lower = start - (start / 2); if (got < expected) {
if (got > upper || got < lower) { throw new RuntimeException("checkIfTimeOut failed: got " + got
throw new RuntimeException("checkTime failed: got " + got + ", expected at least " + expected );
+ ", expected between " + start + " and " + end);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -41,6 +41,7 @@ import java.net.Inet6Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.PortUnreachableException; import java.net.PortUnreachableException;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.util.concurrent.TimeUnit;
public class UdpTest extends Tests { public class UdpTest extends Tests {
static DatagramSocket c3, s1, s2, s3; static DatagramSocket c3, s1, s2, s3;
@ -138,26 +139,27 @@ public class UdpTest extends Tests {
s1 = new DatagramSocket (); s1 = new DatagramSocket ();
s2 = new DatagramSocket (); s2 = new DatagramSocket ();
s1.setSoTimeout (4000); s1.setSoTimeout (4000);
long t1 = System.currentTimeMillis(); long t1 = System.nanoTime();
try { try {
s1.receive (new DatagramPacket (new byte [128], 128)); s1.receive (new DatagramPacket (new byte [128], 128));
throw new Exception ("expected receive timeout "); throw new Exception ("expected receive timeout ");
} catch (SocketTimeoutException e) { } catch (SocketTimeoutException e) {
} }
checkTime (System.currentTimeMillis() - t1, 4000); final long expectedTime = TimeUnit.SECONDS.toMillis(4);
checkIfTimeOut(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t1), expectedTime);
/* check data can be exchanged now */ /* check data can be exchanged now */
simpleDataExchange (s1, ia6addr, s2, ia4addr); simpleDataExchange (s1, ia6addr, s2, ia4addr);
/* double check timeout still works */ /* double check timeout still works */
t1 = System.currentTimeMillis(); t1 = System.nanoTime();
try { try {
s1.receive (new DatagramPacket (new byte [128], 128)); s1.receive (new DatagramPacket (new byte [128], 128));
throw new Exception ("expected receive timeout "); throw new Exception ("expected receive timeout ");
} catch (SocketTimeoutException e) { } catch (SocketTimeoutException e) {
} }
checkTime (System.currentTimeMillis() - t1, 4000); checkIfTimeOut(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t1), expectedTime);
/* check receive works after a delay < timeout */ /* check receive works after a delay < timeout */
@ -174,9 +176,10 @@ public class UdpTest extends Tests {
} catch (Exception e) {} } catch (Exception e) {}
} }
}); });
t1 = System.currentTimeMillis(); t1 = System.nanoTime();
s1.receive (new DatagramPacket (new byte [128], 128)); s1.receive (new DatagramPacket (new byte [128], 128));
checkTime (System.currentTimeMillis() - t1, 2000, 10000); final long startTime = TimeUnit.SECONDS.toMillis(2);
checkIfTimeOut(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t1), startTime);
s1.close (); s1.close ();
s2.close (); s2.close ();
System.out.println ("Test2: OK"); System.out.println ("Test2: OK");