Skip to content

Commit ea9e3cf

Browse files
serhiysachkovMark Sheppard
authored andcommitted
8281511: java/net/ipv6tests/UdpTest.java fails with checkTime failed
Reviewed-by: dfuchs
1 parent caaf409 commit ea9e3cf

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

test/jdk/java/net/ipv6tests/TcpTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -36,6 +36,7 @@
3636

3737
import java.net.*;
3838
import java.io.*;
39+
import java.util.concurrent.TimeUnit;
3940

4041
public class TcpTest extends Tests {
4142
static ServerSocket server, server1, server2;
@@ -193,13 +194,14 @@ static void test3 () throws Exception {
193194
server = new ServerSocket (0);
194195
server.setSoTimeout (5000);
195196
int port = server.getLocalPort();
196-
long t1 = System.currentTimeMillis();
197+
long t1 = System.nanoTime();
197198
try {
198199
server.accept ();
199200
throw new RuntimeException ("accept should not have returned");
200201
} catch (SocketTimeoutException e) {}
201-
t1 = System.currentTimeMillis() - t1;
202-
checkTime (t1, 5000);
202+
t1 = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t1);
203+
final long expectedTime = TimeUnit.SECONDS.toMillis(5);
204+
checkIfTimeOut(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t1), expectedTime);
203205

204206
c1 = new Socket ();
205207
c1.connect (new InetSocketAddress (ia4addr, port), 1000);

test/jdk/java/net/ipv6tests/Tests.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -151,19 +151,14 @@ public static void comparePackets (DatagramPacket p1, DatagramPacket p2)
151151
}
152152
}
153153

154-
/* check the time got is within 50% of the time expected */
155-
public static void checkTime (long got, long expected) {
156-
checkTime(got, expected, expected);
157-
}
158154

159-
/* check the time got is between start and end, given 50% tolerance */
160-
public static void checkTime(long got, long start, long end) {
161-
dprintln("checkTime: got = " + got + " start = " + start + " end = " + end);
162-
long upper = end + (end / 2);
163-
long lower = start - (start / 2);
164-
if (got > upper || got < lower) {
165-
throw new RuntimeException("checkTime failed: got " + got
166-
+ ", expected between " + start + " and " + end);
155+
/* check the timeout breached lower bound time rule */
156+
public static void checkIfTimeOut(long got, long expected) {
157+
dprintln("checkIfTimeOut: got = " + got + " lower bound = " + expected);
158+
159+
if (got < expected) {
160+
throw new RuntimeException("checkIfTimeOut failed: got " + got
161+
+ ", expected at least " + expected );
167162
}
168163
}
169164

test/jdk/java/net/ipv6tests/UdpTest.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -41,6 +41,7 @@
4141
import java.net.InetAddress;
4242
import java.net.PortUnreachableException;
4343
import java.net.SocketTimeoutException;
44+
import java.util.concurrent.TimeUnit;
4445

4546
public class UdpTest extends Tests {
4647
static DatagramSocket c3, s1, s2, s3;
@@ -138,26 +139,27 @@ static void test2 () throws Exception {
138139
s1 = new DatagramSocket ();
139140
s2 = new DatagramSocket ();
140141
s1.setSoTimeout (4000);
141-
long t1 = System.currentTimeMillis();
142+
long t1 = System.nanoTime();
142143
try {
143144
s1.receive (new DatagramPacket (new byte [128], 128));
144145
throw new Exception ("expected receive timeout ");
145146
} catch (SocketTimeoutException e) {
146147
}
147-
checkTime (System.currentTimeMillis() - t1, 4000);
148+
final long expectedTime = TimeUnit.SECONDS.toMillis(4);
149+
checkIfTimeOut(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t1), expectedTime);
148150

149151
/* check data can be exchanged now */
150152

151153
simpleDataExchange (s1, ia6addr, s2, ia4addr);
152154

153155
/* double check timeout still works */
154-
t1 = System.currentTimeMillis();
156+
t1 = System.nanoTime();
155157
try {
156158
s1.receive (new DatagramPacket (new byte [128], 128));
157159
throw new Exception ("expected receive timeout ");
158160
} catch (SocketTimeoutException e) {
159161
}
160-
checkTime (System.currentTimeMillis() - t1, 4000);
162+
checkIfTimeOut(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t1), expectedTime);
161163

162164
/* check receive works after a delay < timeout */
163165

@@ -174,9 +176,10 @@ public void run () {
174176
} catch (Exception e) {}
175177
}
176178
});
177-
t1 = System.currentTimeMillis();
179+
t1 = System.nanoTime();
178180
s1.receive (new DatagramPacket (new byte [128], 128));
179-
checkTime (System.currentTimeMillis() - t1, 2000, 10000);
181+
final long startTime = TimeUnit.SECONDS.toMillis(2);
182+
checkIfTimeOut(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t1), startTime);
180183
s1.close ();
181184
s2.close ();
182185
System.out.println ("Test2: OK");

0 commit comments

Comments
 (0)