Skip to content

Commit cd23c67

Browse files
committed
---
yaml --- r: 6171 b: refs/heads/tswast-patch-1 c: 1fa53a8 h: refs/heads/master i: 6169: 38c5023 6167: 4920fb6
1 parent a5ca670 commit cd23c67

6 files changed

Lines changed: 446 additions & 6 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: 86065a7fc7d130977a40b99b2bf77d6889d84418
60+
refs/heads/tswast-patch-1: 1fa53a817ac6c3962ed89645b0ca86e276e73bdb
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b

branches/tswast-patch-1/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
import org.junit.AfterClass;
4242
import org.junit.Before;
4343
import org.junit.BeforeClass;
44+
import org.junit.Rule;
4445
import org.junit.Test;
46+
import org.junit.rules.ExpectedException;
4547
import org.junit.runner.RunWith;
4648
import org.junit.runners.JUnit4;
4749

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

103105
private static LocalGcdHelper gcdHelper;
104106

107+
@Rule
108+
public ExpectedException thrown = ExpectedException.none();
109+
105110
@BeforeClass
106111
public static void beforeClass() throws IOException, InterruptedException {
107112
if (!LocalGcdHelper.isActive(PROJECT_ID)) {
@@ -635,7 +640,7 @@ public void testKeyFactory() {
635640
}
636641

637642
@Test
638-
public void testRetires() throws Exception {
643+
public void testRetryableException() throws Exception {
639644
DatastoreV1.LookupRequest requestPb =
640645
DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build();
641646
DatastoreV1.LookupResponse responsePb = DatastoreV1.LookupResponse.newBuilder()
@@ -657,4 +662,51 @@ public void testRetires() throws Exception {
657662
assertEquals(ENTITY1, entity);
658663
EasyMock.verify(rpcFactoryMock, rpcMock);
659664
}
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+
}
660712
}

branches/tswast-patch-1/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriterChannelImpl.java renamed to branches/tswast-patch-1/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 {

branches/tswast-patch-1/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
@@ -458,7 +458,7 @@ public BlobReadChannel reader(String bucket, String blob, BlobSourceOption... op
458458
@Override
459459
public BlobWriteChannel writer(BlobInfo blobInfo, BlobTargetOption... options) {
460460
final Map<StorageRpc.Option, ?> optionsMap = optionMap(blobInfo, options);
461-
return new BlobWriterChannelImpl(options(), blobInfo, optionsMap);
461+
return new BlobWriteChannelImpl(options(), blobInfo, optionsMap);
462462
}
463463

464464
@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)