Improve test failure reporting of EpollSocketChannelConfigTest (#11570)
Motivation: This test is inherently flaky due to file descriptor reuse. Even though we have taken steps to make it less flaky, it still fails sometimes. When it does, the error message is not very helpful. Modification: Make use of assertThrows and assertThat to get more descriptive error messages when the tests fail. Result: More meaningful messages on test failures, which may help us make the tests more resilient in the future
This commit is contained in:
parent
beb4588b4a
commit
699549dcbc
@ -26,6 +26,7 @@ import org.junit.jupiter.api.AfterEach;
|
|||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.function.Executable;
|
||||||
import org.opentest4j.TestAbortedException;
|
import org.opentest4j.TestAbortedException;
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
@ -33,8 +34,10 @@ import java.nio.channels.ClosedChannelException;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
@ -140,34 +143,31 @@ public class EpollSocketChannelConfigTest {
|
|||||||
// For this test to pass, we are relying on the sockets file descriptor not being reused after the socket is closed.
|
// For this test to pass, we are relying on the sockets file descriptor not being reused after the socket is closed.
|
||||||
// This is inherently racy, so we allow getSoLinger to throw ChannelException a few of times, but eventually we do
|
// This is inherently racy, so we allow getSoLinger to throw ChannelException a few of times, but eventually we do
|
||||||
// want to see a ClosedChannelException for the test to pass.
|
// want to see a ClosedChannelException for the test to pass.
|
||||||
@RepeatedIfExceptionsTest(repeats = 4, exceptions = ChannelException.class)
|
@RepeatedIfExceptionsTest(repeats = 4)
|
||||||
public void testSetOptionWhenClosed() {
|
public void testSetOptionWhenClosed() {
|
||||||
ch.close().syncUninterruptibly();
|
ch.close().syncUninterruptibly();
|
||||||
try {
|
ChannelException e = assertThrows(ChannelException.class, new Executable() {
|
||||||
ch.config().setSoLinger(0);
|
@Override
|
||||||
fail();
|
public void execute() throws Throwable {
|
||||||
} catch (ChannelException e) {
|
ch.config().setSoLinger(0);
|
||||||
assertTrue(e.getCause() instanceof ClosedChannelException);
|
}
|
||||||
}
|
});
|
||||||
|
assertThat(e).hasCauseInstanceOf(ClosedChannelException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
// For this test to pass, we are relying on the sockets file descriptor not being reused after the socket is closed.
|
// For this test to pass, we are relying on the sockets file descriptor not being reused after the socket is closed.
|
||||||
// This is inherently racy, so we allow getSoLinger to throw ChannelException a few of times, but eventually we do
|
// This is inherently racy, so we allow getSoLinger to throw ChannelException a few of times, but eventually we do
|
||||||
// want to see a ClosedChannelException for the test to pass.
|
// want to see a ClosedChannelException for the test to pass.
|
||||||
@RepeatedIfExceptionsTest(repeats = 4, exceptions = ChannelException.class)
|
@RepeatedIfExceptionsTest(repeats = 4)
|
||||||
public void testGetOptionWhenClosed() {
|
public void testGetOptionWhenClosed() {
|
||||||
ch.close().syncUninterruptibly();
|
ch.close().syncUninterruptibly();
|
||||||
try {
|
ChannelException e = assertThrows(ChannelException.class, new Executable() {
|
||||||
ch.config().getSoLinger();
|
@Override
|
||||||
fail();
|
public void execute() throws Throwable {
|
||||||
} catch (ChannelException e) {
|
ch.config().getSoLinger();
|
||||||
if (!(e.getCause() instanceof ClosedChannelException)) {
|
|
||||||
AssertionError error = new AssertionError(
|
|
||||||
"Expected the suppressed exception to be an instance of ClosedChannelException.");
|
|
||||||
error.addSuppressed(e);
|
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
assertThat(e).hasCauseInstanceOf(ClosedChannelException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user