|
| 1 | +/* |
| 2 | + * Copyright 2026 The Netty Project |
| 3 | + * |
| 4 | + * The Netty Project licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package io.netty.testsuite.transport.socket; |
| 17 | + |
| 18 | +import io.netty.bootstrap.Bootstrap; |
| 19 | +import io.netty.buffer.ByteBuf; |
| 20 | +import io.netty.buffer.Unpooled; |
| 21 | +import io.netty.channel.Channel; |
| 22 | +import io.netty.channel.ChannelFuture; |
| 23 | +import io.netty.channel.ChannelFutureListener; |
| 24 | +import io.netty.channel.ChannelHandlerContext; |
| 25 | +import io.netty.channel.ChannelOption; |
| 26 | +import io.netty.channel.SimpleChannelInboundHandler; |
| 27 | +import io.netty.channel.socket.DatagramPacket; |
| 28 | +import io.netty.testsuite.transport.TestsuitePermutation; |
| 29 | +import io.netty.util.CharsetUtil; |
| 30 | +import io.netty.util.NetUtil; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.api.TestInfo; |
| 33 | +import org.junit.jupiter.api.Timeout; |
| 34 | +import org.junit.jupiter.api.condition.DisabledOnOs; |
| 35 | +import org.junit.jupiter.api.condition.OS; |
| 36 | + |
| 37 | +import java.net.InetSocketAddress; |
| 38 | +import java.net.PortUnreachableException; |
| 39 | +import java.util.List; |
| 40 | +import java.util.concurrent.CountDownLatch; |
| 41 | +import java.util.concurrent.TimeUnit; |
| 42 | +import java.util.concurrent.atomic.AtomicReference; |
| 43 | + |
| 44 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 45 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 46 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 47 | + |
| 48 | +public class DatagramConnectedWriteExceptionTest extends AbstractClientSocketTest { |
| 49 | + |
| 50 | + @Override |
| 51 | + protected List<TestsuitePermutation.BootstrapFactory<Bootstrap>> newFactories() { |
| 52 | + return SocketTestPermutation.INSTANCE.datagramSocket(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + @Timeout(value = 10000, unit = TimeUnit.MILLISECONDS) |
| 57 | + @DisabledOnOs(OS.WINDOWS) |
| 58 | + public void testWriteThrowsPortUnreachableException(TestInfo testInfo) throws Throwable { |
| 59 | + run(testInfo, new Runner<Bootstrap>() { |
| 60 | + @Override |
| 61 | + public void run(Bootstrap bootstrap) throws Throwable { |
| 62 | + testWriteExceptionAfterServerStop(bootstrap); |
| 63 | + } |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + protected void testWriteExceptionAfterServerStop(Bootstrap clientBootstrap) throws Throwable { |
| 68 | + final CountDownLatch serverReceivedLatch = new CountDownLatch(1); |
| 69 | + Bootstrap serverBootstrap = clientBootstrap.clone() |
| 70 | + .option(ChannelOption.SO_BROADCAST, false) |
| 71 | + .handler(new SimpleChannelInboundHandler<DatagramPacket>() { |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) { |
| 75 | + serverReceivedLatch.countDown(); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + Channel serverChannel = serverBootstrap.bind(new InetSocketAddress(NetUtil.LOCALHOST, 0)).sync().channel(); |
| 80 | + InetSocketAddress serverAddress = (InetSocketAddress) serverChannel.localAddress(); |
| 81 | + |
| 82 | + clientBootstrap.option(ChannelOption.AUTO_READ, false) |
| 83 | + .handler(new SimpleChannelInboundHandler<DatagramPacket>() { |
| 84 | + |
| 85 | + @Override |
| 86 | + protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) { |
| 87 | + // no-op |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + Channel clientChannel = clientBootstrap.connect(serverAddress).sync().channel(); |
| 92 | + |
| 93 | + final CountDownLatch clientFirstSendLatch = new CountDownLatch(1); |
| 94 | + try { |
| 95 | + ByteBuf firstMessage = Unpooled.wrappedBuffer("First message".getBytes(CharsetUtil.UTF_8)); |
| 96 | + clientChannel.writeAndFlush(firstMessage) |
| 97 | + .addListener(new ChannelFutureListener() { |
| 98 | + @Override |
| 99 | + public void operationComplete(ChannelFuture future) { |
| 100 | + if (future.isSuccess()) { |
| 101 | + clientFirstSendLatch.countDown(); |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + assertTrue(serverReceivedLatch.await(5, TimeUnit.SECONDS), "Server should receive first message"); |
| 107 | + assertTrue(clientFirstSendLatch.await(5, TimeUnit.SECONDS), "Client should send first message"); |
| 108 | + |
| 109 | + serverChannel.close().sync(); |
| 110 | + |
| 111 | + final AtomicReference<Throwable> writeException = new AtomicReference<Throwable>(); |
| 112 | + final CountDownLatch writesCompleteLatch = new CountDownLatch(10); |
| 113 | + |
| 114 | + for (int i = 0; i < 10; i++) { |
| 115 | + ByteBuf message = Unpooled.wrappedBuffer(("Message " + i).getBytes(CharsetUtil.UTF_8)); |
| 116 | + clientChannel.writeAndFlush(message) |
| 117 | + .addListener(new ChannelFutureListener() { |
| 118 | + @Override |
| 119 | + public void operationComplete(ChannelFuture future) { |
| 120 | + if (!future.isSuccess()) { |
| 121 | + writeException.compareAndSet(null, future.cause()); |
| 122 | + } |
| 123 | + writesCompleteLatch.countDown(); |
| 124 | + } |
| 125 | + }); |
| 126 | + Thread.sleep(50); |
| 127 | + } |
| 128 | + |
| 129 | + assertTrue(writesCompleteLatch.await(5, TimeUnit.SECONDS), "All writes should complete"); |
| 130 | + |
| 131 | + assertNotNull(writeException.get(), "Should have captured a write exception"); |
| 132 | + |
| 133 | + assertInstanceOf(PortUnreachableException.class, writeException.get(), "Expected " + |
| 134 | + "PortUnreachableException but got: " + writeException.get().getClass().getName()); |
| 135 | + } finally { |
| 136 | + if (clientChannel != null) { |
| 137 | + clientChannel.close().sync(); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |