mirror of https://github.com/apache/lucene.git
LUCENE-2852: fix false EOF corner case in RAMInputStream
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1056428 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
602a4e906a
commit
cabea43938
|
@ -83,6 +83,7 @@ class RAMInputStream extends IndexInput implements Cloneable {
|
||||||
}
|
}
|
||||||
|
|
||||||
private final void switchCurrentBuffer(boolean enforceEOF) throws IOException {
|
private final void switchCurrentBuffer(boolean enforceEOF) throws IOException {
|
||||||
|
bufferStart = (long) BUFFER_SIZE * (long) currentBufferIndex;
|
||||||
if (currentBufferIndex >= file.numBuffers()) {
|
if (currentBufferIndex >= file.numBuffers()) {
|
||||||
// end of file reached, no more buffers left
|
// end of file reached, no more buffers left
|
||||||
if (enforceEOF)
|
if (enforceEOF)
|
||||||
|
@ -95,7 +96,6 @@ class RAMInputStream extends IndexInput implements Cloneable {
|
||||||
} else {
|
} else {
|
||||||
currentBuffer = file.getBuffer(currentBufferIndex);
|
currentBuffer = file.getBuffer(currentBufferIndex);
|
||||||
bufferPosition = 0;
|
bufferPosition = 0;
|
||||||
bufferStart = (long) BUFFER_SIZE * (long) currentBufferIndex;
|
|
||||||
long buflen = length - bufferStart;
|
long buflen = length - bufferStart;
|
||||||
bufferLength = buflen > BUFFER_SIZE ? BUFFER_SIZE : (int) buflen;
|
bufferLength = buflen > BUFFER_SIZE ? BUFFER_SIZE : (int) buflen;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ import org.apache.lucene.document.*;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class TestThreadSafe extends LuceneTestCase {
|
public class TestThreadSafe extends LuceneTestCase {
|
||||||
|
@ -34,16 +35,16 @@ public class TestThreadSafe extends LuceneTestCase {
|
||||||
|
|
||||||
IndexReader ir1;
|
IndexReader ir1;
|
||||||
|
|
||||||
String failure=null;
|
|
||||||
|
|
||||||
|
|
||||||
class Thr extends Thread {
|
class Thr extends Thread {
|
||||||
final int iter;
|
final int iter;
|
||||||
final Random rand;
|
final Random rand;
|
||||||
|
final AtomicBoolean failed;
|
||||||
|
|
||||||
// pass in random in case we want to make things reproducable
|
// pass in random in case we want to make things reproducable
|
||||||
public Thr(int iter, Random rand) {
|
public Thr(int iter, Random rand, AtomicBoolean failed) {
|
||||||
this.iter = iter;
|
this.iter = iter;
|
||||||
this.rand = rand;
|
this.rand = rand;
|
||||||
|
this.failed = failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -61,8 +62,8 @@ public class TestThreadSafe extends LuceneTestCase {
|
||||||
|
|
||||||
}
|
}
|
||||||
} catch (Throwable th) {
|
} catch (Throwable th) {
|
||||||
failure=th.toString();
|
failed.set(true);
|
||||||
fail(failure);
|
throw new RuntimeException(th);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,16 +125,15 @@ public class TestThreadSafe extends LuceneTestCase {
|
||||||
|
|
||||||
void doTest(int iter, int nThreads) throws Exception {
|
void doTest(int iter, int nThreads) throws Exception {
|
||||||
Thr[] tarr = new Thr[nThreads];
|
Thr[] tarr = new Thr[nThreads];
|
||||||
|
AtomicBoolean failed = new AtomicBoolean();
|
||||||
for (int i=0; i<nThreads; i++) {
|
for (int i=0; i<nThreads; i++) {
|
||||||
tarr[i] = new Thr(iter, new Random(random.nextLong()));
|
tarr[i] = new Thr(iter, new Random(random.nextLong()), failed);
|
||||||
tarr[i].start();
|
tarr[i].start();
|
||||||
}
|
}
|
||||||
for (int i=0; i<nThreads; i++) {
|
for (int i=0; i<nThreads; i++) {
|
||||||
tarr[i].join();
|
tarr[i].join();
|
||||||
}
|
}
|
||||||
if (failure!=null) {
|
assertFalse(failed.get());
|
||||||
fail(failure);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testLazyLoadThreadSafety() throws Exception{
|
public void testLazyLoadThreadSafety() throws Exception{
|
||||||
|
|
|
@ -180,4 +180,22 @@ public class TestRAMDirectory extends LuceneTestCase {
|
||||||
}
|
}
|
||||||
dir.delete();
|
dir.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LUCENE-2852
|
||||||
|
public void testSeekToEOFThenBack() throws Exception {
|
||||||
|
RAMDirectory dir = new RAMDirectory();
|
||||||
|
|
||||||
|
IndexOutput o = dir.createOutput("out");
|
||||||
|
byte[] bytes = new byte[3*RAMInputStream.BUFFER_SIZE];
|
||||||
|
o.writeBytes(bytes, 0, bytes.length);
|
||||||
|
o.close();
|
||||||
|
|
||||||
|
IndexInput i = dir.openInput("out");
|
||||||
|
i.seek(2*RAMInputStream.BUFFER_SIZE-1);
|
||||||
|
i.seek(3*RAMInputStream.BUFFER_SIZE);
|
||||||
|
i.seek(RAMInputStream.BUFFER_SIZE);
|
||||||
|
i.readBytes(bytes, 0, 2*RAMInputStream.BUFFER_SIZE);
|
||||||
|
i.close();
|
||||||
|
dir.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue