Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.csp.sentinel.cluster.client.handler;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -106,7 +107,8 @@ private String getRemoteAddress(ChannelHandlerContext ctx) {
return null;
}
InetSocketAddress inetAddress = (InetSocketAddress) ctx.channel().remoteAddress();
return inetAddress.getAddress().getHostAddress() + ":" + inetAddress.getPort();
InetAddress addr = inetAddress.getAddress();
return (addr != null ? addr.getHostAddress() : inetAddress.getHostName()) + ":" + inetAddress.getPort();
}

public int getCurrentState() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.alibaba.csp.sentinel.cluster.client.handler;

import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicInteger;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class TokenClientHandlerTest {

private Channel mockChannel;
private ChannelHandlerContext mockCtx;
private TokenClientHandler handler;

@Before
public void setUp() {
mockChannel = mock(Channel.class);
mockCtx = mock(ChannelHandlerContext.class);
handler = new TokenClientHandler(new AtomicInteger(0), () -> { });
when(mockCtx.channel()).thenReturn(mockChannel);
}

@Test
public void testGetRemoteAddressWithUnresolvedSocketAddress() throws Exception {
InetSocketAddress unresolved = InetSocketAddress.createUnresolved("unresolved.host", 8710);
Assert.assertNull("Unresolved address should have null InetAddress", unresolved.getAddress());
when(mockChannel.remoteAddress()).thenReturn(unresolved);
Method method = TokenClientHandler.class.getDeclaredMethod("getRemoteAddress", ChannelHandlerContext.class);
method.setAccessible(true);
try {
String result = (String) method.invoke(handler, mockCtx);
Assert.assertNotNull("Should return a non-null result for unresolved address", result);
} catch (java.lang.reflect.InvocationTargetException e) {
if (e.getCause() instanceof NullPointerException) {
Assert.fail("getRemoteAddress should handle unresolved InetSocketAddress without NPE, but got: " + e.getCause());
}
throw e;
}
}
}