From 3cfa0152671318aeaaf7c33f889501070c835778 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Fri, 3 Oct 2014 20:30:17 +0000 Subject: [PATCH] LUCENE-5969: port remaining tests git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene5969@1629313 13f79535-47bb-0310-9956-ffa450edef68 --- .../lucene/index/TestCompoundFile2.java | 36 --- .../index/BaseCompoundFormatTestCase.java | 267 ++++++++++++++++++ 2 files changed, 267 insertions(+), 36 deletions(-) delete mode 100644 lucene/core/src/test/org/apache/lucene/index/TestCompoundFile2.java diff --git a/lucene/core/src/test/org/apache/lucene/index/TestCompoundFile2.java b/lucene/core/src/test/org/apache/lucene/index/TestCompoundFile2.java deleted file mode 100644 index 92bd0bd18df..00000000000 --- a/lucene/core/src/test/org/apache/lucene/index/TestCompoundFile2.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.apache.lucene.index; - -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import java.io.IOException; - -import org.apache.lucene.store.Directory; -import org.apache.lucene.store.IndexInput; -import org.apache.lucene.util.LuceneTestCase; -import org.apache.lucene.util.StringHelper; - -/** - * 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 - f0 to f19, - * the size of each file is 1000 bytes. - */ -public class TestCompoundFile2 extends LuceneTestCase { - /* nocommit: fold all these tests into BaseCompoundFormatTestCase */ - -} diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java index 84dcddc23b1..bef1db7de29 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java +++ b/lucene/test-framework/src/java/org/apache/lucene/index/BaseCompoundFormatTestCase.java @@ -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 BaseCompoundFormatTestCase extends BaseIndexFileFormatTest 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 BaseCompoundFormatTestCase extends BaseIndexFileFormatTest 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 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) {