Skip to content

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

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

Merged
merged 29 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
7 changes: 5 additions & 2 deletions core/src/main/java/io/grpc/internal/TimeProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package io.grpc.internal;

import java.util.concurrent.TimeUnit;
import java.time.Instant;

/**
* Time source representing the current system time in nanos. Used to inject a fake clock
Expand All @@ -29,7 +29,10 @@ public interface TimeProvider {
TimeProvider SYSTEM_TIME_PROVIDER = new TimeProvider() {
@Override
public long currentTimeNanos() {
return TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis());
Instant instant = Instant.now();
int nanos = instant.getNano();
long epochSeconds = instant.getEpochSecond();
return epochSeconds * 1_000_000_000L + nanos;
}
};
}
43 changes: 43 additions & 0 deletions core/src/test/java/io/grpc/internal/TimeProviderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 org.junit.Assert.assertTrue;

import java.time.Instant;
import org.junit.Test;

/**
* Unit tests for {@link TimeProvider}.
*/
public class TimeProviderTest {
@Test
public void testCurrentTimeNanos() {

// Get the current time from the TimeProvider
long actualTimeNanos = TimeProvider.SYSTEM_TIME_PROVIDER.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.
long toleranceOfTenMilliSecond = 10_000_000L; // 10 millisecond tolerance in nanoseconds
assertTrue("The current time in nanoseconds should be close to the expected time.",
Math.abs(actualTimeNanos - expectedTimeNanos) < toleranceOfTenMilliSecond);
}
}