From 26b53e52b7e758cd1052c3430e79871284b9b30e Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 22 May 2014 10:43:57 -0700 Subject: [PATCH] Use ByteSource methods to create test input Also avoids excessive system calls due to unbuffered writes. Finally migrate repeatingArrayByteSource to ByteSources. --- .../main/java/org/jclouds/io/ByteSources.java | 5 +++++ ...CommandExecutorServiceIntegrationTest.java | 19 +++++-------------- .../AzureBlobIntegrationLiveTest.java | 8 ++------ 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/core/src/main/java/org/jclouds/io/ByteSources.java b/core/src/main/java/org/jclouds/io/ByteSources.java index 06fe135460..7055664195 100644 --- a/core/src/main/java/org/jclouds/io/ByteSources.java +++ b/core/src/main/java/org/jclouds/io/ByteSources.java @@ -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))); + } } diff --git a/core/src/test/java/org/jclouds/http/BaseHttpCommandExecutorServiceIntegrationTest.java b/core/src/test/java/org/jclouds/http/BaseHttpCommandExecutorServiceIntegrationTest.java index f3c272975b..c10a354a47 100644 --- a/core/src/test/java/org/jclouds/http/BaseHttpCommandExecutorServiceIntegrationTest.java +++ b/core/src/test/java/org/jclouds/http/BaseHttpCommandExecutorServiceIntegrationTest.java @@ -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); diff --git a/providers/azureblob/src/test/java/org/jclouds/azureblob/blobstore/integration/AzureBlobIntegrationLiveTest.java b/providers/azureblob/src/test/java/org/jclouds/azureblob/blobstore/integration/AzureBlobIntegrationLiveTest.java index 2c4060ff9d..6238002053 100644 --- a/providers/azureblob/src/test/java/org/jclouds/azureblob/blobstore/integration/AzureBlobIntegrationLiveTest.java +++ b/providers/azureblob/src/test/java/org/jclouds/azureblob/blobstore/integration/AzureBlobIntegrationLiveTest.java @@ -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))); - } }