-
Notifications
You must be signed in to change notification settings - Fork 148
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
Fix ArithmeticException in toJavaDuration. #1950
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
81179a0
Add unit tests for ProtobufTimeUtils.
chronos-tachyon 09a8e2f
Add copyright/license comment.
chronos-tachyon c7854d1
No, really, fix the copyright/license test.
chronos-tachyon 74bbf96
Actually fix the ArithmeticException and add test cases to prove it.
chronos-tachyon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
temporal-sdk/src/test/java/io/temporal/internal/common/ProtobufTimeUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved. | ||
* | ||
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this material 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.temporal.internal.common; | ||
|
||
import static io.temporal.internal.common.ProtobufTimeUtils.MAX_SECONDS; | ||
import static io.temporal.internal.common.ProtobufTimeUtils.MIN_SECONDS; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.time.Duration; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
@RunWith(Parameterized.class) | ||
public class ProtobufTimeUtilsTest { | ||
|
||
@Parameters | ||
public static Collection<Object[]> data() { | ||
return Arrays.asList( | ||
// Values with integral milliseconds | ||
new Object[] {0L, 0, 0L, 0}, | ||
new Object[] {0L, 1_000_000, 0L, 1_000_000}, | ||
new Object[] {0L, 500_000_000, 0L, 500_000_000}, | ||
new Object[] {0L, 999_000_000, 0L, 999_000_000}, | ||
new Object[] {123L, 456_000_000, 123L, 456_000_000}, | ||
new Object[] {0L, -1_000_000, 0L, -1_000_000}, | ||
new Object[] {0L, -500_000_000, 0L, -500_000_000}, | ||
new Object[] {0L, -999_000_000, 0L, -999_000_000}, | ||
new Object[] {-123L, -456_000_000, -123L, -456_000_000}, | ||
|
||
// Values with fractional milliseconds | ||
new Object[] {0L, 123_000_001, 0L, 123_000_000}, | ||
new Object[] {0L, 123_100_000, 0L, 123_000_000}, | ||
new Object[] {0L, 123_499_999, 0L, 123_000_000}, | ||
new Object[] {0L, 123_500_000, 0L, 123_000_000}, | ||
new Object[] {0L, 123_999_999, 0L, 123_000_000}, | ||
new Object[] {0L, -123_000_001, 0L, -123_000_000}, | ||
new Object[] {0L, -123_100_000, 0L, -123_000_000}, | ||
new Object[] {0L, -123_499_999, 0L, -123_000_000}, | ||
new Object[] {0L, -123_500_000, 0L, -123_000_000}, | ||
new Object[] {0L, -123_999_999, 0L, -123_000_000}, | ||
|
||
// Extremely large values | ||
new Object[] {MAX_SECONDS, 0, MAX_SECONDS, 0}, | ||
new Object[] {MAX_SECONDS, 999_000_000, MAX_SECONDS, 999_000_000}, | ||
new Object[] {MAX_SECONDS, 999_999_999, MAX_SECONDS, 999_000_000}, | ||
new Object[] {Long.MAX_VALUE, 0, MAX_SECONDS, 0}, | ||
new Object[] {Long.MAX_VALUE, 999_000_000, MAX_SECONDS, 999_000_000}, | ||
new Object[] {Long.MAX_VALUE, 999_999_999, MAX_SECONDS, 999_000_000}, | ||
new Object[] {MIN_SECONDS, 0, MIN_SECONDS, 0}, | ||
new Object[] {MIN_SECONDS, -999_000_000, MIN_SECONDS, -999_000_000}, | ||
new Object[] {MIN_SECONDS, -999_999_999, MIN_SECONDS, -999_000_000}, | ||
new Object[] {Long.MIN_VALUE, 0, MIN_SECONDS, 0}, | ||
new Object[] {Long.MIN_VALUE, -999_000_000, MIN_SECONDS, -999_000_000}, | ||
new Object[] {Long.MIN_VALUE, -999_999_999, MIN_SECONDS, -999_000_000}); | ||
} | ||
|
||
private final long inputSeconds; | ||
private final int inputNanos; | ||
private final long outputSeconds; | ||
private final int outputNanos; | ||
|
||
public ProtobufTimeUtilsTest( | ||
long inputSeconds, int inputNanos, long outputSeconds, int outputNanos) { | ||
this.inputSeconds = inputSeconds; | ||
this.inputNanos = inputNanos; | ||
this.outputSeconds = outputSeconds; | ||
this.outputNanos = outputNanos; | ||
} | ||
|
||
@Test | ||
public void toJavaDuration() { | ||
final Duration actual = ProtobufTimeUtils.toJavaDuration(makeProto(inputSeconds, inputNanos)); | ||
final Duration expect = makeJava(outputSeconds, outputNanos); | ||
assertEquals(expect, actual); | ||
} | ||
|
||
@Test | ||
public void toProtoDuration() { | ||
final com.google.protobuf.Duration actual = | ||
ProtobufTimeUtils.toProtoDuration(makeJava(inputSeconds, inputNanos)); | ||
final com.google.protobuf.Duration expect = makeProto(outputSeconds, outputNanos); | ||
assertEquals(expect, actual); | ||
} | ||
|
||
private static com.google.protobuf.Duration makeProto(long seconds, int nanos) { | ||
return com.google.protobuf.Duration.newBuilder().setSeconds(seconds).setNanos(nanos).build(); | ||
} | ||
|
||
private static Duration makeJava(long seconds, int nanos) { | ||
final long saturatedSeconds = Math.min(MAX_SECONDS, Math.max(MIN_SECONDS, seconds)); | ||
return Duration.ofSeconds(saturatedSeconds, nanos); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we confirm all the tests that didn't throw have the same behaviour before and after your change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I specifically tested that in the first commit after the rebase + force-push. Commit c7854d1e641e contains no changes to the logic (just the
Objects.isNull
cosmetic change) and passes all tests.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happily, the belief that
Durations.toMillis
does round to nearest instead of truncation was mistaken, as that would have meant that there was an asymmetry in the conversion. BothDuration.toMillis
(Java) andDurations.toMillis
(protobuf) truncate.