[TEST] prevent stackoverflow in mockInternalEngine

This commit is contained in:
Simon Willnauer 2015-02-18 14:40:22 +01:00
parent 8c3cb6c613
commit 4125e406c7
1 changed files with 15 additions and 5 deletions

View File

@ -39,11 +39,13 @@ import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;
public class MockInternalEngine extends InternalEngine { public class MockInternalEngine extends InternalEngine {
public static final String WRAP_READER_RATIO = "index.engine.mock.random.wrap_reader_ratio"; public static final String WRAP_READER_RATIO = "index.engine.mock.random.wrap_reader_ratio";
public static final String READER_WRAPPER_TYPE = "index.engine.mock.random.wrapper"; public static final String READER_WRAPPER_TYPE = "index.engine.mock.random.wrapper";
public static final String FLUSH_ON_CLOSE_RATIO = "index.engine.mock.flush_on_close.ratio"; public static final String FLUSH_ON_CLOSE_RATIO = "index.engine.mock.flush_on_close.ratio";
private final AtomicBoolean closing = new AtomicBoolean(false);
public static class MockContext { public static class MockContext {
public final Random random; public final Random random;
@ -82,11 +84,15 @@ public class MockInternalEngine extends InternalEngine {
@Override @Override
public void close() throws IOException { public void close() throws IOException {
try { try {
if (closing.compareAndSet(false, true)) { // only do the random thing if we are the first call to this since super.flushOnClose() calls #close() again and then we might end up with a stackoverflow.
if (mockContext.flushOnClose > mockContext.random.nextDouble()) { if (mockContext.flushOnClose > mockContext.random.nextDouble()) {
super.flushAndClose(); super.flushAndClose();
} else { } else {
super.close(); super.close();
} }
} else {
super.close();
}
} finally { } finally {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
// log debug if we have pending searchers // log debug if we have pending searchers
@ -100,11 +106,15 @@ public class MockInternalEngine extends InternalEngine {
@Override @Override
public void flushAndClose() throws IOException { public void flushAndClose() throws IOException {
if (closing.compareAndSet(false, true)) { // only do the random thing if we are the first call to this since super.flushOnClose() calls #close() again and then we might end up with a stackoverflow.
if (mockContext.flushOnClose > mockContext.random.nextDouble()) { if (mockContext.flushOnClose > mockContext.random.nextDouble()) {
super.flushAndClose(); super.flushAndClose();
} else { } else {
super.close(); super.close();
} }
} else {
super.flushAndClose();
}
} }
@Override @Override