|
1 | 1 | /* |
2 | | - * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
47 | 47 | @Test |
48 | 48 | public class UdpSocket { |
49 | 49 |
|
| 50 | + private static final int MAX_RETRIES = 3; |
| 51 | + |
50 | 52 | /** |
51 | 53 | * Test using the Socket API to send/receive datagrams |
52 | 54 | */ |
@@ -133,16 +135,21 @@ public void testMaxSockets() throws Exception { |
133 | 135 | } |
134 | 136 |
|
135 | 137 |
|
136 | | - private Socket newUdpSocket() throws IOException { |
137 | | - Socket s = null; |
138 | | - |
139 | | - try { |
140 | | - s = new Socket(InetAddress.getLoopbackAddress(), 8000, false); |
141 | | - } catch (BindException unexpected) { |
142 | | - System.out.println("BindException caught retry Socket creation"); |
143 | | - s = new Socket(InetAddress.getLoopbackAddress(), 8000, false); |
| 138 | + private Socket newUdpSocket() throws IOException, InterruptedException { |
| 139 | + BindException unexpected = null; |
| 140 | + for (int i=0; i < MAX_RETRIES; i++) { |
| 141 | + try { |
| 142 | + return new Socket(InetAddress.getLoopbackAddress(), 8000, false); |
| 143 | + } catch (BindException be) { |
| 144 | + unexpected = be; |
| 145 | + if (i != MAX_RETRIES - 1) { |
| 146 | + System.out.printf("BindException caught: retry Socket creation [%s/%s]%n", |
| 147 | + i + 1, MAX_RETRIES); |
| 148 | + Thread.sleep(10 + 10 * i); |
| 149 | + } |
| 150 | + } |
144 | 151 | } |
145 | | - return s; |
| 152 | + throw unexpected; |
146 | 153 | } |
147 | 154 |
|
148 | 155 | private void closeAll(Deque<Socket> sockets) throws IOException { |
|
0 commit comments