@@ -70,8 +70,6 @@ public abstract class DatagramUnicastTest extends AbstractDatagramTest { |
70 | 70 | private static final byte[] BAD_PREFIX = "BAD-PREFIX-".getBytes(StandardCharsets.UTF_8); |
71 | 71 | private static final byte[] EXPECTED = "EXPECTED-direct-reader-index".getBytes(StandardCharsets.UTF_8); |
72 | 72 | private static final byte[] BYTES = {0, 1, 2, 3}; |
73 | | - // UDP delivery is best-effort even on loopback, so allow a few resends before failing the test. |
74 | | - private static final int SEND_ATTEMPTS = 3; |
75 | 73 | protected enum WrapType { |
76 | 74 | NONE, DUP, SLICE, READ_ONLY |
77 | 75 | } |
@@ -237,23 +235,31 @@ protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throw |
237 | 235 | } |
238 | 236 | }).bind(newSocketAddress()).get(); |
239 | 237 |
|
240 | | - SocketAddress address = server.localAddress(); |
241 | | - DatagramSocket client; |
242 | 238 | try { |
243 | | - client = new DatagramSocket(newSocketAddress()); |
244 | | - } catch (IllegalArgumentException e) { |
245 | | - assumeThat(e.getMessage()).doesNotContainIgnoringCase("unsupported address type"); |
246 | | - throw e; |
247 | | - } |
248 | | - SocketAddress sendAddress = address instanceof InetSocketAddress ? |
249 | | - convertAnyAddress((InetSocketAddress) address) : address; |
250 | | - for (int i = 0; i < 100; i++) { |
| 239 | + SocketAddress address = server.localAddress(); |
| 240 | + DatagramSocket client; |
| 241 | + try { |
| 242 | + client = new DatagramSocket(newSocketAddress()); |
| 243 | + } catch (IllegalArgumentException e) { |
| 244 | + assumeThat(e.getMessage()).doesNotContainIgnoringCase("unsupported address type"); |
| 245 | + throw e; |
| 246 | + } |
251 | 247 | try { |
252 | | - client.send(new java.net.DatagramPacket(EmptyArrays.EMPTY_BYTES, 0, sendAddress)); |
253 | | - } catch (BindException e) { |
254 | | - throw new TestAbortedException("JDK sockets do not support binding to these addresses.", e); |
| 248 | + SocketAddress sendAddress = address instanceof InetSocketAddress ? |
| 249 | + convertAnyAddress((InetSocketAddress) address) : address; |
| 250 | + for (int i = 0; i < 100; i++) { |
| 251 | + try { |
| 252 | + client.send(new java.net.DatagramPacket(EmptyArrays.EMPTY_BYTES, 0, sendAddress)); |
| 253 | + } catch (BindException e) { |
| 254 | + throw new TestAbortedException("JDK sockets do not support binding to these addresses.", e); |
| 255 | + } |
| 256 | + semaphore.acquire(); |
| 257 | + } |
| 258 | + } finally { |
| 259 | + client.close(); |
255 | 260 | } |
256 | | - semaphore.acquire(); |
| 261 | + } finally { |
| 262 | + closeChannel(server); |
257 | 263 | } |
258 | 264 | } |
259 | 265 |
|
@@ -344,22 +350,17 @@ public void channelRead0(ChannelHandlerContext ctx, Object msgs) { |
344 | 350 | SocketAddress localAddr = sc.localAddress(); |
345 | 351 | SocketAddress addr = localAddr instanceof InetSocketAddress ? |
346 | 352 | convertAnyAddress((InetSocketAddress) sc.localAddress()) : localAddr; |
347 | | - boolean received = false; |
348 | | - for (int attempt = 1; attempt <= SEND_ATTEMPTS && !received; attempt++) { |
349 | | - List<Future<Void>> futures = new ArrayList<>(count); |
350 | | - for (int i = 0; i < count; i++) { |
351 | | - futures.add(write(cc, buf, addr, wrapType)); |
352 | | - } |
353 | | - // release as we used buf.retain() before |
354 | | - cc.flush(); |
| 353 | + List<Future<Void>> futures = new ArrayList<>(count); |
| 354 | + for (int i = 0; i < count; i++) { |
| 355 | + futures.add(write(cc, buf, addr, wrapType)); |
| 356 | + } |
| 357 | + // release as we used buf.retain() before |
| 358 | + cc.flush(); |
355 | 359 |
|
356 | | - for (Future<Void> future: futures) { |
357 | | - future.sync(); |
358 | | - } |
359 | | - // UDP datagrams can be lost even on loopback, so retry a few times before giving up. |
360 | | - received = latch.await(10, TimeUnit.SECONDS); |
| 360 | + for (Future<Void> future: futures) { |
| 361 | + future.sync(); |
361 | 362 | } |
362 | | - if (!received) { |
| 363 | + if (!latch.await(10, TimeUnit.SECONDS)) { |
363 | 364 | Throwable error = errorRef.get(); |
364 | 365 | if (error != null) { |
365 | 366 | throw error; |
@@ -404,30 +405,23 @@ private void testSimpleSendWithConnect0(Bootstrap sb, Bootstrap cb, ByteBuf buf, |
404 | 405 | cc.connect(addr).syncUninterruptibly(); |
405 | 406 |
|
406 | 407 | List<Future<Void>> futures = new ArrayList<>(); |
407 | | - boolean received = false; |
408 | | - for (int attempt = 1; attempt <= SEND_ATTEMPTS && !received; attempt++) { |
409 | | - futures.clear(); |
410 | | - for (int i = 0; i < count; i++) { |
411 | | - futures.add(write(cc, buf, wrapType)); |
412 | | - } |
413 | | - cc.flush(); |
414 | | - |
415 | | - for (Future<Void> future: futures) { |
416 | | - future.sync(); |
417 | | - } |
| 408 | + for (int i = 0; i < count; i++) { |
| 409 | + futures.add(write(cc, buf, wrapType)); |
| 410 | + } |
| 411 | + cc.flush(); |
418 | 412 |
|
419 | | - // UDP datagrams (including the echo) can be lost even on loopback, so retry a few times |
420 | | - // before giving up. |
421 | | - received = latch.await(10, TimeUnit.SECONDS) && clientLatch.await(10, TimeUnit.SECONDS); |
| 413 | + for (Future<Void> future: futures) { |
| 414 | + future.sync(); |
422 | 415 | } |
423 | | - if (!received) { |
424 | | - if (latch.getCount() != 0) { |
425 | | - Throwable cause = errorRef.get(); |
426 | | - if (cause != null) { |
427 | | - throw cause; |
428 | | - } |
429 | | - fail(); |
| 416 | + |
| 417 | + if (!latch.await(10, TimeUnit.SECONDS)) { |
| 418 | + Throwable cause = errorRef.get(); |
| 419 | + if (cause != null) { |
| 420 | + throw cause; |
430 | 421 | } |
| 422 | + fail(); |
| 423 | + } |
| 424 | + if (!clientLatch.await(10, TimeUnit.SECONDS)) { |
431 | 425 | Throwable cause = clientErrorRef.get(); |
432 | 426 | if (cause != null) { |
433 | 427 | throw cause; |
|