mirror of
https://github.com/corda/corda.git
synced 2025-01-31 00:24:59 +00:00
Remove tolerance from TimeWindowChecker (#1047)
This commit is contained in:
parent
cb306a22fe
commit
8a2074eeee
@ -1,26 +1,22 @@
|
|||||||
package net.corda.core.node.services
|
package net.corda.core.node.services
|
||||||
|
|
||||||
import net.corda.core.contracts.TimeWindow
|
import net.corda.core.contracts.TimeWindow
|
||||||
import net.corda.core.utilities.seconds
|
|
||||||
import net.corda.core.until
|
|
||||||
import java.time.Clock
|
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(),
|
class TimeWindowChecker(val clock: Clock = Clock.systemUTC()) {
|
||||||
val tolerance: Duration = 30.seconds) {
|
|
||||||
fun isValid(timeWindow: TimeWindow): Boolean {
|
fun isValid(timeWindow: TimeWindow): Boolean {
|
||||||
val untilTime = timeWindow.untilTime
|
|
||||||
val fromTime = timeWindow.fromTime
|
val fromTime = timeWindow.fromTime
|
||||||
|
val untilTime = timeWindow.untilTime
|
||||||
|
|
||||||
val now = clock.instant()
|
val now = clock.instant()
|
||||||
|
|
||||||
// We don't need to test for (fromTime == null && untilTime == null) or backwards bounds because the TimeWindow
|
// We don't need to test for (fromTime == null && untilTime == null) or backwards bounds because the TimeWindow
|
||||||
// constructor already checks that.
|
// constructor already checks that.
|
||||||
if (untilTime != null && untilTime until now > tolerance) return false
|
if (fromTime != null && now < fromTime) return false
|
||||||
if (fromTime != null && now until fromTime > tolerance) return false
|
if (untilTime != null && now > untilTime) return false
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,23 +11,29 @@ import kotlin.test.assertTrue
|
|||||||
|
|
||||||
class TimeWindowCheckerTests {
|
class TimeWindowCheckerTests {
|
||||||
val clock = Clock.fixed(Instant.now(), ZoneId.systemDefault())
|
val clock = Clock.fixed(Instant.now(), ZoneId.systemDefault())
|
||||||
val timeWindowChecker = TimeWindowChecker(clock, tolerance = 30.seconds)
|
val timeWindowChecker = TimeWindowChecker(clock)
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should return true for valid time-window`() {
|
fun `should return true for valid time-window`() {
|
||||||
val now = clock.instant()
|
val now = clock.instant()
|
||||||
val timeWindowPast = TimeWindow.between(now - 60.seconds, now - 29.seconds)
|
val timeWindowBetween = TimeWindow.between(now - 10.seconds, now + 10.seconds)
|
||||||
val timeWindowFuture = TimeWindow.between(now + 29.seconds, now + 60.seconds)
|
val timeWindowFromOnly = TimeWindow.fromOnly(now - 10.seconds)
|
||||||
assertTrue { timeWindowChecker.isValid(timeWindowPast) }
|
val timeWindowUntilOnly = TimeWindow.untilOnly(now + 10.seconds)
|
||||||
assertTrue { timeWindowChecker.isValid(timeWindowFuture) }
|
assertTrue { timeWindowChecker.isValid(timeWindowBetween) }
|
||||||
|
assertTrue { timeWindowChecker.isValid(timeWindowFromOnly) }
|
||||||
|
assertTrue { timeWindowChecker.isValid(timeWindowUntilOnly) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should return false for invalid time-window`() {
|
fun `should return false for invalid time-window`() {
|
||||||
val now = clock.instant()
|
val now = clock.instant()
|
||||||
val timeWindowPast = TimeWindow.between(now - 60.seconds, now - 31.seconds)
|
val timeWindowBetweenPast = TimeWindow.between(now - 10.seconds, now - 2.seconds)
|
||||||
val timeWindowFuture = TimeWindow.between(now + 31.seconds, now + 60.seconds)
|
val timeWindowBetweenFuture = TimeWindow.between(now + 2.seconds, now + 10.seconds)
|
||||||
assertFalse { timeWindowChecker.isValid(timeWindowPast) }
|
val timeWindowFromOnlyFuture = TimeWindow.fromOnly(now + 10.seconds)
|
||||||
assertFalse { timeWindowChecker.isValid(timeWindowFuture) }
|
val timeWindowUntilOnlyPast = TimeWindow.untilOnly(now - 10.seconds)
|
||||||
|
assertFalse { timeWindowChecker.isValid(timeWindowBetweenPast) }
|
||||||
|
assertFalse { timeWindowChecker.isValid(timeWindowBetweenFuture) }
|
||||||
|
assertFalse { timeWindowChecker.isValid(timeWindowFromOnlyFuture) }
|
||||||
|
assertFalse { timeWindowChecker.isValid(timeWindowUntilOnlyPast) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user