Remove tolerance from TimeWindowChecker (#1047)

This commit is contained in:
Konstantinos Chalkias 2017-07-19 12:33:57 +03:00 committed by GitHub
parent cb306a22fe
commit 8a2074eeee
2 changed files with 20 additions and 18 deletions

View File

@ -1,26 +1,22 @@
package net.corda.core.node.services
import net.corda.core.contracts.TimeWindow
import net.corda.core.utilities.seconds
import net.corda.core.until
import java.time.Clock
import java.time.Duration
/**
* Checks if the given time-window falls within the allowed tolerance interval.
* Checks if the current instant provided by the input clock falls within the provided time-window.
*/
class TimeWindowChecker(val clock: Clock = Clock.systemUTC(),
val tolerance: Duration = 30.seconds) {
class TimeWindowChecker(val clock: Clock = Clock.systemUTC()) {
fun isValid(timeWindow: TimeWindow): Boolean {
val untilTime = timeWindow.untilTime
val fromTime = timeWindow.fromTime
val untilTime = timeWindow.untilTime
val now = clock.instant()
// We don't need to test for (fromTime == null && untilTime == null) or backwards bounds because the TimeWindow
// constructor already checks that.
if (untilTime != null && untilTime until now > tolerance) return false
if (fromTime != null && now until fromTime > tolerance) return false
if (fromTime != null && now < fromTime) return false
if (untilTime != null && now > untilTime) return false
return true
}
}

View File

@ -11,23 +11,29 @@ import kotlin.test.assertTrue
class TimeWindowCheckerTests {
val clock = Clock.fixed(Instant.now(), ZoneId.systemDefault())
val timeWindowChecker = TimeWindowChecker(clock, tolerance = 30.seconds)
val timeWindowChecker = TimeWindowChecker(clock)
@Test
fun `should return true for valid time-window`() {
val now = clock.instant()
val timeWindowPast = TimeWindow.between(now - 60.seconds, now - 29.seconds)
val timeWindowFuture = TimeWindow.between(now + 29.seconds, now + 60.seconds)
assertTrue { timeWindowChecker.isValid(timeWindowPast) }
assertTrue { timeWindowChecker.isValid(timeWindowFuture) }
val timeWindowBetween = TimeWindow.between(now - 10.seconds, now + 10.seconds)
val timeWindowFromOnly = TimeWindow.fromOnly(now - 10.seconds)
val timeWindowUntilOnly = TimeWindow.untilOnly(now + 10.seconds)
assertTrue { timeWindowChecker.isValid(timeWindowBetween) }
assertTrue { timeWindowChecker.isValid(timeWindowFromOnly) }
assertTrue { timeWindowChecker.isValid(timeWindowUntilOnly) }
}
@Test
fun `should return false for invalid time-window`() {
val now = clock.instant()
val timeWindowPast = TimeWindow.between(now - 60.seconds, now - 31.seconds)
val timeWindowFuture = TimeWindow.between(now + 31.seconds, now + 60.seconds)
assertFalse { timeWindowChecker.isValid(timeWindowPast) }
assertFalse { timeWindowChecker.isValid(timeWindowFuture) }
val timeWindowBetweenPast = TimeWindow.between(now - 10.seconds, now - 2.seconds)
val timeWindowBetweenFuture = TimeWindow.between(now + 2.seconds, now + 10.seconds)
val timeWindowFromOnlyFuture = TimeWindow.fromOnly(now + 10.seconds)
val timeWindowUntilOnlyPast = TimeWindow.untilOnly(now - 10.seconds)
assertFalse { timeWindowChecker.isValid(timeWindowBetweenPast) }
assertFalse { timeWindowChecker.isValid(timeWindowBetweenFuture) }
assertFalse { timeWindowChecker.isValid(timeWindowFromOnlyFuture) }
assertFalse { timeWindowChecker.isValid(timeWindowUntilOnlyPast) }
}
}