[TEST] Close input stream in test to not upset windows

This commit is contained in:
Simon Willnauer 2014-09-06 22:07:01 +02:00
parent 13e3a5e99c
commit 36f9d39205
1 changed files with 16 additions and 15 deletions

View File

@ -30,38 +30,39 @@ import org.junit.Test;
import java.io.*; import java.io.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random;
public class BlobStoreTest extends ElasticsearchTestCase { public class BlobStoreTest extends ElasticsearchTestCase {
@Test @Test
public void testWriteRead() throws IOException { public void testWriteRead() throws IOException {
BlobContainer store = newBlobContainer(); final BlobStore store = newBlobStore();
final BlobContainer container = store.blobContainer(new BlobPath());
int length = randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16)); int length = randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16));
byte[] data = new byte[length]; byte[] data = new byte[length];
for (int i = 0; i < data.length; i++) { for (int i = 0; i < data.length; i++) {
data[i] = (byte) randomInt(); data[i] = (byte) randomInt();
} }
try (OutputStream stream = store.createOutput("foobar")) { try (OutputStream stream = container.createOutput("foobar")) {
stream.write(data); stream.write(data);
} }
InputStream stream = store.openInput("foobar"); try (InputStream stream = container.openInput("foobar")) {
BytesRef target = new BytesRef();
BytesRef target = new BytesRef(); while (target.length < data.length) {
while (target.length < data.length) { byte[] buffer = new byte[scaledRandomIntBetween(1, data.length - target.length)];
byte[] buffer = new byte[scaledRandomIntBetween(1, data.length - target.length)]; int offset = scaledRandomIntBetween(0, buffer.length - 1);
int offset = scaledRandomIntBetween(0, buffer.length - 1); int read = stream.read(buffer, offset, buffer.length - offset);
int read = stream.read(buffer, offset, buffer.length - offset); target.append(new BytesRef(buffer, offset, read));
target.append(new BytesRef(buffer, offset, read)); }
assertEquals(data.length, target.length);
assertArrayEquals(data, Arrays.copyOfRange(target.bytes, target.offset, target.length));
} }
assertEquals(data.length, target.length); store.close();
assertArrayEquals(data, Arrays.copyOfRange(target.bytes, target.offset, target.length));
} }
protected BlobContainer newBlobContainer() { protected BlobStore newBlobStore() {
File tempDir = newTempDir(LifecycleScope.TEST); File tempDir = newTempDir(LifecycleScope.TEST);
Settings settings = randomBoolean() ? ImmutableSettings.EMPTY : ImmutableSettings.builder().put("buffer_size", new ByteSizeValue(randomIntBetween(1, 100), ByteSizeUnit.KB)).build(); Settings settings = randomBoolean() ? ImmutableSettings.EMPTY : ImmutableSettings.builder().put("buffer_size", new ByteSizeValue(randomIntBetween(1, 100), ByteSizeUnit.KB)).build();
FsBlobStore store = new FsBlobStore(settings, tempDir); FsBlobStore store = new FsBlobStore(settings, tempDir);
return store.blobContainer(new BlobPath()); return store;
} }
} }