Skip to content

Commit d0cd3f9

Browse files
committed
HDFS-17828. SocketTimeoutException not recognized in TestWebHdfsTimeouts with Java 24
1 parent 79f37d0 commit d0cd3f9

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,17 @@ public static void assertExceptionContains(String expectedText, Throwable t) {
318318
assertExceptionContains(expectedText, t, "");
319319
}
320320

321+
/**
322+
* Assert that an exception's <code>toString()</code> value
323+
* matches the regex pattern.
324+
* @param pattern regex pattern to match
325+
* @param t thrown exception
326+
* @throws AssertionError if the pattern does not match
327+
*/
328+
public static void assertExceptionMatches(Pattern pattern, Throwable t) {
329+
assertExceptionMatches(pattern, t, "");
330+
}
331+
321332
/**
322333
* Assert that an exception's <code>toString()</code> value
323334
* contained the expected text.
@@ -345,6 +356,33 @@ public static void assertExceptionContains(String expectedText,
345356
}
346357
}
347358

359+
/**
360+
* Assert that an exception's <code>toString()</code> value
361+
* matches the pattern.
362+
* @param pattern regex pattern to match
363+
* @param t thrown exception
364+
* @param message any extra text for the string
365+
* @throws AssertionError if the expected string is not found
366+
*/
367+
public static void assertExceptionMatches(Pattern pattern,
368+
Throwable t,
369+
String message) {
370+
assertNotNull(t, E_NULL_THROWABLE);
371+
String msg = t.toString();
372+
if (msg == null) {
373+
throw new AssertionError(E_NULL_THROWABLE_STRING, t);
374+
}
375+
if (pattern != null && !pattern.matcher(msg).matches()) {
376+
String prefix = org.apache.commons.lang3.StringUtils.isEmpty(message)
377+
? "" : (message + ": ");
378+
throw new AssertionError(
379+
String.format("%s Expected to match '%s' %s: %s",
380+
prefix, pattern, E_UNEXPECTED_EXCEPTION,
381+
StringUtils.stringifyException(t)),
382+
t);
383+
}
384+
}
385+
348386
/**
349387
* Wait for the specified test to return true. The test will be performed
350388
* initially and then every {@code checkEveryMillis} until at least

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/web/TestWebHdfsTimeouts.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@
3737
import java.util.Collection;
3838
import java.util.List;
3939
import java.util.concurrent.TimeoutException;
40+
import java.util.regex.Pattern;
4041

4142
import org.junit.jupiter.params.ParameterizedTest;
4243
import org.junit.jupiter.params.provider.EnumSource;
4344
import org.junit.jupiter.params.provider.MethodSource;
4445
import org.slf4j.Logger;
4546
import org.slf4j.LoggerFactory;
47+
4648
import org.apache.hadoop.conf.Configuration;
4749
import org.apache.hadoop.fs.Path;
4850
import org.apache.hadoop.hdfs.client.HdfsClientConfigKeys;
@@ -152,8 +154,8 @@ public void testConnectTimeout(TimeoutSource src) throws Exception {
152154
fs.listFiles(new Path("/"), false);
153155
fail("expected timeout");
154156
} catch (SocketTimeoutException e) {
155-
GenericTestUtils.assertExceptionContains(fs.getUri().getAuthority()
156-
+ ": connect timed out",e);
157+
GenericTestUtils.assertExceptionMatches(Pattern.compile(
158+
".*" + Pattern.quote(fs.getUri().getAuthority()) + ": [Cc]onnect timed out"), e);
157159
}
158160
}
159161

@@ -190,8 +192,8 @@ public void testAuthUrlConnectTimeout(TimeoutSource src) throws Exception {
190192
fs.getDelegationToken("renewer");
191193
fail("expected timeout");
192194
} catch (SocketTimeoutException e) {
193-
GenericTestUtils.assertExceptionContains(fs.getUri().getAuthority() +
194-
": connect timed out", e);
195+
GenericTestUtils.assertExceptionMatches(Pattern.compile(
196+
".*" + Pattern.quote(fs.getUri().getAuthority()) + ": [Cc]onnect timed out"), e);
195197
}
196198
}
197199

@@ -230,8 +232,8 @@ public void testRedirectConnectTimeout(TimeoutSource src) throws Exception {
230232
fail("expected timeout");
231233
} catch (SocketTimeoutException e) {
232234
assumeBacklogConsumed();
233-
GenericTestUtils.assertExceptionContains(
234-
fs.getUri().getAuthority() + ": connect timed out", e);
235+
GenericTestUtils.assertExceptionMatches(Pattern.compile(
236+
".*" + Pattern.quote(fs.getUri().getAuthority()) + ": [Cc]onnect timed out"), e);
235237
}
236238
}
237239

@@ -272,8 +274,8 @@ public void testTwoStepWriteConnectTimeout(TimeoutSource src) throws Exception {
272274
fail("expected timeout");
273275
} catch (SocketTimeoutException e) {
274276
assumeBacklogConsumed();
275-
GenericTestUtils.assertExceptionContains(
276-
fs.getUri().getAuthority() + ": connect timed out", e);
277+
GenericTestUtils.assertExceptionMatches(Pattern.compile(
278+
".*" + Pattern.quote(fs.getUri().getAuthority()) + ": [Cc]onnect timed out"), e);
277279
} finally {
278280
IOUtils.cleanupWithLogger(LOG, os);
279281
}

0 commit comments

Comments
 (0)