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
10 changes: 9 additions & 1 deletion core/src/main/java/io/grpc/internal/ProxyDetectorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,15 @@ private ProxiedSocketAddress detectProxy(InetSocketAddress targetAddr) throws IO
return null;
}

List<Proxy> proxies = proxySelector.select(uri);
List<Proxy> proxies;
try {
proxies = proxySelector.select(uri);
} catch (IllegalArgumentException e) {
throw new IOException(
"ProxySelector " + proxySelector.getClass().getName()
+ " threw an exception while selecting a proxy",
e);
}
// ProxySelector.select(URI) is contractually required to return a non-null, non-empty list.
// Surface the offending implementation's class name so a broken ProxySelector can be fixed.
if (proxies == null || proxies.isEmpty()) {
Expand Down
15 changes: 15 additions & 0 deletions core/src/test/java/io/grpc/internal/ProxyDetectorImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,19 @@ public void throwsWhenProxySelectorReturnsNullList() throws Exception {
assertTrue(e.getMessage(), e.getMessage().contains("null"));
assertTrue(e.getMessage(), e.getMessage().contains(proxySelector.getClass().getName()));
}

@Test
public void throwsWhenProxySelectorThrowsIllegalArgumentException() throws Exception {
IllegalArgumentException cause =
new IllegalArgumentException("port out of range: 899858473");
when(proxySelector.select(any(URI.class))).thenThrow(cause);

IOException exception =
assertThrows(IOException.class, () -> proxyDetector.proxyFor(destination));

assertEquals(cause, exception.getCause());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: assertSame for the cause.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking a close look. This is helpful. I agree that a resolver level test would better cover the actual failure path, and I will change the cause check to assertSame.

Before I rework the implementation, could one of the maintainers confirm the preferred behavior? Should this fall back to a direct connection, or return UNAVAILABLE? Also, should the broader RuntimeException handling be placed around detectProxy() in DnsNameResolver?

Once that is confirmed, I will update the implementation.

assertTrue(
exception.getMessage(),
exception.getMessage().contains(proxySelector.getClass().getName()));
}
}
Loading