-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#51 protect from undefined behavior caused by arithmetic overflow
- Loading branch information
1 parent
895d436
commit c939130
Showing
6 changed files
with
133 additions
and
26 deletions.
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
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
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
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
49 changes: 49 additions & 0 deletions
49
...et4j-core/src/test/java/io/github/bucket4j/HandlingArithmeticOverflowSpecification.groovy
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,49 @@ | ||
/* | ||
* | ||
* Copyright 2015-2017 Vladimir Bukhtoyarov | ||
* | ||
* 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.github.bucket4j | ||
|
||
import io.github.bucket4j.mock.TimeMeterMock | ||
import spock.lang.Specification | ||
|
||
import java.time.Duration | ||
|
||
class HandlingArithmeticOverflowSpecification extends Specification { | ||
|
||
def "regression test for https://github.com/vladimir-bukhtoyarov/bucket4j/issues/51"() { | ||
setup: | ||
final Bandwidth limit1 = Bandwidth.simple(700000, Duration.ofHours(1)) | ||
final Bandwidth limit2 = Bandwidth.simple(14500, Duration.ofMinutes(1)) | ||
final Bandwidth limit3 = Bandwidth.simple(300, Duration.ofSeconds(1)) | ||
TimeMeterMock customTimeMeter = new TimeMeterMock(0) | ||
long twelveHourNanos = 12 * 60 * 60 * 1_000_000_000L; | ||
Bucket bucket = Bucket4j.builder() | ||
.addLimit(limit1) | ||
.addLimit(limit2) | ||
.addLimit(limit3) | ||
.withCustomTimePrecision(customTimeMeter) | ||
.build() | ||
when: | ||
// shift time to 12 hours forward | ||
customTimeMeter.addTime(twelveHourNanos) | ||
then: | ||
bucket.tryConsume(1) | ||
bucket.tryConsume(300 - 1) | ||
!bucket.tryConsume(1) | ||
} | ||
|
||
} |
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