Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing: Ensure RepeatOnException rule is available in test-jar #9675

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,31 +16,38 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.transport.netty;
package org.elasticsearch.test.junit.rule;

import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.transport.BindTransportException;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

/**
* A helper rule to catch all BindTransportExceptions
* and rerun the test for a configured number of times
*
* Note: Be aware, that when a test is repeated, the @After and @Before
* annotated methods are not run a second time
*
*/
public class RepeatOnBindExceptionRule implements TestRule {
public class RepeatOnExceptionRule implements TestRule {

private ESLogger logger;
private int retryCount;
private Class expectedException;

/**
*
* @param logger the es logger from the test class
* @param retryCount number of amounts to try a single test before failing
* @param expectedException The exception class you want to catch
*
*/
public RepeatOnBindExceptionRule(ESLogger logger, int retryCount) {
public RepeatOnExceptionRule(ESLogger logger, int retryCount, Class expectedException) {
this.logger = logger;
this.retryCount = retryCount;
this.expectedException = expectedException;
}

@Override
Expand All @@ -55,9 +62,11 @@ public void evaluate() throws Throwable {
try {
base.evaluate();
return;
} catch (BindTransportException t) {
caughtThrowable = t;
logger.info("Bind exception occurred, rerunning the test after [{}] failures", t, i+1);
} catch (Throwable t) {
if (t.getClass().equals(expectedException)) {
caughtThrowable = t;
logger.info("Exception [{}] occurred, rerunning the test after [{}] failures", t, t.getClass().getSimpleName(), i+1);
}
}
}
logger.error("Giving up after [{}] failures... marking test as failed", retryCount);
Expand Down
Expand Up @@ -30,9 +30,10 @@
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.elasticsearch.test.junit.rule.RepeatOnExceptionRule;
import org.elasticsearch.test.cache.recycler.MockBigArrays;
import org.elasticsearch.threadpool.ThreadPool;
import org.junit.After;
import org.elasticsearch.transport.BindTransportException;
import org.junit.Rule;
import org.junit.Test;

Expand All @@ -47,23 +48,10 @@

public class NettyTransportMultiPortTests extends ElasticsearchTestCase {

public static int MAX_RETRIES = 10;
private static final int MAX_RETRIES = 10;

@Rule
public RepeatOnBindExceptionRule repeatOnBindExceptionRule = new RepeatOnBindExceptionRule(logger, MAX_RETRIES);

private NettyTransport nettyTransport;
private ThreadPool threadPool;

@After
public void shutdownNettyTransport() {
if (nettyTransport != null) {
nettyTransport.stop();
}
if (threadPool != null) {
threadPool.shutdownNow();
}
}
public RepeatOnExceptionRule repeatOnBindExceptionRule = new RepeatOnExceptionRule(logger, MAX_RETRIES, BindTransportException.class);

@Test
public void testThatNettyCanBindToMultiplePorts() throws Exception {
Expand All @@ -76,11 +64,14 @@ public void testThatNettyCanBindToMultiplePorts() throws Exception {
.put("transport.profiles.client1.port", ports[2])
.build();

startNettyTransport(settings);

assertConnectionRefused(ports[0]);
assertPortIsBound(ports[1]);
assertPortIsBound(ports[2]);
ThreadPool threadPool = new ThreadPool("tst");
try (NettyTransport ignored = startNettyTransport(settings, threadPool)) {
assertConnectionRefused(ports[0]);
assertPortIsBound(ports[1]);
assertPortIsBound(ports[2]);
} finally {
threadPool.shutdownNow();
}
}

@Test
Expand All @@ -93,10 +84,13 @@ public void testThatDefaultProfileInheritsFromStandardSettings() throws Exceptio
.put("transport.profiles.client1.port", ports[1])
.build();

startNettyTransport(settings);

assertPortIsBound(ports[0]);
assertPortIsBound(ports[1]);
ThreadPool threadPool = new ThreadPool("tst");
try (NettyTransport ignored = startNettyTransport(settings, threadPool)) {
assertPortIsBound(ports[0]);
assertPortIsBound(ports[1]);
} finally {
threadPool.shutdownNow();
}
}

@Test
Expand All @@ -109,9 +103,12 @@ public void testThatProfileWithoutPortSettingsFails() throws Exception {
.put("transport.profiles.client1.whatever", "foo")
.build();

startNettyTransport(settings);

assertPortIsBound(ports[0]);
ThreadPool threadPool = new ThreadPool("tst");
try (NettyTransport ignored = startNettyTransport(settings, threadPool)) {
assertPortIsBound(ports[0]);
} finally {
threadPool.shutdownNow();
}
}

@Test
Expand All @@ -125,11 +122,14 @@ public void testThatDefaultProfilePortOverridesGeneralConfiguration() throws Exc
.put("transport.profiles.default.port", ports[2])
.build();

startNettyTransport(settings);

assertConnectionRefused(ports[0]);
assertConnectionRefused(ports[1]);
assertPortIsBound(ports[2]);
ThreadPool threadPool = new ThreadPool("tst");
try (NettyTransport ignored = startNettyTransport(settings, threadPool)) {
assertConnectionRefused(ports[0]);
assertConnectionRefused(ports[1]);
assertPortIsBound(ports[2]);
} finally {
threadPool.shutdownNow();
}
}

@Test
Expand All @@ -145,11 +145,14 @@ public void testThatBindingOnDifferentHostsWorks() throws Exception {
.put("transport.profiles.client1.port", ports[1])
.build();

startNettyTransport(settings);

assertPortIsBound("127.0.0.1", ports[0]);
assertPortIsBound(firstNonLoopbackAddress.getHostAddress(), ports[1]);
assertConnectionRefused(ports[1]);
ThreadPool threadPool = new ThreadPool("tst");
try (NettyTransport ignored = startNettyTransport(settings, threadPool)) {
assertPortIsBound("127.0.0.1", ports[0]);
assertPortIsBound(firstNonLoopbackAddress.getHostAddress(), ports[1]);
assertConnectionRefused(ports[1]);
} finally {
threadPool.shutdownNow();
}
}

private int[] getRandomPorts(int numberOfPorts) {
Expand All @@ -166,14 +169,14 @@ private int[] getRandomPorts(int numberOfPorts) {
return ports.toArray();
}

private void startNettyTransport(Settings settings) {
threadPool = new ThreadPool("tst");
private NettyTransport startNettyTransport(Settings settings, ThreadPool threadPool) {
BigArrays bigArrays = new MockBigArrays(settings, new PageCacheRecycler(settings, threadPool), new NoneCircuitBreakerService());

nettyTransport = new NettyTransport(settings, threadPool, new NetworkService(settings), bigArrays, Version.CURRENT);
NettyTransport nettyTransport = new NettyTransport(settings, threadPool, new NetworkService(settings), bigArrays, Version.CURRENT);
nettyTransport.start();

assertThat(nettyTransport.lifecycleState(), is(Lifecycle.State.STARTED));
return nettyTransport;
}

private void assertConnectionRefused(int port) throws Exception {
Expand Down