Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for BlobWriteChannel and BlobReadChannel #188

Merged
merged 3 commits into from
Sep 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/**
* Default implementation for BlobWriteChannel.
*/
class BlobWriterChannelImpl implements BlobWriteChannel {
class BlobWriteChannelImpl implements BlobWriteChannel {

private static final long serialVersionUID = 8675286882724938737L;
private static final int MIN_CHUNK_SIZE = 256 * 1024;
Expand All @@ -50,12 +50,12 @@ class BlobWriterChannelImpl implements BlobWriteChannel {
private transient StorageRpc storageRpc;
private transient StorageObject storageObject;

BlobWriterChannelImpl(StorageOptions options, BlobInfo blobInfo,
BlobWriteChannelImpl(StorageOptions options, BlobInfo blobInfo,
Map<StorageRpc.Option, ?> optionsMap) {
this.options = options;
this.blobInfo = blobInfo;
initTransients();
uploadId = options.storageRpc().open(storageObject, optionsMap);
uploadId = storageRpc.open(storageObject, optionsMap);
}

private void writeObject(ObjectOutputStream out) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public BlobReadChannel reader(String bucket, String blob, BlobSourceOption... op
@Override
public BlobWriteChannel writer(BlobInfo blobInfo, BlobTargetOption... options) {
final Map<StorageRpc.Option, ?> optionsMap = optionMap(blobInfo, options);
return new BlobWriterChannelImpl(options(), blobInfo, optionsMap);
return new BlobWriteChannelImpl(options(), blobInfo, optionsMap);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed 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.
*/

package com.google.gcloud.storage;

import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import com.google.common.collect.ImmutableMap;
import com.google.gcloud.RetryParams;
import com.google.gcloud.spi.StorageRpc;

import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.Before;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Map;
import java.util.Random;
import org.junit.After;

public class BlobReadChannelImplTest {

private static final String BUCKET_NAME = "b";
private static final String BLOB_NAME = "n";
private static final BlobInfo BLOB_INFO = BlobInfo.of(BUCKET_NAME, BLOB_NAME);
private static final Map<StorageRpc.Option, ?> EMPTY_RPC_OPTIONS = ImmutableMap.of();
private static final int DEFAULT_CHUNK_SIZE = 2 * 1024 * 1024;
private static final int CUSTOM_CHUNK_SIZE = 2 * 1024 * 1024;
private static final Random RANDOM = new Random();

private StorageOptions optionsMock;
private StorageRpc storageRpcMock;
private BlobReadChannelImpl reader;

@Before
public void setUp() throws IOException, InterruptedException {
optionsMock = EasyMock.createMock(StorageOptions.class);
storageRpcMock = EasyMock.createMock(StorageRpc.class);
}

@After
public void tearDown() throws Exception {
verify(optionsMock);
verify(storageRpcMock);
}

@Test
public void testCreate() {
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
EasyMock.replay(optionsMock);
EasyMock.replay(storageRpcMock);
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
assertTrue(reader.isOpen());
}

@Test
public void testReadBuffered() throws IOException {
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries());
EasyMock.replay(optionsMock);
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
byte[] result = randomByteArray(DEFAULT_CHUNK_SIZE);
ByteBuffer firstReadBuffer = ByteBuffer.allocate(42);
ByteBuffer secondReadBuffer = ByteBuffer.allocate(42);
EasyMock
.expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
.andReturn(result);
EasyMock.replay(storageRpcMock);
reader.read(firstReadBuffer);
reader.read(secondReadBuffer);
assertArrayEquals(Arrays.copyOf(result, firstReadBuffer.capacity()), firstReadBuffer.array());
assertArrayEquals(
Arrays.copyOfRange(result, firstReadBuffer.capacity(), firstReadBuffer.capacity()
+ secondReadBuffer.capacity()),
secondReadBuffer.array());
}

@Test
public void testReadBig() throws IOException {
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries()).times(2);
EasyMock.replay(optionsMock);
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
reader.chunkSize(CUSTOM_CHUNK_SIZE);
byte[] firstResult = randomByteArray(DEFAULT_CHUNK_SIZE);
byte[] secondResult = randomByteArray(DEFAULT_CHUNK_SIZE);
ByteBuffer firstReadBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
ByteBuffer secondReadBuffer = ByteBuffer.allocate(42);
EasyMock
.expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
.andReturn(firstResult);
EasyMock
.expect(
storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, DEFAULT_CHUNK_SIZE,
CUSTOM_CHUNK_SIZE))
.andReturn(secondResult);
EasyMock.replay(storageRpcMock);
reader.read(firstReadBuffer);
reader.read(secondReadBuffer);
assertArrayEquals(firstResult, firstReadBuffer.array());
assertArrayEquals(Arrays.copyOf(secondResult, secondReadBuffer.capacity()),
secondReadBuffer.array());
}

@Test
public void testReadFinish() throws IOException {
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries());
EasyMock.replay(optionsMock);
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
byte[] result = {};
ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
EasyMock
.expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
.andReturn(result);
EasyMock.replay(storageRpcMock);
assertEquals(-1, reader.read(readBuffer));
}

@Test
public void testSeek() throws IOException {
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries());
EasyMock.replay(optionsMock);
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
reader.seek(42);
byte[] result = randomByteArray(DEFAULT_CHUNK_SIZE);
ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
EasyMock
.expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 42, DEFAULT_CHUNK_SIZE))
.andReturn(result);
EasyMock.replay(storageRpcMock);
reader.read(readBuffer);
assertArrayEquals(result, readBuffer.array());
}

@Test
public void testClose() throws IOException {
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
EasyMock.replay(optionsMock);
EasyMock.replay(storageRpcMock);
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
assertTrue(reader.isOpen());
reader.close();
assertTrue(!reader.isOpen());
}

@Test
public void testReadClosed() {
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
EasyMock.replay(optionsMock);
EasyMock.replay(storageRpcMock);
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
reader.close();
try {
ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
reader.read(readBuffer);
fail("Expected BlobReadChannel read to throw IOException");
} catch (IOException ex) {
// expected
}
}

private static byte[] randomByteArray(int size) {
byte[] byteArray = new byte[size];
RANDOM.nextBytes(byteArray);
return byteArray;
}
}
Loading