|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2025 AsyncHttpClient Project. |
| 3 | + */ |
| 4 | +package org.asynchttpclient.netty.timeout; |
| 5 | + |
| 6 | +import org.asynchttpclient.AsyncCompletionHandler; |
| 7 | +import org.asynchttpclient.DefaultAsyncHttpClientConfig; |
| 8 | +import org.asynchttpclient.Request; |
| 9 | +import org.asynchttpclient.RequestBuilder; |
| 10 | +import org.asynchttpclient.channel.ChannelPoolPartitioning; |
| 11 | +import org.asynchttpclient.netty.NettyResponseFuture; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import java.net.InetSocketAddress; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 17 | + |
| 18 | +public class TimeoutTimerTaskTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + public void appendRemoteAddressShouldNotThrowWhenRemoteAddressIsNull() { |
| 22 | + Request request = new RequestBuilder().setUrl("http://example.com:12345").build(); |
| 23 | + NettyResponseFuture<?> future = new NettyResponseFuture<>(request, new AsyncCompletionHandler<Object>() { |
| 24 | + @Override |
| 25 | + public Object onCompleted(org.asynchttpclient.Response response) throws Exception { |
| 26 | + return null; |
| 27 | + } |
| 28 | + }, null, |
| 29 | + 0, ChannelPoolPartitioning.PerHostChannelPoolPartitioning.INSTANCE, null, null); |
| 30 | + |
| 31 | + // create TimeoutsHolder without an original remote address |
| 32 | + TimeoutsHolder timeoutsHolder = new TimeoutsHolder(null, future, null, new DefaultAsyncHttpClientConfig.Builder().build(), null); |
| 33 | + |
| 34 | + TimeoutTimerTask task = new TimeoutTimerTask(future, null, timeoutsHolder) { |
| 35 | + @Override |
| 36 | + public void run(io.netty.util.Timeout timeout) { |
| 37 | + // no-op |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + StringBuilder sb = new StringBuilder(); |
| 42 | + task.appendRemoteAddress(sb); |
| 43 | + |
| 44 | + // fallback should include URI host/port |
| 45 | + assertTrue(sb.toString().contains("example.com:12345"), sb.toString()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void appendRemoteAddressShouldPrintResolvedAddressIfAvailable() { |
| 50 | + Request request = new RequestBuilder().setUrl("http://example.com:12345").build(); |
| 51 | + NettyResponseFuture<?> future = new NettyResponseFuture<>(request, new AsyncCompletionHandler<Object>() { |
| 52 | + @Override |
| 53 | + public Object onCompleted(org.asynchttpclient.Response response) throws Exception { |
| 54 | + return null; |
| 55 | + } |
| 56 | + }, null, |
| 57 | + 0, ChannelPoolPartitioning.PerHostChannelPoolPartitioning.INSTANCE, null, null); |
| 58 | + |
| 59 | + TimeoutsHolder timeoutsHolder = new TimeoutsHolder(null, future, null, new DefaultAsyncHttpClientConfig.Builder().build(), null); |
| 60 | + |
| 61 | + // set a resolved remote address |
| 62 | + timeoutsHolder.setResolvedRemoteAddress(new InetSocketAddress("127.0.0.1", 8080)); |
| 63 | + |
| 64 | + TimeoutTimerTask task = new TimeoutTimerTask(future, null, timeoutsHolder) { |
| 65 | + @Override |
| 66 | + public void run(io.netty.util.Timeout timeout) { |
| 67 | + // no-op |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + StringBuilder sb = new StringBuilder(); |
| 72 | + task.appendRemoteAddress(sb); |
| 73 | + assertTrue(sb.toString().contains(":8080"), sb.toString()); |
| 74 | + } |
| 75 | +} |
0 commit comments