private Instant getScheduledTime() { return creationTime.plus(delay); }
/** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override public Instant plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case NANOS: return plusNanos(amountToAdd); case MICROS: return plus(amountToAdd / 1000000, (amountToAdd % 1000000) * 1000); case MILLIS: return plusMillis(amountToAdd); case SECONDS: return plusSeconds(amountToAdd); case MINUTES: return plusSeconds(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_MINUTE)); case HOURS: return plusSeconds(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_HOUR)); case HALF_DAYS: return plusSeconds(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_DAY / 2)); case DAYS: return plusSeconds(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_DAY)); } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } return unit.addTo(this, amountToAdd); }
@Test public void testTimeoutBeforeDeadline() { HttpJsonChannel mockChannel = Mockito.mock(HttpJsonChannel.class); String expectedRequest = "fake"; HttpJsonDirectCallable<String, String> callable = new HttpJsonDirectCallable<>(API_DESCRIPTOR); // Mock the channel that captures the call options ArgumentCaptor<HttpJsonCallOptions> capturedCallOptions = ArgumentCaptor.forClass(HttpJsonCallOptions.class); Mockito.when( mockChannel.issueFutureUnaryCall( capturedCallOptions.capture(), Mockito.anyString(), Mockito.any(ApiMethodDescriptor.class))) .thenReturn(SettableApiFuture.create()); // Compose the call context Duration timeout = Duration.ofSeconds(10); Instant subsequentDeadline = Instant.now().plusSeconds(15); Instant minExpectedDeadline = Instant.now().plus(timeout); HttpJsonCallContext callContext = HttpJsonCallContext.createDefault() .withChannel(mockChannel) .withDeadline(subsequentDeadline) .withTimeout(timeout); callable.futureCall(expectedRequest, callContext); Instant maxExpectedDeadline = Instant.now().plus(timeout); // Verify that the timeout was converted into a deadline assertThat(capturedCallOptions.getValue().getDeadline()).isAtLeast(minExpectedDeadline); assertThat(capturedCallOptions.getValue().getDeadline()).isAtMost(maxExpectedDeadline); }
if (currTime.isAfter(lastResetTime.plus(windowLength))) { int sessionsToKeep = Math.max(options.getMinSessions(), maxSessionsInUse + options.getMaxIdleSessions());
/** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override public Instant plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case NANOS: return plusNanos(amountToAdd); case MICROS: return plus(amountToAdd / 1000000, (amountToAdd % 1000000) * 1000); case MILLIS: return plusMillis(amountToAdd); case SECONDS: return plusSeconds(amountToAdd); case MINUTES: return plusSeconds(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_MINUTE)); case HOURS: return plusSeconds(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_HOUR)); case HALF_DAYS: return plusSeconds(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_DAY / 2)); case DAYS: return plusSeconds(Jdk8Methods.safeMultiply(amountToAdd, SECONDS_PER_DAY)); } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } return unit.addTo(this, amountToAdd); }
Instant totalExpiration = now().plus(maxAckExtensionPeriod); OutstandingMessageBatch outstandingBatch = new OutstandingMessageBatch(doneCallback); for (ReceivedMessage message : messages) {
break; case FIXED_RATE: this.creationTime = this.creationTime.plus(delay); schedulePendingCallable(this); break;
/** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override public Instant minus(long amountToSubtract, TemporalUnit unit) { return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); }
/** * Returns a copy of this instant with the specified duration in nanoseconds added. * <p> * This instance is immutable and unaffected by this method call. * * @param nanosToAdd the nanoseconds to add, positive or negative * @return an {@code Instant} based on this instant with the specified nanoseconds added, not null * @throws DateTimeException if the result exceeds the maximum or minimum instant * @throws ArithmeticException if numeric overflow occurs */ public Instant plusNanos(long nanosToAdd) { return plus(0, nanosToAdd); }
/** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override public Instant minus(long amountToSubtract, TemporalUnit unit) { return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit)); }
/** * Returns a copy of this instant with the specified duration in nanoseconds added. * <p> * This instance is immutable and unaffected by this method call. * * @param nanosToAdd the nanoseconds to add, positive or negative * @return an {@code Instant} based on this instant with the specified nanoseconds added, not null * @throws DateTimeException if the result exceeds the maximum or minimum instant * @throws ArithmeticException if numeric overflow occurs */ public Instant plusNanos(long nanosToAdd) { return plus(0, nanosToAdd); }
/** * Returns a copy of this instant with the specified duration in seconds added. * <p> * This instance is immutable and unaffected by this method call. * * @param secondsToAdd the seconds to add, positive or negative * @return an {@code Instant} based on this instant with the specified seconds added, not null * @throws DateTimeException if the result exceeds the maximum or minimum instant * @throws ArithmeticException if numeric overflow occurs */ public Instant plusSeconds(long secondsToAdd) { return plus(secondsToAdd, 0); }
/** * Returns a copy of this instant with the specified duration in milliseconds added. * <p> * This instance is immutable and unaffected by this method call. * * @param millisToAdd the milliseconds to add, positive or negative * @return an {@code Instant} based on this instant with the specified milliseconds added, not null * @throws DateTimeException if the result exceeds the maximum or minimum instant * @throws ArithmeticException if numeric overflow occurs */ public Instant plusMillis(long millisToAdd) { return plus(millisToAdd / 1000, (millisToAdd % 1000) * NANOS_PER_MILLI); }
/** * Returns a copy of this instant with the specified duration in seconds added. * <p> * This instance is immutable and unaffected by this method call. * * @param secondsToAdd the seconds to add, positive or negative * @return an {@code Instant} based on this instant with the specified seconds added, not null * @throws DateTimeException if the result exceeds the maximum or minimum instant * @throws ArithmeticException if numeric overflow occurs */ public Instant plusSeconds(long secondsToAdd) { return plus(secondsToAdd, 0); }
/** * Returns a copy of this instant with the specified duration in milliseconds added. * <p> * This instance is immutable and unaffected by this method call. * * @param millisToAdd the milliseconds to add, positive or negative * @return an {@code Instant} based on this instant with the specified milliseconds added, not null * @throws DateTimeException if the result exceeds the maximum or minimum instant * @throws ArithmeticException if numeric overflow occurs */ public Instant plusMillis(long millisToAdd) { return plus(millisToAdd / 1000, (millisToAdd % 1000) * NANOS_PER_MILLI); }
@Override public Instant instant() { return baseClock.instant().plus(offset); } @Override
@Override public Instant instant() { return baseClock.instant().plus(offset); } @Override
@Override public ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext inputContext) { Preconditions.checkNotNull(request); HttpJsonCallContext context = HttpJsonCallContext.createDefault().nullToSelf(inputContext); @Nullable Instant deadline = context.getDeadline(); // Try to convert the timeout into a deadline and use it if it occurs before the actual deadline if (context.getTimeout() != null) { @Nonnull Instant newDeadline = Instant.now().plus(context.getTimeout()); if (deadline == null || newDeadline.isBefore(deadline)) { deadline = newDeadline; } } HttpJsonCallOptions callOptions = HttpJsonCallOptions.newBuilder() .setDeadline(deadline) .setCredentials(context.getCredentials()) .build(); return context.getChannel().issueFutureUnaryCall(callOptions, request, descriptor); }
@Override public ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext inputContext) { Preconditions.checkNotNull(request); HttpJsonCallContext context = HttpJsonCallContext.createDefault().nullToSelf(inputContext); @Nullable Instant deadline = context.getDeadline(); // Try to convert the timeout into a deadline and use it if it occurs before the actual deadline if (context.getTimeout() != null) { @Nonnull Instant newDeadline = Instant.now().plus(context.getTimeout()); if (deadline == null || newDeadline.isBefore(deadline)) { deadline = newDeadline; } } HttpJsonCallOptions callOptions = HttpJsonCallOptions.newBuilder() .setDeadline(deadline) .setCredentials(context.getCredentials()) .build(); return context.getChannel().issueFutureUnaryCall(callOptions, request, descriptor); }
@Test public void testTimeout() { HttpJsonChannel mockChannel = Mockito.mock(HttpJsonChannel.class); String expectedRequest = "fake"; HttpJsonDirectCallable<String, String> callable = new HttpJsonDirectCallable<>(API_DESCRIPTOR); // Mock the channel that captures the call options ArgumentCaptor<HttpJsonCallOptions> capturedCallOptions = ArgumentCaptor.forClass(HttpJsonCallOptions.class); Mockito.when( mockChannel.issueFutureUnaryCall( capturedCallOptions.capture(), Mockito.anyString(), Mockito.any(ApiMethodDescriptor.class))) .thenReturn(SettableApiFuture.create()); // Compose the call context Duration timeout = Duration.ofSeconds(10); Instant minExpectedDeadline = Instant.now().plus(timeout); HttpJsonCallContext callContext = HttpJsonCallContext.createDefault().withChannel(mockChannel).withTimeout(timeout); callable.futureCall(expectedRequest, callContext); Instant maxExpectedDeadline = Instant.now().plus(timeout); // Verify that the timeout was converted into a deadline assertThat(capturedCallOptions.getValue().getDeadline()).isAtLeast(minExpectedDeadline); assertThat(capturedCallOptions.getValue().getDeadline()).isAtMost(maxExpectedDeadline); }