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

core: combined epoch seconds and nanos changes in TimeProvider #11604

Merged
merged 29 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
fca2584
grpc-core: combined epoch seconds and nanos changes in TimeProvider
vinodhabib Oct 7, 2024
bd0a6c6
Merge remote-tracking branch 'origin/defect-5494-timeprovider-changes…
vinodhabib Oct 7, 2024
12b9b1a
grpc-core: combined epoch seconds and nanos changes in TimeProvider
vinodhabib Oct 8, 2024
3d538d8
grpc-core: Fixed checkstyle issue
vinodhabib Oct 8, 2024
9d04a74
grpc-core: added the comment
vinodhabib Oct 8, 2024
9c564dc
grpc-core: removed the comment
vinodhabib Oct 8, 2024
a64afcc
grpc-core: Fixed Review points along with junits
vinodhabib Oct 9, 2024
34d7c1d
grpc-core: Fixed Review points
vinodhabib Oct 10, 2024
32cd455
grpc-core: Fixed Review points
vinodhabib Oct 10, 2024
71ce040
grpc-core: Fixed Review points
vinodhabib Oct 17, 2024
58073d4
grpc-core: Reverted back to previous changes
vinodhabib Oct 17, 2024
808b587
core: Fixed the UT as per Review point
vinodhabib Oct 18, 2024
7eee75d
core: changed the implementation as per the review points
vinodhabib Oct 21, 2024
6d59fa1
core: Fixed checkstyle issue
vinodhabib Oct 21, 2024
a43e9fc
core: Fixed checkstyle issue
vinodhabib Oct 21, 2024
04f1c3d
core: Added junits for the changes
vinodhabib Oct 21, 2024
7d09147
core: Added annotation for junit class
vinodhabib Oct 21, 2024
19a44f5
okHttp: Code cleanup
vinodhabib Oct 21, 2024
1757f99
core: Fixed the review points
vinodhabib Oct 23, 2024
b6a91b9
core: Fixed checkstyle issue
vinodhabib Oct 23, 2024
5178e78
core: Added junit for exception flow
vinodhabib Oct 23, 2024
133988b
core: Fixed checkstyle issues
vinodhabib Oct 23, 2024
ee69565
core: Fixed review points
vinodhabib Oct 24, 2024
00fae97
core: Fixed review points
vinodhabib Oct 24, 2024
f4abffc
core: Fixed review points
vinodhabib Oct 25, 2024
09fb474
core: Fixed checkstyle issues
vinodhabib Oct 25, 2024
b24dd90
core: Fixed recent review points
vinodhabib Oct 28, 2024
72ecc01
Merge branch 'master' into defect-5494-timeprovider-changes
vinodhabib Oct 28, 2024
d637a36
core: Fixed recent review points
vinodhabib Oct 28, 2024
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
32 changes: 32 additions & 0 deletions core/src/main/java/io/grpc/internal/ConcurrentTimeProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2024 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.internal;

import java.util.concurrent.TimeUnit;

/**
* {@link ConcurrentTimeProvider} resolves ConcurrentTimeProvider which implements
* {@link TimeProvider}.
*/

final class ConcurrentTimeProvider implements TimeProvider {

@Override
public long currentTimeNanos() {
return TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis());
}
}
42 changes: 42 additions & 0 deletions core/src/main/java/io/grpc/internal/InstantTimeProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2024 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.internal;

/**
* {@link InstantTimeProvider} resolves InstantTimeProvider which implements {@link TimeProvider}.
*/

final class InstantTimeProvider implements TimeProvider {

private Class<?> instantClass;

public InstantTimeProvider(Class<?> instantClass) {
this.instantClass = instantClass;
}

@Override
public long currentTimeNanos() {
try {
Object instant = instantClass.getMethod("now").invoke(null);
int nanos = (int) instantClass.getMethod("getNano").invoke(instant);
long epochSeconds = (long) instantClass.getMethod("getEpochSecond").invoke(instant);
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
return epochSeconds * 1_000_000_000L + nanos;
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception ex) {
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
throw new RuntimeException(ex);

Check warning on line 39 in core/src/main/java/io/grpc/internal/InstantTimeProvider.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/InstantTimeProvider.java#L38-L39

Added lines #L38 - L39 were not covered by tests
}
}
}
9 changes: 1 addition & 8 deletions core/src/main/java/io/grpc/internal/TimeProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package io.grpc.internal;

