📄 testlockfactory.java
字号:
public void testFSDirectoryDifferentLockFactory() throws IOException { String indexDirName = "index.TestLockFactory5"; LockFactory lf = new SingleInstanceLockFactory(); FSDirectory fs1 = FSDirectory.getDirectory(indexDirName, lf); // Different lock factory instance should hit IOException: try { FSDirectory fs2 = FSDirectory.getDirectory(indexDirName, new SingleInstanceLockFactory()); fail("Should have hit an IOException because LockFactory instances differ"); } catch (IOException e) { } FSDirectory fs2 = null; // Same lock factory instance should not: try { fs2 = FSDirectory.getDirectory(indexDirName, lf); } catch (IOException e) { e.printStackTrace(System.out); fail("Should not have hit an IOException because LockFactory instances are the same"); } fs1.close(); if (fs2 != null) { fs2.close(); } // Cleanup rmDir(indexDirName); } // Verify: do stress test, by opening IndexReaders and // IndexWriters over & over in 2 threads and making sure // no unexpected exceptions are raised: public void testStressLocks() throws IOException { _testStressLocks(null, "index.TestLockFactory6"); } // Verify: do stress test, by opening IndexReaders and // IndexWriters over & over in 2 threads and making sure // no unexpected exceptions are raised, but use // NativeFSLockFactory: public void testStressLocksNativeFSLockFactory() throws IOException { _testStressLocks(new NativeFSLockFactory("index.TestLockFactory7"), "index.TestLockFactory7"); } public void _testStressLocks(LockFactory lockFactory, String indexDirName) throws IOException { FSDirectory fs1 = FSDirectory.getDirectory(indexDirName, lockFactory); // First create a 1 doc index: IndexWriter w = new IndexWriter(fs1, new WhitespaceAnalyzer(), true); addDoc(w); w.close(); WriterThread writer = new WriterThread(100, fs1); SearcherThread searcher = new SearcherThread(100, fs1); writer.start(); searcher.start(); while(writer.isAlive() || searcher.isAlive()) { try { Thread.sleep(1000); } catch (InterruptedException e) { } } assertTrue("IndexWriter hit unexpected exceptions", !writer.hitException); assertTrue("IndexSearcher hit unexpected exceptions", !searcher.hitException); // Cleanup rmDir(indexDirName); } // Verify: NativeFSLockFactory works correctly public void testNativeFSLockFactory() throws IOException { NativeFSLockFactory f = new NativeFSLockFactory(System.getProperty("tempDir")); NativeFSLockFactory f2 = new NativeFSLockFactory(System.getProperty("tempDir")); f.setLockPrefix("test"); Lock l = f.makeLock("commit"); Lock l2 = f.makeLock("commit"); assertTrue("failed to obtain lock", l.obtain()); assertTrue("succeeded in obtaining lock twice", !l2.obtain()); l.release(); assertTrue("failed to obtain 2nd lock after first one was freed", l2.obtain()); l2.release(); // Make sure we can obtain first one again: assertTrue("failed to obtain lock", l.obtain()); l.release(); } // Verify: NativeFSLockFactory assigns different lock // prefixes to different directories: public void testNativeFSLockFactoryPrefix() throws IOException { // Make sure we get identical instances: Directory dir1 = FSDirectory.getDirectory("TestLockFactory.8", new NativeFSLockFactory("TestLockFactory.8")); Directory dir2 = FSDirectory.getDirectory("TestLockFactory.9", new NativeFSLockFactory("TestLockFactory.9")); String prefix1 = dir1.getLockFactory().getLockPrefix(); String prefix2 = dir2.getLockFactory().getLockPrefix(); assertTrue("Native Lock Factories are incorrectly shared: dir1 and dir2 have same lock prefix '" + prefix1 + "'; they should be different", !prefix1.equals(prefix2)); rmDir("TestLockFactory.8"); rmDir("TestLockFactory.9"); } // Verify: default LockFactory has no prefix (ie // write.lock is stored in index): public void testDefaultFSLockFactoryPrefix() throws IOException { // Make sure we get null prefix: Directory dir = FSDirectory.getDirectory("TestLockFactory.10"); String prefix = dir.getLockFactory().getLockPrefix(); assertTrue("Default lock prefix should be null", null == prefix); rmDir("TestLockFactory.10"); } private class WriterThread extends Thread { private Directory dir; private int numIteration; public boolean hitException = false; public WriterThread(int numIteration, Directory dir) { this.numIteration = numIteration; this.dir = dir; } public void run() { WhitespaceAnalyzer analyzer = new WhitespaceAnalyzer(); IndexWriter writer = null; for(int i=0;i<this.numIteration;i++) { try { writer = new IndexWriter(dir, analyzer, false); } catch (IOException e) { if (e.toString().indexOf(" timed out:") == -1) { hitException = true; } else { // lock obtain timed out // NOTE: we should at some point // consider this a failure? The lock // obtains, across IndexReader & // IndexWriters should be "fair" (ie // FIFO). } } catch (Exception e) { hitException = true; System.out.println("Stress Test Index Writer: creation hit unexpected exception: " + e.toString()); e.printStackTrace(System.out); break; } if (writer != null) { try { addDoc(writer); } catch (IOException e) { hitException = true; System.out.println("Stress Test Index Writer: addDoc hit unexpected exception: " + e.toString()); e.printStackTrace(System.out); break; } try { writer.close(); } catch (IOException e) { hitException = true; System.out.println("Stress Test Index Writer: close hit unexpected exception: " + e.toString()); e.printStackTrace(System.out); break; } writer = null; } } } } private class SearcherThread extends Thread { private Directory dir; private int numIteration; public boolean hitException = false; public SearcherThread(int numIteration, Directory dir) { this.numIteration = numIteration; this.dir = dir; } public void run() { IndexSearcher searcher = null; WhitespaceAnalyzer analyzer = new WhitespaceAnalyzer(); Query query = new TermQuery(new Term("content", "aaa")); for(int i=0;i<this.numIteration;i++) { try{ searcher = new IndexSearcher(dir); } catch (Exception e) { hitException = true; System.out.println("Stress Test Index Searcher: create hit unexpected exception: " + e.toString()); e.printStackTrace(System.out); break; } if (searcher != null) { Hits hits = null; try { hits = searcher.search(query); } catch (IOException e) { hitException = true; System.out.println("Stress Test Index Searcher: search hit unexpected exception: " + e.toString()); e.printStackTrace(System.out); break; } // System.out.println(hits.length() + " total results"); try { searcher.close(); } catch (IOException e) { hitException = true; System.out.println("Stress Test Index Searcher: close hit unexpected exception: " + e.toString()); e.printStackTrace(System.out); break; } searcher = null; } } } } public class MockLockFactory extends LockFactory { public boolean lockPrefixSet; public Hashtable locksCreated = new Hashtable(); public int makeLockCount = 0; public void setLockPrefix(String lockPrefix) { super.setLockPrefix(lockPrefix); lockPrefixSet = true; } synchronized public Lock makeLock(String lockName) { Lock lock = new MockLock(); locksCreated.put(lockName, lock); makeLockCount++; return lock; } public void clearLock(String specificLockName) {} public class MockLock extends Lock { public int lockAttempts; public boolean obtain() { lockAttempts++; return true; } public void release() { // do nothing } public boolean isLocked() { return false; } } } private void addDoc(IndexWriter writer) throws IOException { Document doc = new Document(); doc.add(new Field("content", "aaa", Field.Store.NO, Field.Index.TOKENIZED)); writer.addDocument(doc); } private void rmDir(String dirName) { File dir = new java.io.File(dirName); String[] files = dir.list(); // clear old files for (int i = 0; i < files.length; i++) { File file = new File(dir, files[i]); file.delete(); } dir.delete(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -