Skip to content

Commit 5d8d123

Browse files
committed
Merge pull request #188 from mziccard/test-storage-blob-channels
Add tests for BlobWriteChannel and BlobReadChannel
2 parents e13fc60 + 7145030 commit 5d8d123

File tree

4 files changed

+392
-4
lines changed

4 files changed

+392
-4
lines changed

gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriterChannelImpl.java renamed to gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriteChannelImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
/**
3333
* Default implementation for BlobWriteChannel.
3434
*/
35-
class BlobWriterChannelImpl implements BlobWriteChannel {
35+
class BlobWriteChannelImpl implements BlobWriteChannel {
3636

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

53-
BlobWriterChannelImpl(StorageOptions options, BlobInfo blobInfo,
53+
BlobWriteChannelImpl(StorageOptions options, BlobInfo blobInfo,
5454
Map<StorageRpc.Option, ?> optionsMap) {
5555
this.options = options;
5656
this.blobInfo = blobInfo;
5757
initTransients();
58-
uploadId = options.storageRpc().open(storageObject, optionsMap);
58+
uploadId = storageRpc.open(storageObject, optionsMap);
5959
}
6060

6161
private void writeObject(ObjectOutputStream out) throws IOException {

gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ public BlobReadChannel reader(String bucket, String blob, BlobSourceOption... op
443443
@Override
444444
public BlobWriteChannel writer(BlobInfo blobInfo, BlobTargetOption... options) {
445445
final Map<StorageRpc.Option, ?> optionsMap = optionMap(blobInfo, options);
446-
return new BlobWriterChannelImpl(options(), blobInfo, optionsMap);
446+
return new BlobWriteChannelImpl(options(), blobInfo, optionsMap);
447447
}
448448

449449
@Override
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud.storage;
18+
19+
import static org.easymock.EasyMock.verify;
20+
import static org.junit.Assert.assertArrayEquals;
21+
import static org.junit.Assert.assertTrue;
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.fail;
24+
25+
import com.google.common.collect.ImmutableMap;
26+
import com.google.gcloud.RetryParams;
27+
import com.google.gcloud.spi.StorageRpc;
28+
29+
import org.easymock.EasyMock;
30+
import org.junit.Test;
31+
import org.junit.Before;
32+
33+
import java.io.IOException;
34+
import java.nio.ByteBuffer;
35+
import java.util.Arrays;
36+
import java.util.Map;
37+
import java.util.Random;
38+
import org.junit.After;
39+
40+
public class BlobReadChannelImplTest {
41+
42+
private static final String BUCKET_NAME = "b";
43+
private static final String BLOB_NAME = "n";
44+
private static final BlobInfo BLOB_INFO = BlobInfo.of(BUCKET_NAME, BLOB_NAME);
45+
private static final Map<StorageRpc.Option, ?> EMPTY_RPC_OPTIONS = ImmutableMap.of();
46+
private static final int DEFAULT_CHUNK_SIZE = 2 * 1024 * 1024;
47+
private static final int CUSTOM_CHUNK_SIZE = 2 * 1024 * 1024;
48+
private static final Random RANDOM = new Random();
49+
50+
private StorageOptions optionsMock;
51+
private StorageRpc storageRpcMock;
52+
private BlobReadChannelImpl reader;
53+
54+
@Before
55+
public void setUp() throws IOException, InterruptedException {
56+
optionsMock = EasyMock.createMock(StorageOptions.class);
57+
storageRpcMock = EasyMock.createMock(StorageRpc.class);
58+
}
59+
60+
@After
61+
public void tearDown() throws Exception {
62+
verify(optionsMock);
63+
verify(storageRpcMock);
64+
}
65+
66+
@Test
67+
public void testCreate() {
68+
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
69+
EasyMock.replay(optionsMock);
70+
EasyMock.replay(storageRpcMock);
71+
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
72+
assertTrue(reader.isOpen());
73+
}
74+
75+
@Test
76+
public void testReadBuffered() throws IOException {
77+
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
78+
EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries());
79+
EasyMock.replay(optionsMock);
80+
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
81+
byte[] result = randomByteArray(DEFAULT_CHUNK_SIZE);
82+
ByteBuffer firstReadBuffer = ByteBuffer.allocate(42);
83+
ByteBuffer secondReadBuffer = ByteBuffer.allocate(42);
84+
EasyMock
85+
.expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
86+
.andReturn(result);
87+
EasyMock.replay(storageRpcMock);
88+
reader.read(firstReadBuffer);
89+
reader.read(secondReadBuffer);
90+
assertArrayEquals(Arrays.copyOf(result, firstReadBuffer.capacity()), firstReadBuffer.array());
91+
assertArrayEquals(
92+
Arrays.copyOfRange(result, firstReadBuffer.capacity(), firstReadBuffer.capacity()
93+
+ secondReadBuffer.capacity()),
94+
secondReadBuffer.array());
95+
}
96+
97+
@Test
98+
public void testReadBig() throws IOException {
99+
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
100+
EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries()).times(2);
101+
EasyMock.replay(optionsMock);
102+
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
103+
reader.chunkSize(CUSTOM_CHUNK_SIZE);
104+
byte[] firstResult = randomByteArray(DEFAULT_CHUNK_SIZE);
105+
byte[] secondResult = randomByteArray(DEFAULT_CHUNK_SIZE);
106+
ByteBuffer firstReadBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
107+
ByteBuffer secondReadBuffer = ByteBuffer.allocate(42);
108+
EasyMock
109+
.expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
110+
.andReturn(firstResult);
111+
EasyMock
112+
.expect(
113+
storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, DEFAULT_CHUNK_SIZE,
114+
CUSTOM_CHUNK_SIZE))
115+
.andReturn(secondResult);
116+
EasyMock.replay(storageRpcMock);
117+
reader.read(firstReadBuffer);
118+
reader.read(secondReadBuffer);
119+
assertArrayEquals(firstResult, firstReadBuffer.array());
120+
assertArrayEquals(Arrays.copyOf(secondResult, secondReadBuffer.capacity()),
121+
secondReadBuffer.array());
122+
}
123+
124+
@Test
125+
public void testReadFinish() throws IOException {
126+
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
127+
EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries());
128+
EasyMock.replay(optionsMock);
129+
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
130+
byte[] result = {};
131+
ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
132+
EasyMock
133+
.expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
134+
.andReturn(result);
135+
EasyMock.replay(storageRpcMock);
136+
assertEquals(-1, reader.read(readBuffer));
137+
}
138+
139+
@Test
140+
public void testSeek() throws IOException {
141+
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
142+
EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries());
143+
EasyMock.replay(optionsMock);
144+
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
145+
reader.seek(42);
146+
byte[] result = randomByteArray(DEFAULT_CHUNK_SIZE);
147+
ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
148+
EasyMock
149+
.expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 42, DEFAULT_CHUNK_SIZE))
150+
.andReturn(result);
151+
EasyMock.replay(storageRpcMock);
152+
reader.read(readBuffer);
153+
assertArrayEquals(result, readBuffer.array());
154+
}
155+
156+
@Test
157+
public void testClose() throws IOException {
158+
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
159+
EasyMock.replay(optionsMock);
160+
EasyMock.replay(storageRpcMock);
161+
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
162+
assertTrue(reader.isOpen());
163+
reader.close();
164+
assertTrue(!reader.isOpen());
165+
}
166+
167+
@Test
168+
public void testReadClosed() {
169+
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
170+
EasyMock.replay(optionsMock);
171+
EasyMock.replay(storageRpcMock);
172+
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
173+
reader.close();
174+
try {
175+
ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
176+
reader.read(readBuffer);
177+
fail("Expected BlobReadChannel read to throw IOException");
178+
} catch (IOException ex) {
179+
// expected
180+
}
181+
}
182+
183+
private static byte[] randomByteArray(int size) {
184+
byte[] byteArray = new byte[size];
185+
RANDOM.nextBytes(byteArray);
186+
return byteArray;
187+
}
188+
}

0 commit comments

Comments
 (0)