@Test public void testRevalidateCacheEntryDoesNotPopulateIdentifierOnRejectedExecutionException() { when(mockSchedulingStrategy.schedule(ArgumentMatchers.anyInt())).thenReturn(TimeValue.ofSeconds(2)); doThrow(new RejectedExecutionException()).when(mockScheduledExecutor).schedule(ArgumentMatchers.<Runnable>any(), ArgumentMatchers.<TimeValue>any()); final String cacheKey = "blah"; impl.scheduleRevalidation(cacheKey, mockOperation); Assert.assertEquals(0, impl.getScheduledIdentifiers().size()); verify(mockScheduledExecutor).schedule(ArgumentMatchers.<Runnable>any(), ArgumentMatchers.eq(TimeValue.ofSeconds(2))); }
@Override public ScheduledFuture<?> schedule(final Runnable command, final TimeValue timeValue) throws RejectedExecutionException { Args.notNull(command, "Runnable"); Args.notNull(timeValue, "Time value"); return executorService.schedule(command, timeValue.getDuration(), timeValue.getTimeUnit()); }
private boolean isGreaterThan(final TimeValue other) { final TimeUnit targetTimeUnit = min(other.getTimeUnit()); return convert(targetTimeUnit) > other.convert(targetTimeUnit); }
/** * Returns a TimeValue whose value is {@code (this / divisor)}. * * @param divisor * value by which this TimeValue is to be divided. * @param targetTimeUnit * the target TimeUnit * @return {@code this / divisor} * @throws ArithmeticException * if {@code divisor} is zero. */ public TimeValue divide(final long divisor, final TimeUnit targetTimeUnit) { return of(convert(targetTimeUnit) / divisor, targetTimeUnit); }
@Test public void testDivide() { // nominator is 0, result should be 0. Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toDays()); Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toHours()); Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toMicros()); Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toMillis()); Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toMinutes()); Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toNanos()); Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toSeconds()); Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toMillisIntBound()); Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toSecondsIntBound()); // Assert.assertEquals(50, TimeValue.ofMilliseconds(100).divide(2).toMillis()); Assert.assertEquals(0, TimeValue.ofMinutes(1).divide(2).toSeconds()); Assert.assertEquals(30, TimeValue.ofMinutes(1).divide(2, TimeUnit.SECONDS).toSeconds()); Assert.assertEquals(30000, TimeValue.ofMinutes(1).divide(2, TimeUnit.MILLISECONDS).toMillis()); }
@Override public void closeIdle(final TimeValue idleTime) { final long deadline = System.currentTimeMillis() - (TimeValue.isPositive(idleTime) ? idleTime.toMillis() : 0); enumAvailable(new Callback<PoolEntry<T, C>>() { @Override public void execute(final PoolEntry<T, C> entry) { if (entry.getUpdated() <= deadline) { entry.discardConnection(CloseMode.GRACEFUL); } } }); }
@Test public void testMin() { final TimeValue nanos1 = TimeValue.ofNanoseconds(1); final TimeValue micros1 = TimeValue.ofMicroseconds(1); final TimeValue millis1 = TimeValue.ofMilliseconds(1); final TimeValue seconds1 = TimeValue.ofSeconds(1); final TimeValue minutes1 = TimeValue.ofMinutes(1); final TimeValue hours1 = TimeValue.ofHours(1); final TimeValue days1 = TimeValue.ofDays(1); Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(nanos1)); Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(micros1)); Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(millis1)); Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(seconds1)); Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(minutes1)); Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(hours1)); Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(days1)); Assert.assertEquals(nanos1, nanos1.min(nanos1)); Assert.assertEquals(nanos1, nanos1.min(micros1)); Assert.assertEquals(nanos1, nanos1.min(millis1)); Assert.assertEquals(nanos1, nanos1.min(seconds1)); Assert.assertEquals(nanos1, nanos1.min(minutes1)); Assert.assertEquals(nanos1, nanos1.min(hours1)); Assert.assertEquals(nanos1, nanos1.min(days1)); Assert.assertEquals(nanos1, micros1.min(nanos1)); Assert.assertEquals(micros1, micros1.min(micros1)); Assert.assertEquals(micros1, micros1.min(millis1)); Assert.assertEquals(micros1, micros1.min(seconds1));
@Test public void testFromString() throws ParseException { Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9,223,372,036,854,775,807 SECONDS")); Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9223372036854775807 SECONDS")); Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse(" 9223372036854775807 SECONDS ")); Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9,223,372,036,854,775,807 Seconds")); Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9,223,372,036,854,775,807 Seconds")); Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9,223,372,036,854,775,807\tSeconds")); Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.parse("0 MILLISECONDS")); Assert.assertEquals(TimeValue.ofMilliseconds(1), TimeValue.parse("1 MILLISECOND")); }
@Override public final void awaitShutdown(final TimeValue waitTime) throws InterruptedException { Args.notNull(waitTime, "Wait time"); final long deadline = System.currentTimeMillis() + waitTime.toMillis(); long remaining = waitTime.toMillis(); for (int i = 0; i < this.ioReactors.length; i++) { final IOReactor ioReactor = this.ioReactors[i]; if (ioReactor.getStatus().compareTo(IOReactorStatus.SHUT_DOWN) < 0) { ioReactor.awaitShutdown(TimeValue.of(remaining, TimeUnit.MILLISECONDS)); remaining = deadline - System.currentTimeMillis(); if (remaining <= 0) { return; } } } for (int i = 0; i < this.threads.length; i++) { final Thread thread = this.threads[i]; thread.join(remaining); remaining = deadline - System.currentTimeMillis(); if (remaining <= 0) { return; } } }
@Test public void testConvert() { Assert.assertEquals(0, TimeValue.ofMilliseconds(0).convert(TimeUnit.DAYS)); Assert.assertEquals(1000, TimeValue.ofSeconds(1).convert(TimeUnit.MILLISECONDS)); }
public int toMillisIntBound() { return asBoundInt(toMillis()); }
public IOReactorConfig build() { return new IOReactorConfig( selectInterval != null ? selectInterval : TimeValue.ofSeconds(1), ioThreadCount, Timeout.defaultsToDisabled(soTimeout), soReuseAddress, TimeValue.defaultsToNegativeOneMillisecond(soLinger), soKeepAlive, tcpNoDelay, sndBufSize, rcvBufSize, backlogSize, socksProxyAddress, socksProxyUsername, socksProxyPassword); }
@Override public Socket connectSocket( final TimeValue connectTimeout, final Socket socket, final HttpHost host, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context) throws IOException { final Socket sock = socket != null ? socket : createSocket(context); if (localAddress != null) { sock.bind(localAddress); } try { sock.connect(remoteAddress, TimeValue.isPositive(connectTimeout) ? connectTimeout.toMillisIntBound() : 0); } catch (final IOException ex) { Closer.closeQuietly(sock); throw ex; } return sock; }
private void testFactory(final TimeUnit timeUnit) { Assert.assertEquals(timeUnit, TimeValue.of(1, timeUnit).getTimeUnit()); }
@Override public TimeValue schedule(final int attemptNumber) { return TimeValue.ofMilliseconds(calculateDelayInMillis(attemptNumber)); }
@Override public void upgradeTls(final HttpClientContext context) { final AsyncConnectionEndpoint endpoint = ensureValid(); final RequestConfig requestConfig = context.getRequestConfig(); final Timeout connectTimeout = requestConfig.getConnectTimeout(); if (TimeValue.isPositive(connectTimeout)) { endpoint.setSocketTimeout(connectTimeout); } manager.upgrade(endpoint, versionPolicy, context); }
private TimeUnit min(final TimeUnit other) { return scale() > scale(other) ? other : getTimeUnit(); }
@Test(expected = ArithmeticException.class) public void testDivideBy0() { TimeValue.ofMilliseconds(0).divide(0); }