You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2014/10/03 22:30:17 UTC

svn commit: r1629313 - in /lucene/dev/branches/lucene5969/lucene: core/src/test/org/apache/lucene/index/TestCompoundFile2.java test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java

Author: rmuir
Date: Fri Oct  3 20:30:17 2014
New Revision: 1629313

URL: http://svn.apache.org/r1629313
Log:
LUCENE-5969: port remaining tests

Removed:
    lucene/dev/branches/lucene5969/lucene/core/src/test/org/apache/lucene/index/TestCompoundFile2.java
Modified:
    lucene/dev/branches/lucene5969/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java

Modified: lucene/dev/branches/lucene5969/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5969/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java?rev=1629313&r1=1629312&r2=1629313&view=diff
==============================================================================
--- lucene/dev/branches/lucene5969/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java (original)
+++ lucene/dev/branches/lucene5969/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java Fri Oct  3 20:30:17 2014
@@ -18,8 +18,10 @@ package org.apache.lucene.index;
  */
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.List;
 
 import org.apache.lucene.codecs.Codec;
 import org.apache.lucene.document.Document;
@@ -396,6 +398,252 @@ public abstract class BaseCompoundFormat
     dir.close();
   }
   
+  public void testClonedStreamsClosing() throws IOException {
+    Directory dir = newDirectory();
+    Directory cr = createLargeCFS(dir);
+    
+    // basic clone
+    IndexInput expected = dir.openInput("_123.f11", newIOContext(random()));
+    
+    IndexInput one = cr.openInput("_123.f11", newIOContext(random()));
+    
+    IndexInput two = one.clone();
+    
+    assertSameStreams("basic clone one", expected, one);
+    expected.seek(0);
+    assertSameStreams("basic clone two", expected, two);
+    
+    // Now close the first stream
+    one.close();
+    
+    // The following should really fail since we couldn't expect to
+    // access a file once close has been called on it (regardless of
+    // buffering and/or clone magic)
+    expected.seek(0);
+    two.seek(0);
+    assertSameStreams("basic clone two/2", expected, two);
+    
+    // Now close the compound reader
+    cr.close();
+    
+    // The following may also fail since the compound stream is closed
+    expected.seek(0);
+    two.seek(0);
+    //assertSameStreams("basic clone two/3", expected, two);
+    
+    // Now close the second clone
+    two.close();
+    expected.seek(0);
+    //assertSameStreams("basic clone two/4", expected, two);
+    
+    expected.close();
+    dir.close();
+  }
+  
+  /** This test opens two files from a compound stream and verifies that
+   *  their file positions are independent of each other.
+   */
+  public void testRandomAccess() throws IOException {
+    Directory dir = newDirectory();
+    Directory cr = createLargeCFS(dir);
+    
+    // Open two files
+    IndexInput e1 = dir.openInput("_123.f11", newIOContext(random()));
+    IndexInput e2 = dir.openInput("_123.f3", newIOContext(random()));
+    
+    IndexInput a1 = cr.openInput("_123.f11", newIOContext(random()));
+    IndexInput a2 = dir.openInput("_123.f3", newIOContext(random()));
+    
+    // Seek the first pair
+    e1.seek(100);
+    a1.seek(100);
+    assertEquals(100, e1.getFilePointer());
+    assertEquals(100, a1.getFilePointer());
+    byte be1 = e1.readByte();
+    byte ba1 = a1.readByte();
+    assertEquals(be1, ba1);
+    
+    // Now seek the second pair
+    e2.seek(1027);
+    a2.seek(1027);
+    assertEquals(1027, e2.getFilePointer());
+    assertEquals(1027, a2.getFilePointer());
+    byte be2 = e2.readByte();
+    byte ba2 = a2.readByte();
+    assertEquals(be2, ba2);
+    
+    // Now make sure the first one didn't move
+    assertEquals(101, e1.getFilePointer());
+    assertEquals(101, a1.getFilePointer());
+    be1 = e1.readByte();
+    ba1 = a1.readByte();
+    assertEquals(be1, ba1);
+    
+    // Now more the first one again, past the buffer length
+    e1.seek(1910);
+    a1.seek(1910);
+    assertEquals(1910, e1.getFilePointer());
+    assertEquals(1910, a1.getFilePointer());
+    be1 = e1.readByte();
+    ba1 = a1.readByte();
+    assertEquals(be1, ba1);
+    
+    // Now make sure the second set didn't move
+    assertEquals(1028, e2.getFilePointer());
+    assertEquals(1028, a2.getFilePointer());
+    be2 = e2.readByte();
+    ba2 = a2.readByte();
+    assertEquals(be2, ba2);
+    
+    // Move the second set back, again cross the buffer size
+    e2.seek(17);
+    a2.seek(17);
+    assertEquals(17, e2.getFilePointer());
+    assertEquals(17, a2.getFilePointer());
+    be2 = e2.readByte();
+    ba2 = a2.readByte();
+    assertEquals(be2, ba2);
+    
+    // Finally, make sure the first set didn't move
+    // Now make sure the first one didn't move
+    assertEquals(1911, e1.getFilePointer());
+    assertEquals(1911, a1.getFilePointer());
+    be1 = e1.readByte();
+    ba1 = a1.readByte();
+    assertEquals(be1, ba1);
+    
+    e1.close();
+    e2.close();
+    a1.close();
+    a2.close();
+    cr.close();
+    dir.close();
+  }
+  
+  /** This test opens two files from a compound stream and verifies that
+   *  their file positions are independent of each other.
+   */
+  public void testRandomAccessClones() throws IOException {
+    Directory dir = newDirectory();
+    Directory cr = createLargeCFS(dir);
+    
+    // Open two files
+    IndexInput e1 = cr.openInput("_123.f11", newIOContext(random()));
+    IndexInput e2 = cr.openInput("_123.f3", newIOContext(random()));
+    
+    IndexInput a1 = e1.clone();
+    IndexInput a2 = e2.clone();
+    
+    // Seek the first pair
+    e1.seek(100);
+    a1.seek(100);
+    assertEquals(100, e1.getFilePointer());
+    assertEquals(100, a1.getFilePointer());
+    byte be1 = e1.readByte();
+    byte ba1 = a1.readByte();
+    assertEquals(be1, ba1);
+    
+    // Now seek the second pair
+    e2.seek(1027);
+    a2.seek(1027);
+    assertEquals(1027, e2.getFilePointer());
+    assertEquals(1027, a2.getFilePointer());
+    byte be2 = e2.readByte();
+    byte ba2 = a2.readByte();
+    assertEquals(be2, ba2);
+    
+    // Now make sure the first one didn't move
+    assertEquals(101, e1.getFilePointer());
+    assertEquals(101, a1.getFilePointer());
+    be1 = e1.readByte();
+    ba1 = a1.readByte();
+    assertEquals(be1, ba1);
+    
+    // Now more the first one again, past the buffer length
+    e1.seek(1910);
+    a1.seek(1910);
+    assertEquals(1910, e1.getFilePointer());
+    assertEquals(1910, a1.getFilePointer());
+    be1 = e1.readByte();
+    ba1 = a1.readByte();
+    assertEquals(be1, ba1);
+    
+    // Now make sure the second set didn't move
+    assertEquals(1028, e2.getFilePointer());
+    assertEquals(1028, a2.getFilePointer());
+    be2 = e2.readByte();
+    ba2 = a2.readByte();
+    assertEquals(be2, ba2);
+    
+    // Move the second set back, again cross the buffer size
+    e2.seek(17);
+    a2.seek(17);
+    assertEquals(17, e2.getFilePointer());
+    assertEquals(17, a2.getFilePointer());
+    be2 = e2.readByte();
+    ba2 = a2.readByte();
+    assertEquals(be2, ba2);
+    
+    // Finally, make sure the first set didn't move
+    // Now make sure the first one didn't move
+    assertEquals(1911, e1.getFilePointer());
+    assertEquals(1911, a1.getFilePointer());
+    be1 = e1.readByte();
+    ba1 = a1.readByte();
+    assertEquals(be1, ba1);
+    
+    e1.close();
+    e2.close();
+    a1.close();
+    a2.close();
+    cr.close();
+    dir.close();
+  }
+  
+  public void testFileNotFound() throws IOException {
+    Directory dir = newDirectory();
+    Directory cr = createLargeCFS(dir);
+    
+    // Open bogus file
+    try {
+      cr.openInput("bogus", newIOContext(random()));
+      fail("File not found");
+    } catch (IOException e) {
+      /* success */;
+    }
+    
+    cr.close();
+    dir.close();
+  }
+  
+  public void testReadPastEOF() throws IOException {
+    Directory dir = newDirectory();
+    Directory cr = createLargeCFS(dir);
+    IndexInput is = cr.openInput("_123.f2", newIOContext(random()));
+    is.seek(is.length() - 10);
+    byte b[] = new byte[100];
+    is.readBytes(b, 0, 10);
+    
+    try {
+      is.readByte();
+      fail("Single byte read past end of file");
+    } catch (IOException e) {
+      /* success */
+    }
+    
+    is.seek(is.length() - 10);
+    try {
+      is.readBytes(b, 0, 50);
+      fail("Block read past end of file");
+    } catch (IOException e) {
+      /* success */
+    }
+    
+    is.close();
+    cr.close();
+    dir.close();
+  }
+  
   /** Returns a new fake segment */
   protected static SegmentInfo newSegmentInfo(Directory dir, String name) {
     return new SegmentInfo(dir, Version.LATEST, name, 10000, false, Codec.getDefault(), null, StringHelper.randomId());
@@ -485,6 +733,25 @@ public abstract class BaseCompoundFormat
       assertEquals(msg + " " + i, expected[i], test[i]);
     }
   }
+  
+  /** 
+   * Setup a large compound file with a number of components, each of
+   * which is a sequential file (so that we can easily tell that we are
+   * reading in the right byte). The methods sets up 20 files - _123.0 to _123.19,
+   * the size of each file is 1000 bytes.
+   */
+  protected static Directory createLargeCFS(Directory dir) throws IOException {
+    List<String> files = new ArrayList<>();
+    for (int i = 0; i < 20; i++) {
+      createSequenceFile(dir, "_123.f" + i, (byte) 0, 2000);
+      files.add("_123.f" + i);
+    }
+    
+    SegmentInfo si = newSegmentInfo(dir, "_123");
+    si.getCodec().compoundFormat().write(dir, si, files, MergeState.CheckAbort.NONE, IOContext.DEFAULT);
+    Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si, IOContext.DEFAULT);
+    return cfs;
+  }
 
   @Override
   protected void addRandomFields(Document doc) {