mirror of https://github.com/apache/lucene.git
better failures from MDW if you have unclosed indexwriter, even if you setLockFactory
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1373204 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8a5bab3e1d
commit
e1ec1e7932
|
@ -77,9 +77,9 @@ public class TestLockFactory extends LuceneTestCase {
|
||||||
// exceptions raised:
|
// exceptions raised:
|
||||||
// Verify: NoLockFactory allows two IndexWriters
|
// Verify: NoLockFactory allows two IndexWriters
|
||||||
public void testRAMDirectoryNoLocking() throws IOException {
|
public void testRAMDirectoryNoLocking() throws IOException {
|
||||||
Directory dir = new MockDirectoryWrapper(random(), new RAMDirectory());
|
MockDirectoryWrapper dir = new MockDirectoryWrapper(random(), new RAMDirectory());
|
||||||
dir.setLockFactory(NoLockFactory.getNoLockFactory());
|
dir.setLockFactory(NoLockFactory.getNoLockFactory());
|
||||||
|
dir.setWrapLockFactory(false); // we are gonna explicitly test we get this back
|
||||||
assertTrue("RAMDirectory.setLockFactory did not take",
|
assertTrue("RAMDirectory.setLockFactory did not take",
|
||||||
NoLockFactory.class.isInstance(dir.getLockFactory()));
|
NoLockFactory.class.isInstance(dir.getLockFactory()));
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,13 @@ package org.apache.lucene.util.junitcompat;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.lucene.index.IndexWriter;
|
||||||
|
import org.apache.lucene.index.IndexWriterConfig;
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
|
import org.apache.lucene.store.MockDirectoryWrapper;
|
||||||
|
import org.apache.lucene.store.SingleInstanceLockFactory;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.JUnitCore;
|
import org.junit.runner.JUnitCore;
|
||||||
|
@ -35,9 +41,38 @@ public class TestFailIfDirectoryNotClosed extends WithNestedTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class Nested2 extends WithNestedTests.AbstractNestedTest {
|
||||||
|
public void testDummy() throws IOException {
|
||||||
|
MockDirectoryWrapper dir = newMockDirectory();
|
||||||
|
IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, null));
|
||||||
|
dir.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Nested3 extends WithNestedTests.AbstractNestedTest {
|
||||||
|
public void testDummy() throws IOException {
|
||||||
|
MockDirectoryWrapper dir = newMockDirectory();
|
||||||
|
dir.setLockFactory(new SingleInstanceLockFactory());
|
||||||
|
IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, null));
|
||||||
|
dir.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFailIfDirectoryNotClosed() {
|
public void testFailIfDirectoryNotClosed() {
|
||||||
Result r = JUnitCore.runClasses(Nested1.class);
|
Result r = JUnitCore.runClasses(Nested1.class);
|
||||||
Assert.assertEquals(1, r.getFailureCount());
|
Assert.assertEquals(1, r.getFailureCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFailIfIndexWriterNotClosed() {
|
||||||
|
Result r = JUnitCore.runClasses(Nested2.class);
|
||||||
|
Assert.assertEquals(1, r.getFailureCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFailIfIndexWriterNotClosedChangeLockFactory() {
|
||||||
|
Result r = JUnitCore.runClasses(Nested3.class);
|
||||||
|
Assert.assertEquals(1, r.getFailureCount());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,6 +67,7 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {
|
||||||
boolean noDeleteOpenFile = true;
|
boolean noDeleteOpenFile = true;
|
||||||
boolean preventDoubleWrite = true;
|
boolean preventDoubleWrite = true;
|
||||||
boolean trackDiskUsage = false;
|
boolean trackDiskUsage = false;
|
||||||
|
boolean wrapLockFactory = true;
|
||||||
private Set<String> unSyncedFiles;
|
private Set<String> unSyncedFiles;
|
||||||
private Set<String> createdFiles;
|
private Set<String> createdFiles;
|
||||||
private Set<String> openFilesForWrite = new HashSet<String>();
|
private Set<String> openFilesForWrite = new HashSet<String>();
|
||||||
|
@ -114,11 +115,7 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {
|
||||||
this.throttledOutput = new ThrottledIndexOutput(ThrottledIndexOutput
|
this.throttledOutput = new ThrottledIndexOutput(ThrottledIndexOutput
|
||||||
.mBitsToBytes(40 + randomState.nextInt(10)), 5 + randomState.nextInt(5), null);
|
.mBitsToBytes(40 + randomState.nextInt(10)), 5 + randomState.nextInt(5), null);
|
||||||
// force wrapping of lockfactory
|
// force wrapping of lockfactory
|
||||||
try {
|
this.lockFactory = new MockLockFactoryWrapper(this, delegate.getLockFactory());
|
||||||
setLockFactory(new MockLockFactoryWrapper(this, delegate.getLockFactory()));
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2% of the time use rate limiter
|
// 2% of the time use rate limiter
|
||||||
if (randomState.nextInt(50) == 17) {
|
if (randomState.nextInt(50) == 17) {
|
||||||
|
@ -531,6 +528,19 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {
|
||||||
assertNoUnreferencedFilesOnClose = v;
|
assertNoUnreferencedFilesOnClose = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set to false if you want to return the pure lockfactory
|
||||||
|
* and not wrap it with MockLockFactoryWrapper.
|
||||||
|
* <p>
|
||||||
|
* Be careful if you turn this off: MockDirectoryWrapper might
|
||||||
|
* no longer be able to detect if you forget to close an IndexWriter,
|
||||||
|
* and spit out horribly scary confusing exceptions instead of
|
||||||
|
* simply telling you that.
|
||||||
|
*/
|
||||||
|
public void setWrapLockFactory(boolean v) {
|
||||||
|
this.wrapLockFactory = v;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void close() throws IOException {
|
public synchronized void close() throws IOException {
|
||||||
maybeYield();
|
maybeYield();
|
||||||
|
@ -699,25 +709,33 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {
|
||||||
@Override
|
@Override
|
||||||
public synchronized Lock makeLock(String name) {
|
public synchronized Lock makeLock(String name) {
|
||||||
maybeYield();
|
maybeYield();
|
||||||
return delegate.makeLock(name);
|
return getLockFactory().makeLock(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void clearLock(String name) throws IOException {
|
public synchronized void clearLock(String name) throws IOException {
|
||||||
maybeYield();
|
maybeYield();
|
||||||
delegate.clearLock(name);
|
getLockFactory().clearLock(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void setLockFactory(LockFactory lockFactory) throws IOException {
|
public synchronized void setLockFactory(LockFactory lockFactory) throws IOException {
|
||||||
maybeYield();
|
maybeYield();
|
||||||
|
// sneaky: we must pass the original this way to the dir, because
|
||||||
|
// some impls (e.g. FSDir) do instanceof here.
|
||||||
delegate.setLockFactory(lockFactory);
|
delegate.setLockFactory(lockFactory);
|
||||||
|
// now set our wrapped factory here
|
||||||
|
this.lockFactory = new MockLockFactoryWrapper(this, lockFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized LockFactory getLockFactory() {
|
public synchronized LockFactory getLockFactory() {
|
||||||
maybeYield();
|
maybeYield();
|
||||||
return delegate.getLockFactory();
|
if (wrapLockFactory) {
|
||||||
|
return lockFactory;
|
||||||
|
} else {
|
||||||
|
return delegate.getLockFactory();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue