Skip to content

Commit d9ad0d1

Browse files
committed
---
yaml --- r: 893 b: refs/heads/master c: 575fa1f h: refs/heads/master i: 891: fc8855a v: v3
1 parent e382a0c commit d9ad0d1

5 files changed

Lines changed: 363 additions & 55 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
2-
refs/heads/master: e13fc604546890faa0235bc154febf5d17abb288
2+
refs/heads/master: 575fa1f50ee34aa68d277cd4b09a054982b871cc
33
refs/heads/travis: 0fa997e2fc9c6b61b2d91e6d163655aae67d44b6
44
refs/heads/gh-pages: 5a10432ecc75f29812e33a8236c900379509fe99

trunk/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@
4141
import org.junit.AfterClass;
4242
import org.junit.Before;
4343
import org.junit.BeforeClass;
44-
import org.junit.Rule;
4544
import org.junit.Test;
46-
import org.junit.rules.ExpectedException;
4745
import org.junit.runner.RunWith;
4846
import org.junit.runners.JUnit4;
4947

@@ -104,9 +102,6 @@ public class DatastoreTest {
104102

105103
private static LocalGcdHelper gcdHelper;
106104

107-
@Rule
108-
public ExpectedException thrown = ExpectedException.none();
109-
110105
@BeforeClass
111106
public static void beforeClass() throws IOException, InterruptedException {
112107
if (!LocalGcdHelper.isActive(PROJECT_ID)) {
@@ -640,7 +635,7 @@ public void testKeyFactory() {
640635
}
641636

642637
@Test
643-
public void testRetryableException() throws Exception {
638+
public void testRetires() throws Exception {
644639
DatastoreV1.LookupRequest requestPb =
645640
DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build();
646641
DatastoreV1.LookupResponse responsePb = DatastoreV1.LookupResponse.newBuilder()
@@ -662,51 +657,4 @@ public void testRetryableException() throws Exception {
662657
assertEquals(ENTITY1, entity);
663658
EasyMock.verify(rpcFactoryMock, rpcMock);
664659
}
665-
666-
@Test
667-
public void testNonRetryableException() throws Exception {
668-
DatastoreV1.LookupRequest requestPb =
669-
DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build();
670-
DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class);
671-
DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class);
672-
EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class)))
673-
.andReturn(rpcMock);
674-
EasyMock.expect(rpcMock.lookup(requestPb))
675-
.andThrow(new DatastoreRpc.DatastoreRpcException(Reason.PERMISSION_DENIED))
676-
.times(1);
677-
EasyMock.replay(rpcFactoryMock, rpcMock);
678-
RetryParams retryParams = RetryParams.builder().retryMinAttempts(2).build();
679-
DatastoreOptions options = this.options.toBuilder()
680-
.retryParams(retryParams)
681-
.serviceRpcFactory(rpcFactoryMock)
682-
.build();
683-
Datastore datastore = DatastoreFactory.instance().get(options);
684-
thrown.expect(DatastoreException.class);
685-
thrown.expectMessage(Reason.PERMISSION_DENIED.description());
686-
datastore.get(KEY1);
687-
EasyMock.verify(rpcFactoryMock, rpcMock);
688-
}
689-
690-
@Test
691-
public void testRuntimeException() throws Exception {
692-
DatastoreV1.LookupRequest requestPb =
693-
DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build();
694-
DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class);
695-
DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class);
696-
EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class)))
697-
.andReturn(rpcMock);
698-
String exceptionMessage = "Artificial runtime exception";
699-
EasyMock.expect(rpcMock.lookup(requestPb))
700-
.andThrow(new RuntimeException(exceptionMessage));
701-
EasyMock.replay(rpcFactoryMock, rpcMock);
702-
DatastoreOptions options = this.options.toBuilder()
703-
.retryParams(RetryParams.getDefaultInstance())
704-
.serviceRpcFactory(rpcFactoryMock)
705-
.build();
706-
Datastore datastore = DatastoreFactory.instance().get(options);
707-
thrown.expect(DatastoreException.class);
708-
thrown.expectMessage(exceptionMessage);
709-
datastore.get(KEY1);
710-
EasyMock.verify(rpcFactoryMock, rpcMock);
711-
}
712660
}

trunk/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriterChannelImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class BlobWriterChannelImpl implements BlobWriteChannel {
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 {
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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.junit.Assert.assertArrayEquals;
20+
import static org.junit.Assert.assertTrue;
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.fail;
23+
24+
import com.google.common.collect.ImmutableMap;
25+
import com.google.gcloud.RetryParams;
26+
import com.google.gcloud.spi.StorageRpc;
27+
28+
import org.easymock.EasyMock;
29+
import org.junit.Test;
30+
import org.junit.Before;
31+
32+
import java.io.IOException;
33+
import java.nio.ByteBuffer;
34+
import java.util.Arrays;
35+
import java.util.Map;
36+
import java.util.Random;
37+
38+
public class BlobReadChannelTest {
39+
40+
private static final String BUCKET_NAME = "b";
41+
private static final String BLOB_NAME = "n";
42+
private static final BlobInfo BLOB_INFO = BlobInfo.of(BUCKET_NAME, BLOB_NAME);
43+
private static final Map<StorageRpc.Option, ?> EMPTY_RPC_OPTIONS = ImmutableMap.of();
44+
private static final int DEFAULT_CHUNK_SIZE = 2 * 1024 * 1024;
45+
private static final int CUSTOM_CHUNK_SIZE = 2 * 1024 * 1024;
46+
private static final Random RANDOM = new Random();
47+
48+
private StorageOptions optionsMock;
49+
private StorageRpc storageRpcMock;
50+
private BlobReadChannelImpl reader;
51+
52+
@Before
53+
public void setUp() throws IOException, InterruptedException {
54+
optionsMock = EasyMock.createMock(StorageOptions.class);
55+
storageRpcMock = EasyMock.createMock(StorageRpc.class);
56+
}
57+
58+
@Test
59+
public void testCreate() {
60+
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
61+
EasyMock.replay(optionsMock);
62+
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
63+
assertTrue(reader.isOpen());
64+
}
65+
66+
@Test
67+
public void testReadSmall() throws IOException {
68+
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
69+
EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries());
70+
EasyMock.replay(optionsMock);
71+
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
72+
byte[] result = randomByteArray(DEFAULT_CHUNK_SIZE);
73+
ByteBuffer readBuffer = ByteBuffer.allocate(42);
74+
EasyMock
75+
.expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
76+
.andReturn(result);
77+
EasyMock.replay(storageRpcMock);
78+
reader.read(readBuffer);
79+
assertArrayEquals(Arrays.copyOf(result, readBuffer.capacity()), readBuffer.array());
80+
}
81+
82+
@Test
83+
public void testReadBuffered() throws IOException {
84+
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
85+
EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries());
86+
EasyMock.replay(optionsMock);
87+
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
88+
byte[] result = randomByteArray(DEFAULT_CHUNK_SIZE);
89+
ByteBuffer firstReadBuffer = ByteBuffer.allocate(42);
90+
ByteBuffer secondReadBuffer = ByteBuffer.allocate(42);
91+
EasyMock
92+
.expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
93+
.andReturn(result);
94+
EasyMock.replay(storageRpcMock);
95+
reader.read(firstReadBuffer);
96+
reader.read(secondReadBuffer);
97+
assertArrayEquals(Arrays.copyOf(result, firstReadBuffer.capacity()), firstReadBuffer.array());
98+
assertArrayEquals(
99+
Arrays.copyOfRange(result, firstReadBuffer.capacity(), firstReadBuffer.capacity()
100+
+ secondReadBuffer.capacity()),
101+
secondReadBuffer.array());
102+
}
103+
104+
@Test
105+
public void testReadBig() throws IOException {
106+
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
107+
EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries()).times(2);
108+
EasyMock.replay(optionsMock);
109+
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
110+
reader.chunkSize(CUSTOM_CHUNK_SIZE);
111+
byte[] firstResult = randomByteArray(DEFAULT_CHUNK_SIZE);
112+
byte[] secondResult = randomByteArray(DEFAULT_CHUNK_SIZE);
113+
ByteBuffer firstReadBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
114+
ByteBuffer secondReadBuffer = ByteBuffer.allocate(42);
115+
EasyMock
116+
.expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
117+
.andReturn(firstResult);
118+
EasyMock
119+
.expect(
120+
storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, DEFAULT_CHUNK_SIZE,
121+
CUSTOM_CHUNK_SIZE))
122+
.andReturn(secondResult);
123+
EasyMock.expectLastCall();
124+
EasyMock.replay(storageRpcMock);
125+
reader.read(firstReadBuffer);
126+
reader.read(secondReadBuffer);
127+
assertArrayEquals(firstResult, firstReadBuffer.array());
128+
assertArrayEquals(Arrays.copyOf(secondResult, secondReadBuffer.capacity()),
129+
secondReadBuffer.array());
130+
}
131+
132+
@Test
133+
public void testReadFinish() throws IOException {
134+
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
135+
EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries());
136+
EasyMock.replay(optionsMock);
137+
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
138+
byte[] result = {};
139+
ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
140+
EasyMock
141+
.expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
142+
.andReturn(result);
143+
EasyMock.replay(storageRpcMock);
144+
assertEquals(-1, reader.read(readBuffer));
145+
}
146+
147+
@Test
148+
public void testSeek() throws IOException {
149+
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
150+
EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries());
151+
EasyMock.replay(optionsMock);
152+
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
153+
reader.seek(42);
154+
byte[] result = randomByteArray(DEFAULT_CHUNK_SIZE);
155+
ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
156+
EasyMock
157+
.expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 42, DEFAULT_CHUNK_SIZE))
158+
.andReturn(result);
159+
EasyMock.replay(storageRpcMock);
160+
reader.read(readBuffer);
161+
assertArrayEquals(result, readBuffer.array());
162+
}
163+
164+
@Test
165+
public void testClose() throws IOException {
166+
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
167+
EasyMock.replay(optionsMock);
168+
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
169+
assertTrue(reader.isOpen());
170+
reader.close();
171+
assertTrue(!reader.isOpen());
172+
}
173+
174+
@Test
175+
public void testReadClosed() {
176+
EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock);
177+
EasyMock.replay(optionsMock);
178+
reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS);
179+
reader.close();
180+
try {
181+
ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
182+
reader.read(readBuffer);
183+
fail("Expected BlobReadChannel read to throw IOException");
184+
} catch (IOException ex) {
185+
// expected
186+
}
187+
}
188+
189+
private static byte[] randomByteArray(int size) {
190+
byte[] byteArray = new byte[size];
191+
RANDOM.nextBytes(byteArray);
192+
return byteArray;
193+
}
194+
}

0 commit comments

Comments
 (0)