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
4141import java .net .InetAddress ;
4242import java .net .PortUnreachableException ;
4343import java .net .SocketTimeoutException ;
44+ import java .util .concurrent .TimeUnit ;
4445
4546public 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