Use ByteSource methods to create test input

Also avoids excessive system calls due to unbuffered writes.  Finally
migrate repeatingArrayByteSource to ByteSources.
This commit is contained in:
Andrew Gaul 2014-05-22 10:43:57 -07:00
parent 4c265d3168
commit 26b53e52b7
3 changed files with 12 additions and 20 deletions

View File

@ -22,6 +22,7 @@ import java.io.IOException;
import java.io.InputStream;
import com.google.common.annotations.Beta;
import com.google.common.collect.Iterables;
import com.google.common.io.ByteSource;
/**
@ -48,4 +49,8 @@ public class ByteSources {
};
}
/** Create an infinite-length ByteSource which repeats its input. */
public static ByteSource repeatingArrayByteSource(final byte[] input) {
return ByteSource.concat(Iterables.cycle(ByteSource.wrap(input)));
}
}

View File

@ -32,11 +32,12 @@ import static org.testng.Assert.fail;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Random;
import java.util.zip.GZIPInputStream;
import org.jclouds.io.ByteSources;
import org.jclouds.io.Payload;
import org.jclouds.util.Strings2;
import org.testng.annotations.BeforeClass;
@ -48,7 +49,6 @@ import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;
import com.google.common.io.ByteSource;
import com.google.common.io.CharSink;
import com.google.common.io.Files;
import com.squareup.okhttp.mockwebserver.Dispatcher;
import com.squareup.okhttp.mockwebserver.MockResponse;
@ -277,18 +277,9 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
f = File.createTempFile("jclouds", "tmp");
f.deleteOnExit();
long length = (new Random().nextInt(32) + 1) * 1024 * 1024;
CharSink fileSink = Files.asCharSink(f, Charsets.UTF_8);
Writer out = null;
try {
out = fileSink.openStream();
for (long i = 0; i < length; i++) {
out.append('a');
}
out.flush();
} finally {
close(out, true);
}
byte[] buf = new byte[1024];
Arrays.fill(buf, (byte) 'a');
ByteSources.repeatingArrayByteSource(buf).slice(0, length).copyTo(Files.asByteSink(f));
ByteSource byteSource = asByteSource(f);
payload = newByteSourcePayload(byteSource);

View File

@ -20,7 +20,6 @@ import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import com.google.common.collect.Iterables;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
import org.jclouds.azureblob.blobstore.strategy.MultipartUploadStrategy;
@ -28,6 +27,7 @@ import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.integration.internal.BaseBlobIntegrationTest;
import org.jclouds.blobstore.options.PutOptions;
import org.jclouds.io.ByteSources;
import org.testng.SkipException;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
@ -98,7 +98,7 @@ public class AzureBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
public void testMultipartChunkedFileStreamPowerOfTwoSize() throws IOException, InterruptedException {
final long limit = MultipartUploadStrategy.MAX_BLOCK_SIZE;
ByteSource input = repeatingArrayByteSource(new byte[1024]).slice(0, limit);
ByteSource input = ByteSources.repeatingArrayByteSource(new byte[1024]).slice(0, limit);
File file = new File("target/const.txt");
input.copyTo(Files.asByteSink(file));
String containerName = getContainerName();
@ -114,8 +114,4 @@ public class AzureBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
returnContainer(containerName);
}
}
private static ByteSource repeatingArrayByteSource(final byte[] input) {
return ByteSource.concat(Iterables.cycle(ByteSource.wrap(input)));
}
}