Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce java.time methods and variables #1671

Merged
merged 6 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@
*/
package com.google.cloud.datastore.models;

import static com.google.api.gax.util.TimeConversionUtils.toThreetenDuration;

import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.api.core.ObsoleteApi;
import com.google.cloud.Structs;
import com.google.common.base.Objects;
import java.util.Map;
import org.threeten.bp.Duration;

/** Model class for {@link com.google.datastore.v1.ExecutionStats} */
@BetaApi
public class ExecutionStats {
private final long resultsReturned;
private final Duration executionDuration;
private final java.time.Duration executionDuration;
private final long readOperations;
private final Map<String, Object> debugStats;

@InternalApi
public ExecutionStats(com.google.datastore.v1.ExecutionStats proto) {
this.resultsReturned = proto.getResultsReturned();
this.executionDuration = Duration.ofNanos(proto.getExecutionDuration().getNanos());
this.executionDuration = java.time.Duration.ofNanos(proto.getExecutionDuration().getNanos());
this.readOperations = proto.getReadOperations();
this.debugStats = Structs.asMap(proto.getDebugStats());
}
Expand All @@ -51,8 +53,14 @@ public Map<String, Object> getDebugStats() {
return debugStats;
}

/** This method is obsolete. Use {@link #getExecutionDurationJavaTime()} instead. */
@ObsoleteApi("Use getExecutionDurationJavaTime() instead")
public org.threeten.bp.Duration getExecutionDuration() {
return toThreetenDuration(getExecutionDurationJavaTime());
}

/** Returns the total time to execute the query in the backend. */
public Duration getExecutionDuration() {
public java.time.Duration getExecutionDurationJavaTime() {
return executionDuration;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package com.google.cloud.datastore.testing;

import static com.google.api.gax.util.TimeConversionUtils.toJavaTimeDuration;
import static com.google.common.base.MoreObjects.firstNonNull;

import com.google.api.core.InternalApi;
import com.google.api.core.ObsoleteApi;
import com.google.cloud.NoCredentials;
import com.google.cloud.ServiceOptions;
import com.google.cloud.datastore.DatastoreOptions;
Expand All @@ -38,7 +40,6 @@
import java.util.UUID;
import java.util.concurrent.TimeoutException;
import java.util.logging.Logger;
import org.threeten.bp.Duration;

/**
* Utility to start and stop local Google Cloud Datastore emulators.
Expand Down Expand Up @@ -307,6 +308,14 @@ public void reset() throws IOException {
sendPostRequest("/reset");
}

/** This method is obsolete. Use {@link #stopDuration(java.time.Duration)} instead */
@ObsoleteApi("Use stopDuration(java.time.Duration) instead")
@Override
public void stop(org.threeten.bp.Duration timeout)
throws IOException, InterruptedException, TimeoutException {
stopDuration(toJavaTimeDuration(timeout));
}

/**
* Stops the Datastore emulator.
*
Expand All @@ -319,23 +328,24 @@ public void reset() throws IOException {
* this value high to ensure proper shutdown, like 5 seconds or more.
*/
@Override
public void stop(Duration timeout) throws IOException, InterruptedException, TimeoutException {
public void stopDuration(java.time.Duration timeout)
throws IOException, InterruptedException, TimeoutException {
sendPostRequest("/shutdown");
waitForProcess(timeout);
waitForProcessDuration(timeout);
deleteRecursively(gcdPath);
}

/**
* Stops the Datastore emulator. The same as {@link #stop(Duration)} but with timeout duration of
* 20 seconds.
* Stops the Datastore emulator. The same as {@link #stopDuration(java.time.Duration)} but with
* timeout duration of 20 seconds.
*
* <p>It is important to stop the emulator. Since the emulator runs in its own process, not
* stopping it might cause it to become orphan.
*
* <p>It is not required to call {@link #reset()} before {@code stop()}.
*/
public void stop() throws IOException, InterruptedException, TimeoutException {
stop(Duration.ofSeconds(20));
stopDuration(java.time.Duration.ofSeconds(20));
}

static void deleteRecursively(Path path) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import com.google.datastore.v1.TransactionOptions;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -87,7 +88,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.threeten.bp.Duration;

@RunWith(JUnit4.class)
public class DatastoreTest {
Expand Down Expand Up @@ -193,7 +193,7 @@ public void setUp() {

@AfterClass
public static void afterClass() throws IOException, InterruptedException, TimeoutException {
helper.stop(Duration.ofMinutes(1));
helper.stopDuration(Duration.ofMinutes(1));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import com.google.common.truth.Truth;
import com.google.datastore.v1.TransactionOptions;
import com.google.datastore.v1.TransactionOptions.ReadOnly;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -103,7 +104,6 @@
import org.junit.rules.Timeout;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.threeten.bp.Duration;

@RunWith(Parameterized.class)
public class ITDatastoreTest {
Expand Down Expand Up @@ -674,7 +674,7 @@ private void assertExecutionStats(
Truth.assertThat(debugStats.get("index_entries_scanned"))
.isEqualTo(expectedIndexEntriesScanned);

Duration executionDuration = executionStats.getExecutionDuration();
Duration executionDuration = executionStats.getExecutionDurationJavaTime();
Truth.assertThat(executionDuration).isIn(Range.greaterThan(Duration.ofMillis(0)));

long readOperations = executionStats.getReadOperations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public class ExecutionStatsTest {
@Test
public void testModel() {
Truth.assertThat(executionStats.getDebugStats()).isEqualTo(Structs.asMap(struct));
Truth.assertThat(executionStats.getExecutionDuration())
.isEqualTo(org.threeten.bp.Duration.ofNanos(duration.getNanos()));
Truth.assertThat(executionStats.getExecutionDurationJavaTime())
.isEqualTo(java.time.Duration.ofNanos(duration.getNanos()));
Truth.assertThat(executionStats.getReadOperations()).isEqualTo(2);
Truth.assertThat(executionStats.getResultsReturned()).isEqualTo(3);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.concurrent.TimeoutException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.threeten.bp.Duration;

@RunWith(JUnit4.class)
public class ITLocalDatastoreHelperTest {
Expand Down Expand Up @@ -178,7 +178,7 @@ public void testStartStopReset() throws IOException, InterruptedException, Timeo
assertNotNull(datastore.get(key));
helper.reset();
assertNull(datastore.get(key));
helper.stop(Duration.ofMinutes(1));
helper.stopDuration(Duration.ofMinutes(1));
datastore.get(key);
Assert.fail();
} catch (DatastoreException ex) {
Expand All @@ -198,7 +198,7 @@ public void testStartStopResetWithBuilder()
assertNotNull(datastore.get(key));
helper.reset();
assertNull(datastore.get(key));
helper.stop(Duration.ofMinutes(1));
helper.stopDuration(Duration.ofMinutes(1));
datastore.get(key);
Assert.fail();
} catch (DatastoreException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import com.google.cloud.datastore.StructuredQuery;
import com.google.cloud.http.HttpTransportOptions;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import java.time.Duration;
import java.util.UUID;
import javax.annotation.Nullable;
import org.threeten.bp.Duration;

/**
* Utility to create a remote datastore configuration for testing. Datastore options can be obtained
Expand Down Expand Up @@ -110,13 +110,13 @@ public static RemoteDatastoreHelper create(String databaseId) {
private static RetrySettings retrySettings() {
return RetrySettings.newBuilder()
.setMaxAttempts(10)
.setMaxRetryDelay(Duration.ofMillis(30000L))
.setTotalTimeout(Duration.ofMillis(120000L))
.setInitialRetryDelay(Duration.ofMillis(250L))
.setMaxRetryDelayDuration(Duration.ofMillis(30000L))
.setTotalTimeoutDuration(Duration.ofMillis(120000L))
.setInitialRetryDelayDuration(Duration.ofMillis(250L))
.setRetryDelayMultiplier(1.0)
.setInitialRpcTimeout(Duration.ofMillis(120000L))
.setInitialRpcTimeoutDuration(Duration.ofMillis(120000L))
.setRpcTimeoutMultiplier(1.0)
.setMaxRpcTimeout(Duration.ofMillis(120000L))
.setMaxRpcTimeoutDuration(Duration.ofMillis(120000L))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.threeten.bp.Duration;

/** Contains Cloud Datastore snippets demonstrating concepts for documentation. */
@RunWith(JUnit4.class)
Expand Down Expand Up @@ -147,7 +146,7 @@ public void tearDown() throws Exception {
*/
@AfterClass
public static void afterClass() throws IOException, InterruptedException, TimeoutException {
HELPER.stop(Duration.ofMinutes(1));
HELPER.stopDuration(java.time.Duration.ofMinutes(1));
}

private void assertValidKey(Key taskKey) {
Expand Down