Package org.apache.lucene.tests.store
Class MockDirectoryWrapper
java.lang.Object
org.apache.lucene.store.Directory
org.apache.lucene.store.FilterDirectory
org.apache.lucene.tests.store.BaseDirectoryWrapper
org.apache.lucene.tests.store.MockDirectoryWrapper
- All Implemented Interfaces:
Closeable,AutoCloseable
This is a Directory Wrapper that adds methods intended to be used only by unit tests. It also
adds a number of features useful for testing:
- Instances created by
LuceneTestCase.newDirectory()are tracked to ensure they are closed by the test. - When a MockDirectoryWrapper is closed, it will throw an exception if it has any open files against it (with a stacktrace indicating where they were opened from).
- When a MockDirectoryWrapper is closed, it runs CheckIndex to test if the index was corrupted.
- MockDirectoryWrapper simulates some "features" of Windows, such as refusing to write/delete to open files.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classObjects that represent fail-able conditions.static classUse this when throwing fakeIOException, e.g.static enumEnum for controlling hard disk throttling. -
Field Summary
Fields inherited from class org.apache.lucene.tests.store.BaseDirectoryWrapper
isOpenFields inherited from class org.apache.lucene.store.FilterDirectory
in -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidclose()final voidvoidcorruptFiles(Collection<String> files) voidvoidcrash()Simulates a crash of OS or machine by overwriting unsynced files.createOutput(String name, IOContext context) createTempOutput(String prefix, String suffix, IOContext context) voiddeleteFile(String name) protected final voidvoidadd a Failure object to the list of objects to be evaluated at every potential failure pointlongfileLength(String name) booleanlongreturns current open file handle countintlonglongReturns the peek actual storage used (bytes) in this directory.doubledoubleString[]listAll()obtainLock(String name) final ChecksumIndexInputopenChecksumInput(String name) voidremoveIndexInput(IndexInput in, String name) voidremoveIndexOutput(IndexOutput out, String name) voidvoidvoidsetAllowRandomFileNotFoundException(boolean value) If set to true (the default), when we throw random IOException on openInput or createOutput, we may sometimes throw FileNotFoundException or NoSuchFileException.voidsetAllowReadingFilesStillOpenForWrite(boolean value) If set to true, you can open an inputstream on a file that is still open for writes.voidsetAssertNoDeleteOpenFile(boolean value) Trip a test assert if there is an attempt to delete an open file.voidsetAssertNoUnrefencedFilesOnClose(boolean v) voidsetFailOnCreateOutput(boolean v) voidsetFailOnOpenInput(boolean v) voidsetMaxSizeInBytes(long maxSize) voidsetRandomIOExceptionRate(double rate) If 0.0, no exceptions will be thrown.voidsetRandomIOExceptionRateOnOpen(double rate) If 0.0, no exceptions will be thrown during openInput and createOutput.voidsetThrottling(MockDirectoryWrapper.Throttling throttling) voidsetTrackDiskUsage(boolean v) voidsetUseSlowOpenClosers(boolean v) Add a rare small sleep to catch race conditions in open/closevoidsetVerboseClone(boolean v) If set to true, we print a fake exception with filename and stacktrace on every indexinput clone()final longvoidsync(Collection<String> names) voidtoString()Methods inherited from class org.apache.lucene.tests.store.BaseDirectoryWrapper
getCheckIndexOnClose, getLevelForCheckOnClose, isOpen, setCheckIndexOnClose, setCrossCheckTermVectorsOnCloseMethods inherited from class org.apache.lucene.store.FilterDirectory
getDelegate, getPendingDeletions, unwrapMethods inherited from class org.apache.lucene.store.Directory
getTempFileName
-
Constructor Details
-
MockDirectoryWrapper
-
-
Method Details
-
getInputCloneCount
public int getInputCloneCount() -
setVerboseClone
public void setVerboseClone(boolean v) If set to true, we print a fake exception with filename and stacktrace on every indexinput clone() -
setTrackDiskUsage
public void setTrackDiskUsage(boolean v) -
setAllowRandomFileNotFoundException
public void setAllowRandomFileNotFoundException(boolean value) If set to true (the default), when we throw random IOException on openInput or createOutput, we may sometimes throw FileNotFoundException or NoSuchFileException. -
setAllowReadingFilesStillOpenForWrite
public void setAllowReadingFilesStillOpenForWrite(boolean value) If set to true, you can open an inputstream on a file that is still open for writes. -
setThrottling
-
setUseSlowOpenClosers
public void setUseSlowOpenClosers(boolean v) Add a rare small sleep to catch race conditions in open/closeYou can enable this if you need it.
-
sync
- Overrides:
syncin classFilterDirectory- Throws:
IOException
-
rename
- Overrides:
renamein classFilterDirectory- Throws:
IOException
-
syncMetaData
- Overrides:
syncMetaDatain classFilterDirectory- Throws:
IOException
-
sizeInBytes
- Throws:
IOException
-
corruptUnknownFiles
- Throws:
IOException
-
corruptFiles
- Throws:
IOException
-
crash
Simulates a crash of OS or machine by overwriting unsynced files.- Throws:
IOException
-
clearCrash
public void clearCrash() -
setMaxSizeInBytes
public void setMaxSizeInBytes(long maxSize) -
getMaxSizeInBytes
public long getMaxSizeInBytes() -
getMaxUsedSizeInBytes
public long getMaxUsedSizeInBytes()Returns the peek actual storage used (bytes) in this directory. -
resetMaxUsedSizeInBytes
- Throws:
IOException
-
setAssertNoDeleteOpenFile
public void setAssertNoDeleteOpenFile(boolean value) Trip a test assert if there is an attempt to delete an open file. -
getAssertNoDeleteOpenFile
public boolean getAssertNoDeleteOpenFile() -
setRandomIOExceptionRate
public void setRandomIOExceptionRate(double rate) If 0.0, no exceptions will be thrown. Else this should be a double 0.0 - 1.0. We will randomly throw an IOException on the first write to an OutputStream based on this probability. -
getRandomIOExceptionRate
public double getRandomIOExceptionRate() -
setRandomIOExceptionRateOnOpen
public void setRandomIOExceptionRateOnOpen(double rate) If 0.0, no exceptions will be thrown during openInput and createOutput. Else this should be a double 0.0 - 1.0 and we will randomly throw an IOException in openInput and createOutput with this probability. -
getRandomIOExceptionRateOnOpen
public double getRandomIOExceptionRateOnOpen() -
getFileHandleCount
public long getFileHandleCount()returns current open file handle count -
deleteFile
- Overrides:
deleteFilein classFilterDirectory- Throws:
IOException
-
getOpenDeletedFiles
-
setFailOnCreateOutput
public void setFailOnCreateOutput(boolean v) -
createOutput
- Overrides:
createOutputin classFilterDirectory- Throws:
IOException
-
createTempOutput
public IndexOutput createTempOutput(String prefix, String suffix, IOContext context) throws IOException - Overrides:
createTempOutputin classFilterDirectory- Throws:
IOException
-
setFailOnOpenInput
public void setFailOnOpenInput(boolean v) -
openInput
- Overrides:
openInputin classFilterDirectory- Throws:
IOException
-
setAssertNoUnrefencedFilesOnClose
public void setAssertNoUnrefencedFilesOnClose(boolean v) -
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classBaseDirectoryWrapper- Throws:
IOException
-
removeIndexOutput
-
removeIndexInput
-
failOn
add a Failure object to the list of objects to be evaluated at every potential failure point -
listAll
- Overrides:
listAllin classFilterDirectory- Throws:
IOException
-
fileLength
- Overrides:
fileLengthin classFilterDirectory- Throws:
IOException
-
obtainLock
- Overrides:
obtainLockin classFilterDirectory- Throws:
IOException
-
toString
- Overrides:
toStringin classFilterDirectory
-
openChecksumInput
- Overrides:
openChecksumInputin classDirectory- Throws:
IOException
-
copyFrom
public final void copyFrom(Directory from, String src, String dest, IOContext context) throws IOException - Overrides:
copyFromin classDirectory- Throws:
IOException
-
ensureOpen
- Overrides:
ensureOpenin classFilterDirectory- Throws:
AlreadyClosedException
-