Example usage for org.apache.lucene.store MockDirectoryWrapper setAllowRandomFileNotFoundException

List of usage examples for org.apache.lucene.store MockDirectoryWrapper setAllowRandomFileNotFoundException

Introduction

In this page you can find the example usage for org.apache.lucene.store MockDirectoryWrapper setAllowRandomFileNotFoundException.

Prototype

public void setAllowRandomFileNotFoundException(boolean value) 

Source Link

Document

If set to true (the default), when we throw random IOException on openInput or createOutput, we may sometimes throw FileNotFoundException or NoSuchFileException.

Usage

From source file:org.elasticsearch.index.engine.InternalEngineTests.java

License:Apache License

/**
 * Random test that throws random exception and ensures all references are
 * counted down / released and resources are closed.
 *//*from   ww  w.  ja v a2 s  . c  om*/
@Test
public void testFailStart() throws IOException {
    // this test fails if any reader, searcher or directory is not closed - MDW FTW
    final int iters = scaledRandomIntBetween(10, 100);
    for (int i = 0; i < iters; i++) {
        MockDirectoryWrapper wrapper = newMockDirectory();
        wrapper.setFailOnOpenInput(randomBoolean());
        wrapper.setAllowRandomFileNotFoundException(randomBoolean());
        wrapper.setRandomIOExceptionRate(randomDouble());
        wrapper.setRandomIOExceptionRateOnOpen(randomDouble());
        final Path translogPath = createTempDir("testFailStart");
        try (Store store = createStore(wrapper)) {
            int refCount = store.refCount();
            assertTrue("refCount: " + store.refCount(), store.refCount() > 0);
            InternalEngine holder;
            try {
                holder = createEngine(store, translogPath);
            } catch (EngineCreationFailureException ex) {
                assertEquals(store.refCount(), refCount);
                continue;
            }
            assertEquals(store.refCount(), refCount + 1);
            final int numStarts = scaledRandomIntBetween(1, 5);
            for (int j = 0; j < numStarts; j++) {
                try {
                    assertEquals(store.refCount(), refCount + 1);
                    holder.close();
                    holder = createEngine(store, translogPath);
                    assertEquals(store.refCount(), refCount + 1);
                } catch (EngineCreationFailureException ex) {
                    // all is fine
                    assertEquals(store.refCount(), refCount);
                    break;
                }
            }
            holder.close();
            assertEquals(store.refCount(), refCount);
        }
    }
}

From source file:org.elasticsearch.index.engine.ShadowEngineTests.java

License:Apache License

/**
 * Random test that throws random exception and ensures all references are
 * counted down / released and resources are closed.
 */// ww w  .  j  a va2  s  . c  o m
@Test
public void testFailStart() throws IOException {
    // Need a commit point for this
    ParsedDocument doc = testParsedDocument("1", "1", "test", null, -1, -1, testDocumentWithTextField(), B_1,
            false);
    primaryEngine.create(new Engine.Create(null, newUid("1"), doc));
    primaryEngine.flush();

    // this test fails if any reader, searcher or directory is not closed - MDW FTW
    final int iters = scaledRandomIntBetween(10, 100);
    for (int i = 0; i < iters; i++) {
        MockDirectoryWrapper wrapper = newMockFSDirectory(dirPath.toFile());
        wrapper.setFailOnOpenInput(randomBoolean());
        wrapper.setAllowRandomFileNotFoundException(randomBoolean());
        wrapper.setRandomIOExceptionRate(randomDouble());
        wrapper.setRandomIOExceptionRateOnOpen(randomDouble());
        try (Store store = createStore(wrapper)) {
            int refCount = store.refCount();
            assertTrue("refCount: " + store.refCount(), store.refCount() > 0);
            Translog translog = createTranslog();
            ShadowEngine holder;
            try {
                holder = createShadowEngine(store, translog);
            } catch (EngineCreationFailureException ex) {
                assertEquals(store.refCount(), refCount);
                continue;
            }
            holder.config().setFailEngineOnCorruption(true);
            assertEquals(store.refCount(), refCount + 1);
            final int numStarts = scaledRandomIntBetween(1, 5);
            for (int j = 0; j < numStarts; j++) {
                try {
                    assertEquals(store.refCount(), refCount + 1);
                    holder.close();
                    holder = createShadowEngine(store, translog);
                    holder.config().setFailEngineOnCorruption(true);
                    assertEquals(store.refCount(), refCount + 1);
                } catch (EngineCreationFailureException ex) {
                    // all is fine
                    assertEquals(store.refCount(), refCount);
                    break;
                }
            }
            translog.close();
            holder.close();
            assertEquals(store.refCount(), refCount);
        }
    }
}