@Override public Duration fromString(String string) { if ( string == null ) { return null; } return Duration.ofNanos( Long.valueOf( string ) ); }
/** * Returns the time left between the deadline and now. The result is negative if the deadline * has passed. */ public Duration timeLeft() { return Duration.ofNanos(Math.subtractExact(timeNanos, System.nanoTime())); }
/** * Set the default timeout for connections created by this client. The timeout applies to connection attempts and * non-blocking commands. * * @param timeout Default connection timeout. * @param unit Unit of time for the timeout. * @deprecated since 5.0, use {@link #setDefaultTimeout(Duration)}. */ @Deprecated public void setDefaultTimeout(long timeout, TimeUnit unit) { setDefaultTimeout(Duration.ofNanos(unit.toNanos(timeout))); }
/** * Set the command timeout for this connection. * * @param timeout Command timeout. * @param unit Unit of time for the timeout. * @deprecated since 5.0, use {@link #setTimeout(Duration)} */ @Deprecated public void setTimeout(long timeout, TimeUnit unit) { setTimeout(Duration.ofNanos(unit.toNanos(timeout))); }
/** * Returns the current elapsed time shown on this stopwatch as a {@link Duration}. Unlike {@link * #elapsed(TimeUnit)}, this method does not lose any precision due to rounding. */ public Duration elapsed() { return Duration.ofNanos(elapsedNanos()); }
@Override public Duration createDelay(long attempt) { long value = randomBetween(base, Math.max(base, prevDelay * 3)); Duration delay = applyBounds(Duration.ofNanos(targetTimeUnit.toNanos(value)), lower, upper); prevDelay = delay.toNanos(); return delay; }
/** * Returns the current elapsed time shown on this stopwatch as a {@link Duration}. Unlike {@link * #elapsed(TimeUnit)}, this method does not lose any precision due to rounding. * * @since 22.0 */ @GwtIncompatible @J2ObjCIncompatible public Duration elapsed() { return Duration.ofNanos(elapsedNanos()); }
/** * Returns the fixed duration used to determine if an entry should be automatically removed due * to elapsing this time bound. An entry is considered fresh if its age is less than this * duration, and stale otherwise. The expiration policy determines when the entry's age is * reset. * * @return the length of time after which an entry should be automatically removed */ @NonNull default Duration getExpiresAfter() { // This method will be abstract in version 3.0.0 return Duration.ofNanos(getExpiresAfter(TimeUnit.NANOSECONDS)); }
private void publishSuccessEvent(final long durationInNanos) { final CircuitBreakerOnSuccessEvent event = new CircuitBreakerOnSuccessEvent(name, Duration.ofNanos(durationInNanos)); publishEventIfPossible(event); }
/** * Returns the duration until the entry should be automatically removed. The expiration policy * determines when the entry's duration is reset. * * @param key the key for the entry being queried * @return the duration if the entry is present in the cache */ @NonNull default Optional<Duration> getExpiresAfter(@NonNull K key) { // This method will be abstract in version 3.0.0 OptionalLong duration = getExpiresAfter(key, TimeUnit.NANOSECONDS); return duration.isPresent() ? Optional.of(Duration.ofNanos(duration.getAsLong())) : Optional.empty(); }
/** * Returns the current elapsed time shown on this stopwatch as a {@link Duration}. Unlike {@link * #elapsed(TimeUnit)}, this method does not lose any precision due to rounding. * * @since 22.0 */ @GwtIncompatible @J2ObjCIncompatible public Duration elapsed() { return Duration.ofNanos(elapsedNanos()); }
@Override public Duration createDelay(long attempt) { long delay; if (attempt <= 0) { // safeguard against underflow delay = 0; } else if (powersOf == 2) { delay = calculatePowerOfTwo(attempt); } else { delay = calculateAlternatePower(attempt); } return applyBounds(Duration.ofNanos(targetTimeUnit.toNanos(delay))); }
@Override public Duration createDelay(long attempt) { long value = randomBetween(0, base * calculatePowerOfTwo(attempt)); return applyBounds(Duration.ofNanos(targetTimeUnit.toNanos(value))); } }
/** * Returns the current elapsed time shown on this stopwatch as a {@link Duration}. Unlike {@link * #elapsed(TimeUnit)}, this method does not lose any precision due to rounding. * * @since 22.0 */ @GwtIncompatible @J2ObjCIncompatible public Duration elapsed() { return Duration.ofNanos(elapsedNanos()); }
@GwtIncompatible public void testElapsed_duration() { stopwatch.start(); ticker.advance(999999); assertEquals(Duration.ofNanos(999999), stopwatch.elapsed()); ticker.advance(1); assertEquals(Duration.ofMillis(1), stopwatch.elapsed()); }
@Test public void expireAfterAccess_duration_large() { Caffeine<?, ?> builder = Caffeine.newBuilder() .expireAfterAccess(Duration.ofNanos(Integer.MAX_VALUE)); assertThat(builder.expireAfterAccessNanos, is((long) Integer.MAX_VALUE)); Expiration<?, ?> expiration = builder.build().policy().expireAfterAccess().get(); assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE)); }
@Test public void expireAfterWrite_duration_large() { Caffeine<?, ?> builder = Caffeine.newBuilder() .expireAfterWrite(Duration.ofNanos(Integer.MAX_VALUE)); assertThat(builder.expireAfterWriteNanos, is((long) Integer.MAX_VALUE)); Expiration<?, ?> expiration = builder.build().policy().expireAfterWrite().get(); assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE)); }
@Test public void monoProcessorBlockIsUnbounded() { long start = System.nanoTime(); String result = Mono.just("foo") .delayElement(Duration.ofMillis(500)) .toProcessor() .block(); assertThat(result).isEqualTo("foo"); assertThat(Duration.ofNanos(System.nanoTime() - start)) .isGreaterThanOrEqualTo(Duration.ofMillis(500)); }
@Test public void validateWhenBothExpiryAndNotBeforeAreValidThenReturnsSuccessfulResult() { Jwt jwt = new Jwt( MOCK_TOKEN_VALUE, MOCK_ISSUED_AT, Instant.now(MOCK_NOW), MOCK_HEADER, Collections.singletonMap(JwtClaimNames.NBF, Instant.now(MOCK_NOW))); JwtTimestampValidator jwtValidator = new JwtTimestampValidator(Duration.ofNanos(0)); jwtValidator.setClock(MOCK_NOW); assertThat(jwtValidator.validate(jwt).hasErrors()).isFalse(); }
@Before public void setup() { rateLimiterConfig = RateLimiterConfig.custom() .limitForPeriod(PERMISSIONS_RER_CYCLE) .limitRefreshPeriod(Duration.ofNanos(CYCLE_IN_NANOS)) .timeoutDuration(Duration.ZERO) .build(); AtomicRateLimiter testLimiter = new AtomicRateLimiter(LIMITER_NAME, rateLimiterConfig); rateLimiter = PowerMockito.spy(testLimiter); metrics = rateLimiter.getDetailedMetrics(); }