import java.util.concurrent.TimeUnit;

/**
* Time source representing the current system time in nanos. Used to inject a fake clock
* into unit tests.
Expand All @@ -26,10 +24,5 @@ public interface TimeProvider {
/** Returns the current nano time. */
long currentTimeNanos();

TimeProvider SYSTEM_TIME_PROVIDER = new TimeProvider() {
@Override
public long currentTimeNanos() {
return TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis());
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
}
};
TimeProvider SYSTEM_TIME_PROVIDER = TimeProviderResolverFactory.resolveTimeProvider();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2024 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.internal;

/**
* {@link TimeProviderResolverFactory} resolves Time providers.
*/

final class TimeProviderResolverFactory {

Check warning on line 23 in core/src/main/java/io/grpc/internal/TimeProviderResolverFactory.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/TimeProviderResolverFactory.java#L23

Added line #L23 was not covered by tests

private static final Class<?> INSTANT_CLASS = initInstant();
ejona86 marked this conversation as resolved.
Show resolved Hide resolved

private static Class<?> initInstant() {
try {
return Class.forName("java.time.Instant");
} catch (Exception ex) {
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
return null;

Check warning on line 31 in core/src/main/java/io/grpc/internal/TimeProviderResolverFactory.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/TimeProviderResolverFactory.java#L30-L31

Added lines #L30 - L31 were not covered by tests
}
}

static TimeProvider resolveTimeProvider() {
if (INSTANT_CLASS == null) {
return new ConcurrentTimeProvider();

Check warning on line 37 in core/src/main/java/io/grpc/internal/TimeProviderResolverFactory.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/TimeProviderResolverFactory.java#L37

Added line #L37 was not covered by tests
} else {
return new InstantTimeProvider(INSTANT_CLASS);
}
}
}
68 changes: 68 additions & 0 deletions core/src/test/java/io/grpc/internal/InstantTimeProviderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2024 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.internal;

import static com.google.common.truth.Truth.assertThat;

import java.time.Instant;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* Unit tests for {@link InstantTimeProvider}.
*/
@RunWith(JUnit4.class)
public class InstantTimeProviderTest {

@Test
public void testConcurrentCurrentTimeNanos() {

ConcurrentTimeProvider concurrentTimeProvider = new ConcurrentTimeProvider();
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
// Get the current time from the TimeProvider
long actualTimeNanos = concurrentTimeProvider.currentTimeNanos();

// Get the current time from Instant for comparison
Instant instantNow = Instant.now();
long expectedTimeNanos = instantNow.getEpochSecond() * 1_000_000_000L + instantNow.getNano();

// Validate the time returned is close to the expected value within a tolerance
// (i,e 10 millisecond tolerance in nanoseconds).
assertThat(actualTimeNanos).isWithin(10_000_000L).of(expectedTimeNanos);
kannanjgithub marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void testInstantCurrentTimeNanos() {
try {
InstantTimeProvider instantTimeProvider = new InstantTimeProvider(
Class.forName("java.time.Instant"));

// Get the current time from the TimeProvider
long actualTimeNanos = instantTimeProvider.currentTimeNanos();

// Get the current time from Instant for comparison
Instant instantNow = Instant.now();
long expectedTimeNanos = instantNow.getEpochSecond() * 1_000_000_000L + instantNow.getNano();

// Validate the time returned is close to the expected value within a tolerance
// (i,e 10 millisecond tolerance in nanoseconds).
assertThat(actualTimeNanos).isWithin(10_000_000L).of(expectedTimeNanos);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}