@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))); }
@Test public void testRevalidateCacheEntrySchedulesExecutionAndPopulatesIdentifier() { when(mockSchedulingStrategy.schedule(ArgumentMatchers.anyInt())).thenReturn(TimeValue.ofSeconds(1)); final String cacheKey = "blah"; impl.scheduleRevalidation(cacheKey, mockOperation); verify(mockSchedulingStrategy).schedule(0); verify(mockScheduledExecutor).schedule(ArgumentMatchers.same(mockOperation), ArgumentMatchers.eq(TimeValue.ofSeconds(1))); Assert.assertEquals(1, impl.getScheduledIdentifiers().size()); }
@Test public void testMarkCompleteRemovesIdentifier() { when(mockSchedulingStrategy.schedule(ArgumentMatchers.anyInt())).thenReturn(TimeValue.ofSeconds(3)); final String cacheKey = "blah"; impl.scheduleRevalidation(cacheKey, mockOperation); verify(mockSchedulingStrategy).schedule(0); verify(mockScheduledExecutor).schedule(ArgumentMatchers.<Runnable>any(), ArgumentMatchers.eq(TimeValue.ofSeconds(3))); Assert.assertEquals(1, impl.getScheduledIdentifiers().size()); Assert.assertTrue(impl.getScheduledIdentifiers().contains(cacheKey)); impl.jobSuccessful(cacheKey); Assert.assertEquals(0, impl.getScheduledIdentifiers().size()); }
@Test public void testRevalidateCacheEntryProperlyCollapsesRequest() { when(mockSchedulingStrategy.schedule(ArgumentMatchers.anyInt())).thenReturn(TimeValue.ofSeconds(2)); final String cacheKey = "blah"; impl.scheduleRevalidation(cacheKey, mockOperation); impl.scheduleRevalidation(cacheKey, mockOperation); impl.scheduleRevalidation(cacheKey, mockOperation); verify(mockSchedulingStrategy).schedule(ArgumentMatchers.anyInt()); verify(mockScheduledExecutor).schedule(ArgumentMatchers.<Runnable>any(), ArgumentMatchers.eq(TimeValue.ofSeconds(2))); Assert.assertEquals(1, impl.getScheduledIdentifiers().size()); }
Builder() { this.selectInterval = TimeValue.ofSeconds(1); this.ioThreadCount = Builder.getDefaultMaxIoThreadCount(); this.soTimeout = Timeout.ZERO_MILLISECONDS; this.soReuseAddress = false; this.soLinger = TimeValue.NEG_ONE_SECONDS; this.soKeepAlive = false; this.tcpNoDelay = true; this.sndBufSize = 0; this.rcvBufSize = 0; this.backlogSize = 0; }
@Override public void close() throws Exception { shutdown(TimeValue.ofSeconds(5)); }
@Override public void close() throws Exception { shutdown(TimeValue.ofSeconds(5)); }
Builder() { this.selectInterval = TimeValue.ofSeconds(1); this.ioThreadCount = Builder.getDefaultMaxIoThreadCount(); this.soTimeout = Timeout.ZERO_MILLISECONDS; this.soReuseAddress = false; this.soLinger = TimeValue.NEG_ONE_SECONDS; this.soKeepAlive = false; this.tcpNoDelay = true; this.sndBufSize = 0; this.rcvBufSize = 0; this.backlogSize = 0; this.socksProxyAddress = null; this.socksProxyUsername = null; this.socksProxyPassword = null; }
@Override public final void close(final CloseMode closeMode) { if (closeMode == CloseMode.GRACEFUL) { initiateShutdown(); try { awaitShutdown(TimeValue.ofSeconds(5)); } catch (final InterruptedException e) { Thread.currentThread().interrupt(); } } else { final IOReactorStatus previousStatus = this.status.getAndSet(IOReactorStatus.SHUT_DOWN); if (previousStatus.compareTo(IOReactorStatus.ACTIVE) == 0) { this.selector.wakeup(); } synchronized (this.shutdownMutex) { this.shutdownMutex.notifyAll(); } } }
@Override public final void close(final CloseMode closeMode) { if (closeMode == CloseMode.GRACEFUL) { initiateShutdown(); try { awaitShutdown(TimeValue.ofSeconds(5)); } catch (final InterruptedException e) { Thread.currentThread().interrupt(); } } else { final IOReactorStatus previousStatus = this.status.getAndSet(IOReactorStatus.SHUT_DOWN); if (previousStatus.compareTo(IOReactorStatus.ACTIVE) == 0) { this.selector.wakeup(); } synchronized (this.shutdownMutex) { this.shutdownMutex.notifyAll(); } } }
@Override public final void close(final CloseMode closeMode) { final IOReactorStatus currentStatus = this.status.get(); if (currentStatus == IOReactorStatus.INACTIVE || currentStatus == IOReactorStatus.SHUT_DOWN) { return; } if (closeMode == CloseMode.GRACEFUL) { initiateShutdown(); try { awaitShutdown(TimeValue.ofSeconds(5)); } catch (final InterruptedException e) { Thread.currentThread().interrupt(); } } else { this.status.compareAndSet(IOReactorStatus.ACTIVE, IOReactorStatus.SHUTTING_DOWN); for (int i = 0; i < this.ioReactors.length; i++) { Closer.close(this.ioReactors[i], CloseMode.IMMEDIATE); } for (int i = 0; i < this.threads.length; i++) { this.threads[i].interrupt(); } } this.status.set(IOReactorStatus.SHUT_DOWN); }
@Override public TimeValue getKeepAliveDuration(final HttpResponse response, final HttpContext context) { Args.notNull(response, "HTTP response"); final Iterator<HeaderElement> it = MessageSupport.iterate(response, HeaderElements.KEEP_ALIVE); while (it.hasNext()) { final HeaderElement he = it.next(); final String param = he.getName(); final String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { try { return TimeValue.ofSeconds(Long.parseLong(value)); } catch(final NumberFormatException ignore) { } } } return TimeValue.NEG_ONE_MILLISECONDS; }
public IdleConnectionEvictor(final ConnPoolControl<?> connectionManager, final ThreadFactory threadFactory, final TimeValue sleepTime, final TimeValue maxIdleTime) { Args.notNull(connectionManager, "Connection manager"); this.threadFactory = threadFactory != null ? threadFactory : new DefaultThreadFactory("idle-connection-evictor", true); final TimeValue localSleepTime = sleepTime != null ? sleepTime : TimeValue.ofSeconds(5); this.thread = this.threadFactory.newThread(new Runnable() { @Override public void run() { try { while (!Thread.currentThread().isInterrupted()) { Thread.sleep(localSleepTime.toMillis()); connectionManager.closeExpired(); if (maxIdleTime != null) { connectionManager.closeIdle(maxIdleTime); } } } catch (final InterruptedException ex) { Thread.currentThread().interrupt(); } catch (final Exception ex) { } } }); }
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); }
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); }
@Override public void close(final CloseMode closeMode) { initiateShutdown(); if (closeMode == CloseMode.GRACEFUL) { try { awaitTermination(TimeValue.ofSeconds(5)); } catch (final InterruptedException ex) { Thread.currentThread().interrupt(); } } final Set<Worker> workers = this.workerExecutorService.getWorkers(); for (final Worker worker: workers) { Closer.close(worker.getConnection(), CloseMode.GRACEFUL); } }
@Override public void close(final CloseMode closeMode) { initiateShutdown(); if (closeMode == CloseMode.GRACEFUL) { try { awaitTermination(TimeValue.ofSeconds(5)); } catch (final InterruptedException ex) { Thread.currentThread().interrupt(); } } final Set<Worker> workers = this.workerExecutorService.getWorkers(); for (final Worker worker: workers) { Closer.close(worker.getConnection(), CloseMode.GRACEFUL); } }
@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")); }
@Test public void testToString() { Assert.assertEquals("9,223,372,036,854,775,807 SECONDS", TimeValue.ofSeconds(Long.MAX_VALUE).toString()); Assert.assertEquals("0 MILLISECONDS", TimeValue.ZERO_MILLISECONDS.toString()); }
@Test public void testConvert() { Assert.assertEquals(0, TimeValue.ofMilliseconds(0).convert(TimeUnit.DAYS)); Assert.assertEquals(1000, TimeValue.ofSeconds(1).convert(TimeUnit.MILLISECONDS)); }