コード例 #1
0
 /**
  * Test what happens if we try to write to a locked taxonomy writer, and see that we can unlock it
  * and continue.
  */
 @Test
 public void testWriterLock() throws Exception {
   // native fslock impl gets angry if we use it, so use RAMDirectory explicitly.
   Directory indexDir = new RAMDirectory();
   TaxonomyWriter tw = new DirectoryTaxonomyWriter(indexDir);
   tw.addCategory(new FacetLabel("hi", "there"));
   tw.commit();
   // we deliberately not close the write now, and keep it open and
   // locked.
   // Verify that the writer worked:
   TaxonomyReader tr = new DirectoryTaxonomyReader(indexDir);
   assertEquals(2, tr.getOrdinal(new FacetLabel("hi", "there")));
   // Try to open a second writer, with the first one locking the directory.
   // We expect to get a LockObtainFailedException.
   try {
     assertNull(new DirectoryTaxonomyWriter(indexDir));
     fail("should have failed to write in locked directory");
   } catch (LockObtainFailedException e) {
     // this is what we expect to happen.
   }
   // Remove the lock, and now the open should succeed, and we can
   // write to the new writer.
   DirectoryTaxonomyWriter.unlock(indexDir);
   TaxonomyWriter tw2 = new DirectoryTaxonomyWriter(indexDir);
   tw2.addCategory(new FacetLabel("hey"));
   tw2.close();
   // See that the writer indeed wrote:
   TaxonomyReader newtr = TaxonomyReader.openIfChanged(tr);
   assertNotNull(newtr);
   tr.close();
   tr = newtr;
   assertEquals(3, tr.getOrdinal(new FacetLabel("hey")));
   tr.close();
   tw.close();
   indexDir.close();
 }