Skip to content

Commit 09632ce

Browse files
author
Ajay Kannan
committed
---
yaml --- r: 901 b: refs/heads/master c: 1f617b7 h: refs/heads/master i: 899: 8071796 v: v3
1 parent 7b3fad3 commit 09632ce

11 files changed

Lines changed: 54 additions & 516 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: 1fa53a817ac6c3962ed89645b0ca86e276e73bdb
2+
refs/heads/master: 1f617b77fd77d09f327b909d2302f2f4efc6e0a5
33
refs/heads/travis: 0fa997e2fc9c6b61b2d91e6d163655aae67d44b6
44
refs/heads/gh-pages: 5a10432ecc75f29812e33a8236c900379509fe99

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
public class DatastoreOptionsTest {
3535

3636
private static final String PROJECT_ID = "project_id";
37+
private static final int PORT = LocalGcdHelper.findOpenPort();
3738
private DatastoreRpcFactory datastoreRpcFactory;
3839
private DatastoreRpc datastoreRpc;
3940
private DatastoreOptions.Builder options;
@@ -46,7 +47,7 @@ public void setUp() throws IOException, InterruptedException {
4647
.normalizeDataset(false)
4748
.serviceRpcFactory(datastoreRpcFactory)
4849
.projectId(PROJECT_ID)
49-
.host("http://localhost:" + LocalGcdHelper.PORT);
50+
.host("http://localhost:" + PORT);
5051
EasyMock.expect(datastoreRpcFactory.create(EasyMock.anyObject(DatastoreOptions.class)))
5152
.andReturn(datastoreRpc)
5253
.anyTimes();
@@ -60,7 +61,7 @@ public void testProjectId() throws Exception {
6061

6162
@Test
6263
public void testHost() throws Exception {
63-
assertEquals("http://localhost:" + LocalGcdHelper.PORT, options.build().host());
64+
assertEquals("http://localhost:" + PORT, options.build().host());
6465
}
6566

6667
@Test

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

Lines changed: 5 additions & 56 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

@@ -103,22 +101,20 @@ public class DatastoreTest {
103101
private Datastore datastore;
104102

105103
private static LocalGcdHelper gcdHelper;
106-
107-
@Rule
108-
public ExpectedException thrown = ExpectedException.none();
104+
private static final int PORT = LocalGcdHelper.findOpenPort();
109105

110106
@BeforeClass
111107
public static void beforeClass() throws IOException, InterruptedException {
112-
if (!LocalGcdHelper.isActive(PROJECT_ID)) {
113-
gcdHelper = LocalGcdHelper.start(PROJECT_ID);
108+
if (!LocalGcdHelper.isActive(PROJECT_ID, PORT)) {
109+
gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT);
114110
}
115111
}
116112

117113
@Before
118114
public void setUp() throws IOException, InterruptedException {
119115
options = DatastoreOptions.builder()
120116
.projectId(PROJECT_ID)
121-
.host("http://localhost:" + LocalGcdHelper.PORT)
117+
.host("http://localhost:" + PORT)
122118
.build();
123119
datastore = DatastoreFactory.instance().get(options);
124120
StructuredQuery<Key> query = Query.keyQueryBuilder().build();
@@ -640,7 +636,7 @@ public void testKeyFactory() {
640636
}
641637

642638
@Test
643-
public void testRetryableException() throws Exception {
639+
public void testRetires() throws Exception {
644640
DatastoreV1.LookupRequest requestPb =
645641
DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build();
646642
DatastoreV1.LookupResponse responsePb = DatastoreV1.LookupResponse.newBuilder()
@@ -662,51 +658,4 @@ public void testRetryableException() throws Exception {
662658
assertEquals(ENTITY1, entity);
663659
EasyMock.verify(rpcFactoryMock, rpcMock);
664660
}
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-
}
712661
}

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

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.math.BigInteger;
3636
import java.net.HttpURLConnection;
3737
import java.net.MalformedURLException;
38+
import java.net.ServerSocket;
3839
import java.net.URL;
3940
import java.nio.channels.Channels;
4041
import java.nio.channels.ReadableByteChannel;
@@ -68,9 +69,10 @@ public class LocalGcdHelper {
6869
private final String projectId;
6970
private Path gcdPath;
7071
private ProcessStreamReader processReader;
72+
private final int port;
7173

7274
public static final String DEFAULT_PROJECT_ID = "projectid1";
73-
public static final int PORT = 8080;
75+
private static final int DEFAULT_PORT = 8080;
7476
private static final String GCD_VERSION = "v1beta2";
7577
private static final String GCD_BUILD = "rev1-2.1.2b";
7678
private static final String GCD_BASENAME = "gcd-" + GCD_VERSION + "-" + GCD_BUILD;
@@ -94,6 +96,17 @@ public class LocalGcdHelper {
9496
}
9597
}
9698

99+
public static int findOpenPort() {
100+
int port;
101+
try (ServerSocket temp_socket = new ServerSocket(0)) {
102+
port = temp_socket.getLocalPort();
103+
temp_socket.close();
104+
} catch (IOException e) {
105+
port = DEFAULT_PORT;
106+
}
107+
return port;
108+
}
109+
97110
private static Path installedGcdPath() {
98111
String gcloudExecutableName;
99112
if (isWindows()) {
@@ -283,8 +296,9 @@ public static CommandWrapper create() {
283296
}
284297
}
285298

286-
public LocalGcdHelper(String projectId) {
299+
public LocalGcdHelper(String projectId, int port) {
287300
this.projectId = projectId;
301+
this.port = port;
288302
}
289303

290304
/**
@@ -297,7 +311,7 @@ public LocalGcdHelper(String projectId) {
297311
*/
298312
public void start() throws IOException, InterruptedException {
299313
// send a quick request in case we have a hanging process from a previous run
300-
sendQuitRequest();
314+
sendQuitRequest(port);
301315
// Each run is associated with its own folder that is deleted once test completes.
302316
gcdPath = Files.createTempDirectory("gcd");
303317
File gcdFolder = gcdPath.toFile();
@@ -379,13 +393,12 @@ private void startGcd(Path executablePath) throws IOException, InterruptedExcept
379393
if (log.isLoggable(Level.FINE)) {
380394
log.log(Level.FINE, "Starting datastore emulator for the project: {0}", projectId);
381395
}
382-
Process startProcess =
383-
CommandWrapper.create()
384-
.command(gcdAbsolutePath.toString(), "start", "--testing", "--allow_remote_shutdown",
385-
projectId)
386-
.directory(gcdPath)
387-
.redirectErrorStream()
388-
.start();
396+
Process startProcess = CommandWrapper.create()
397+
.command(gcdAbsolutePath.toString(), "start", "--testing", "--allow_remote_shutdown",
398+
"--port=" + Integer.toString(port), projectId)
399+
.directory(gcdPath)
400+
.redirectErrorStream()
401+
.start();
389402
processReader = ProcessStreamReader.start(startProcess, "Dev App Server is now running");
390403
}
391404

@@ -419,9 +432,9 @@ private static void extractFile(ZipInputStream zipIn, File filePath) throws IOEx
419432
}
420433
}
421434

422-
public static void sendQuitRequest() {
435+
public static void sendQuitRequest(int port) {
423436
try {
424-
URL url = new URL("http", "localhost", PORT, "/_ah/admin/quit");
437+
URL url = new URL("http", "localhost", port, "/_ah/admin/quit");
425438
HttpURLConnection con = (HttpURLConnection) url.openConnection();
426439
con.setRequestMethod("POST");
427440
con.setDoOutput(true);
@@ -439,7 +452,7 @@ public static void sendQuitRequest() {
439452
}
440453

441454
public void stop() throws IOException, InterruptedException {
442-
sendQuitRequest();
455+
sendQuitRequest(port);
443456
if (processReader != null) {
444457
processReader.terminate();
445458
}
@@ -468,8 +481,8 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
468481
});
469482
}
470483

471-
public static LocalGcdHelper start(String projectId) throws IOException, InterruptedException {
472-
LocalGcdHelper helper = new LocalGcdHelper(projectId);
484+
public static LocalGcdHelper start(String projectId, int port) throws IOException, InterruptedException {
485+
LocalGcdHelper helper = new LocalGcdHelper(projectId, port);
473486
helper.start();
474487
return helper;
475488
}
@@ -478,15 +491,15 @@ public static void main(String... args) throws IOException, InterruptedException
478491
if (args.length == 1) {
479492
switch (args[0]) {
480493
case "START":
481-
if (!isActive(DEFAULT_PROJECT_ID)) {
482-
LocalGcdHelper helper = start(DEFAULT_PROJECT_ID);
494+
if (!isActive(DEFAULT_PROJECT_ID, DEFAULT_PORT)) {
495+
LocalGcdHelper helper = start(DEFAULT_PROJECT_ID, DEFAULT_PORT);
483496
try (FileWriter writer = new FileWriter(".local_gcd_helper")) {
484497
writer.write(helper.gcdPath.toAbsolutePath().toString());
485498
}
486499
}
487500
return;
488501
case "STOP":
489-
sendQuitRequest();
502+
sendQuitRequest(DEFAULT_PORT);
490503
File file = new File(".local_gcd_helper");
491504
if (file.exists()) {
492505
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
@@ -503,9 +516,9 @@ public static void main(String... args) throws IOException, InterruptedException
503516
throw new RuntimeException("expecting only START | STOP");
504517
}
505518

506-
public static boolean isActive(String projectId) {
519+
public static boolean isActive(String projectId, int port) {
507520
try {
508-
StringBuilder urlBuilder = new StringBuilder("http://localhost:").append(PORT);
521+
StringBuilder urlBuilder = new StringBuilder("http://localhost:").append(port);
509522
urlBuilder.append("/datastore/v1beta2/datasets/").append(projectId).append("/lookup");
510523
URL url = new URL(urlBuilder.toString());
511524
try (BufferedReader reader =

trunk/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import com.google.api.client.http.HttpResponse;
4444
import com.google.api.client.http.HttpResponseException;
4545
import com.google.api.client.http.HttpTransport;
46-
import com.google.api.client.http.InputStreamContent;
4746
import com.google.api.client.http.json.JsonHttpContent;
4847
import com.google.api.client.json.JsonFactory;
4948
import com.google.api.client.json.jackson.JacksonFactory;
@@ -64,7 +63,6 @@
6463

6564
import java.io.ByteArrayOutputStream;
6665
import java.io.IOException;
67-
import java.io.InputStream;
6866
import java.util.ArrayList;
6967
import java.util.List;
7068
import java.util.Map;
@@ -121,14 +119,13 @@ public Bucket create(Bucket bucket, Map<Option, ?> options) throws StorageExcept
121119
}
122120

123121
@Override
124-
public StorageObject create(StorageObject storageObject, final InputStream content,
122+
public StorageObject create(StorageObject storageObject, final byte[] content,
125123
Map<Option, ?> options) throws StorageException {
126124
try {
127-
Storage.Objects.Insert insert = storage.objects()
125+
return storage.objects()
128126
.insert(storageObject.getBucket(), storageObject,
129-
new InputStreamContent(storageObject.getContentType(), content));
130-
insert.getMediaHttpUploader().setDirectUploadEnabled(true);
131-
return insert.setProjection(DEFAULT_PROJECTION)
127+
new ByteArrayContent(storageObject.getContentType(), content))
128+
.setProjection(DEFAULT_PROJECTION)
132129
.setPredefinedAcl(PREDEFINED_ACL.getString(options))
133130
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options))
134131
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options))
@@ -524,3 +521,4 @@ public String open(StorageObject object, Map<Option, ?> options)
524521
}
525522
}
526523
}
524+

trunk/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.common.collect.ImmutableMap;
2323
import com.google.gcloud.storage.StorageException;
2424

25-
import java.io.InputStream;
2625
import java.util.List;
2726
import java.util.Map;
2827

@@ -129,7 +128,7 @@ public BatchResponse(Map<StorageObject, Tuple<Boolean, StorageException>> delete
129128

130129
Bucket create(Bucket bucket, Map<Option, ?> options) throws StorageException;
131130

132-
StorageObject create(StorageObject object, InputStream content, Map<Option, ?> options)
131+
StorageObject create(StorageObject object, byte[] content, Map<Option, ?> options)
133132
throws StorageException;
134133

135134
Tuple<String, Iterable<Bucket>> list(Map<Option, ?> options) throws StorageException;

trunk/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriteChannelImpl.java renamed to trunk/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriterChannelImpl.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 BlobWriteChannelImpl implements BlobWriteChannel {
35+
class BlobWriterChannelImpl 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 BlobWriteChannelImpl implements BlobWriteChannel {
5050
private transient StorageRpc storageRpc;
5151
private transient StorageObject storageObject;
5252

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

6161
private void writeObject(ObjectOutputStream out) throws IOException {

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

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.google.gcloud.Service;
2626
import com.google.gcloud.spi.StorageRpc;
2727

28-
import java.io.InputStream;
2928
import java.io.Serializable;
3029
import java.net.URL;
3130
import java.util.Arrays;
@@ -494,31 +493,13 @@ public static Builder builder() {
494493
BucketInfo create(BucketInfo bucketInfo, BucketTargetOption... options);
495494

496495
/**
497-
* Create a new blob with no content.
498-
*
499-
* @return a complete blob information.
500-
* @throws StorageException upon failure
501-
*/
502-
BlobInfo create(BlobInfo blobInfo, BlobTargetOption... options);
503-
504-
/**
505-
* Create a new blob. Direct upload is used to upload {@code content}. For large content,
506-
* {@link #writer} is recommended as it uses resumable upload.
496+
* Create a new blob.
507497
*
508498
* @return a complete blob information.
509499
* @throws StorageException upon failure
510500
*/
511501
BlobInfo create(BlobInfo blobInfo, byte[] content, BlobTargetOption... options);
512502

513-
/**
514-
* Create a new blob. Direct upload is used to upload {@code content}. For large content,
515-
* {@link #writer} is recommended as it uses resumable upload.
516-
*
517-
* @return a complete blob information.
518-
* @throws StorageException upon failure
519-
*/
520-
BlobInfo create(BlobInfo blobInfo, InputStream content, BlobTargetOption... options);
521-
522503
/**
523504
* Return the requested bucket or {@code null} if not found.
524505
*

0 commit comments

Comments
 (0)