|
| 1 | +/* |
| 2 | + * Copyright 2016 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.cloud.datastore.testing; |
| 18 | + |
| 19 | +import com.google.cloud.RetryParams; |
| 20 | +import com.google.cloud.datastore.Datastore; |
| 21 | +import com.google.cloud.datastore.DatastoreOptions; |
| 22 | +import com.google.cloud.datastore.Key; |
| 23 | +import com.google.cloud.datastore.Query; |
| 24 | +import com.google.cloud.datastore.QueryResults; |
| 25 | +import com.google.cloud.datastore.StructuredQuery; |
| 26 | + |
| 27 | +import java.util.UUID; |
| 28 | + |
| 29 | +/** |
| 30 | + * Utility to create a remote datastore configuration for testing. Datastore options can be obtained |
| 31 | + * via the {@link #options()} method. Returned options use a randomly generated namespace |
| 32 | + * ({@link DatastoreOptions#namespace()}) that can be used to run the tests. Once the tests are run, |
| 33 | + * all entities in the namespace can be deleted using {@link #deleteNamespace()}. Returned options |
| 34 | + * also have custom {@link DatastoreOptions#retryParams()}: {@link RetryParams#retryMaxAttempts()} |
| 35 | + * is {@code 10}, {@link RetryParams#retryMinAttempts()} is {@code 6}, |
| 36 | + * {@link RetryParams#maxRetryDelayMillis()} is {@code 30000}, |
| 37 | + * {@link RetryParams#totalRetryPeriodMillis()} is {@code 120000} and |
| 38 | + * {@link RetryParams#initialRetryDelayMillis()} is {@code 250}. |
| 39 | + * {@link DatastoreOptions#connectTimeout()} and {@link DatastoreOptions#readTimeout()} are both set |
| 40 | + * to {@code 60000}. |
| 41 | + */ |
| 42 | +public class RemoteDatastoreHelper { |
| 43 | + |
| 44 | + private final DatastoreOptions options; |
| 45 | + private final Datastore datastore; |
| 46 | + private final String namespace; |
| 47 | + |
| 48 | + private RemoteDatastoreHelper(DatastoreOptions options) { |
| 49 | + this.options = options; |
| 50 | + this.datastore = options.service(); |
| 51 | + this.namespace = options.namespace(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns a {@link DatastoreOptions} object to be used for testing. The options are associated |
| 56 | + * to a randomly generated namespace. |
| 57 | + */ |
| 58 | + public DatastoreOptions options() { |
| 59 | + return options; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Deletes all entities in the namespace associated with this {@link RemoteDatastoreHelper}. |
| 64 | + */ |
| 65 | + public void deleteNamespace() { |
| 66 | + StructuredQuery<Key> query = Query.keyQueryBuilder().namespace(namespace).build(); |
| 67 | + QueryResults<Key> keys = datastore.run(query); |
| 68 | + while (keys.hasNext()) { |
| 69 | + datastore.delete(keys.next()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Creates a {@code RemoteStorageHelper} object. |
| 75 | + */ |
| 76 | + public static RemoteDatastoreHelper create() { |
| 77 | + DatastoreOptions datastoreOption = DatastoreOptions.builder() |
| 78 | + .namespace(UUID.randomUUID().toString()) |
| 79 | + .retryParams(retryParams()) |
| 80 | + .connectTimeout(60000) |
| 81 | + .readTimeout(60000) |
| 82 | + .build(); |
| 83 | + return new RemoteDatastoreHelper(datastoreOption); |
| 84 | + } |
| 85 | + |
| 86 | + private static RetryParams retryParams() { |
| 87 | + return RetryParams.builder() |
| 88 | + .retryMaxAttempts(10) |
| 89 | + .retryMinAttempts(6) |
| 90 | + .maxRetryDelayMillis(30000) |
| 91 | + .totalRetryPeriodMillis(120000) |
| 92 | + .initialRetryDelayMillis(250) |
| 93 | + .build(); |
| 94 | + } |
| 95 | +} |
0 commit comments