mirror of https://github.com/apache/jclouds.git
Merge pull request #911 from abiquo/timeunit-fix
Use the TimeUnit parameter in async monitor
This commit is contained in:
commit
1d3f36defc
|
@ -131,7 +131,7 @@ public class BaseMonitoringService implements MonitoringService {
|
|||
if (objects != null && objects.length > 0) {
|
||||
for (T object : objects) {
|
||||
AsyncMonitor<T> monitor = new AsyncMonitor<T>(object, completeCondition);
|
||||
monitor.startMonitoring(maxWait);
|
||||
monitor.startMonitoring(maxWait, timeUnit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -176,8 +176,7 @@ public class BaseMonitoringService implements MonitoringService {
|
|||
* Performs the periodical monitoring tasks.
|
||||
*
|
||||
* @author Ignasi Barrera
|
||||
* @param <T>
|
||||
* The type of the object being monitored.
|
||||
* @param <T> The type of the object being monitored.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
class AsyncMonitor<T> implements Runnable {
|
||||
|
@ -205,12 +204,15 @@ public class BaseMonitoringService implements MonitoringService {
|
|||
/**
|
||||
* Starts the monitoring job with the given timeout.
|
||||
*
|
||||
* @param maxWait
|
||||
* The timeout.
|
||||
* @param maxWait The timeout.
|
||||
* @param timeUnit The timeunit used in the maxWait parameter.
|
||||
*/
|
||||
public void startMonitoring(final Long maxWait) {
|
||||
public void startMonitoring(final Long maxWait, TimeUnit timeUnit) {
|
||||
if (maxWait != null) {
|
||||
checkNotNull(timeUnit, "timeUnit must not be null when using timeouts");
|
||||
}
|
||||
future = scheduler.scheduleWithFixedDelay(this, 0L, pollingDelay, TimeUnit.MILLISECONDS);
|
||||
timeout = maxWait == null ? null : System.currentTimeMillis() + maxWait;
|
||||
timeout = maxWait == null ? null : System.currentTimeMillis() + timeUnit.toMillis(maxWait);
|
||||
logger.debug("started monitor job for %s with %s timeout", monitoredObject,
|
||||
timeout == null ? "no" : String.valueOf(timeout));
|
||||
}
|
||||
|
|
|
@ -69,7 +69,43 @@ public class AsyncMonitorTest {
|
|||
assertNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
monitor.startMonitoring(null);
|
||||
monitor.startMonitoring(null, TimeUnit.MILLISECONDS);
|
||||
|
||||
assertNotNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
verify(mockFuture);
|
||||
verify(schedulerMock);
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "timeUnit must not be null when using timeouts")
|
||||
public void testStartMonitoringWithNullTimeout() {
|
||||
ScheduledExecutorService schedulerMock = EasyMock.createMock(ScheduledExecutorService.class);
|
||||
AsyncMonitor<Object> monitor = mockMonitor(schedulerMock, new Object(), mockFunction(MonitorStatus.DONE),
|
||||
new EventBus());
|
||||
|
||||
monitor.startMonitoring(100L, null);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void testStartMonitoringWithoutTimeoutAndNullTimeUnit() {
|
||||
ScheduledFuture mockFuture = EasyMock.createMock(ScheduledFuture.class);
|
||||
ScheduledExecutorService schedulerMock = EasyMock.createMock(ScheduledExecutorService.class);
|
||||
expect(
|
||||
schedulerMock.scheduleWithFixedDelay(anyObject(Runnable.class), anyLong(), anyLong(),
|
||||
anyObject(TimeUnit.class))).andReturn(mockFuture);
|
||||
|
||||
replay(mockFuture);
|
||||
replay(schedulerMock);
|
||||
|
||||
AsyncMonitor<Object> monitor = mockMonitor(schedulerMock, new Object(), mockFunction(MonitorStatus.DONE),
|
||||
new EventBus());
|
||||
|
||||
assertNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
// If the maxWait parameter is null, timeUnit is not required
|
||||
monitor.startMonitoring(null, null);
|
||||
|
||||
assertNotNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
@ -95,7 +131,7 @@ public class AsyncMonitorTest {
|
|||
assertNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
monitor.startMonitoring(100L);
|
||||
monitor.startMonitoring(100L, TimeUnit.MILLISECONDS);
|
||||
|
||||
assertNotNull(monitor.getFuture());
|
||||
assertNotNull(monitor.getTimeout());
|
||||
|
@ -105,6 +141,33 @@ public class AsyncMonitorTest {
|
|||
verify(schedulerMock);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void testStartMonitoringWithTimeoutInMinutes() {
|
||||
ScheduledFuture mockFuture = EasyMock.createMock(ScheduledFuture.class);
|
||||
ScheduledExecutorService schedulerMock = EasyMock.createMock(ScheduledExecutorService.class);
|
||||
expect(
|
||||
schedulerMock.scheduleWithFixedDelay(anyObject(Runnable.class), anyLong(), anyLong(),
|
||||
anyObject(TimeUnit.class))).andReturn(mockFuture);
|
||||
|
||||
replay(mockFuture);
|
||||
replay(schedulerMock);
|
||||
|
||||
AsyncMonitor<Object> monitor = mockMonitor(schedulerMock, new Object(), mockFunction(MonitorStatus.DONE),
|
||||
new EventBus());
|
||||
|
||||
assertNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
monitor.startMonitoring(1L, TimeUnit.MINUTES);
|
||||
|
||||
assertNotNull(monitor.getFuture());
|
||||
assertNotNull(monitor.getTimeout());
|
||||
assertTrue(monitor.getTimeout() > TimeUnit.MINUTES.toMillis(1));
|
||||
|
||||
verify(mockFuture);
|
||||
verify(schedulerMock);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void testIsTimeoutWhenNullTimeout() {
|
||||
ScheduledFuture mockFuture = EasyMock.createMock(ScheduledFuture.class);
|
||||
|
@ -122,7 +185,7 @@ public class AsyncMonitorTest {
|
|||
assertNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
monitor.startMonitoring(null);
|
||||
monitor.startMonitoring(null, TimeUnit.MILLISECONDS);
|
||||
assertNotNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
assertFalse(monitor.isTimeout());
|
||||
|
@ -148,7 +211,7 @@ public class AsyncMonitorTest {
|
|||
assertNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
monitor.startMonitoring(60000L);
|
||||
monitor.startMonitoring(60000L, TimeUnit.MILLISECONDS);
|
||||
assertNotNull(monitor.getFuture());
|
||||
assertNotNull(monitor.getTimeout());
|
||||
assertFalse(monitor.isTimeout());
|
||||
|
@ -174,7 +237,7 @@ public class AsyncMonitorTest {
|
|||
assertNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
monitor.startMonitoring(1L);
|
||||
monitor.startMonitoring(1L, TimeUnit.MILLISECONDS);
|
||||
Thread.sleep(2L);
|
||||
assertNotNull(monitor.getFuture());
|
||||
assertNotNull(monitor.getTimeout());
|
||||
|
@ -203,7 +266,7 @@ public class AsyncMonitorTest {
|
|||
assertNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
monitor.startMonitoring(null);
|
||||
monitor.startMonitoring(null, TimeUnit.MILLISECONDS);
|
||||
assertNotNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
|
@ -233,7 +296,7 @@ public class AsyncMonitorTest {
|
|||
assertNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
monitor.startMonitoring(null);
|
||||
monitor.startMonitoring(null, TimeUnit.MILLISECONDS);
|
||||
assertNotNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
|
@ -264,7 +327,7 @@ public class AsyncMonitorTest {
|
|||
assertNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
monitor.startMonitoring(null);
|
||||
monitor.startMonitoring(null, TimeUnit.MILLISECONDS);
|
||||
assertNotNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
|
@ -297,7 +360,7 @@ public class AsyncMonitorTest {
|
|||
assertNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
monitor.startMonitoring(null);
|
||||
monitor.startMonitoring(null, TimeUnit.MILLISECONDS);
|
||||
assertNotNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
|
@ -333,7 +396,7 @@ public class AsyncMonitorTest {
|
|||
assertNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
monitor.startMonitoring(null);
|
||||
monitor.startMonitoring(null, TimeUnit.MILLISECONDS);
|
||||
assertNotNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
|
@ -367,7 +430,7 @@ public class AsyncMonitorTest {
|
|||
assertNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
monitor.startMonitoring(null);
|
||||
monitor.startMonitoring(null, TimeUnit.MILLISECONDS);
|
||||
assertNotNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
|
@ -403,7 +466,7 @@ public class AsyncMonitorTest {
|
|||
assertNull(monitor.getFuture());
|
||||
assertNull(monitor.getTimeout());
|
||||
|
||||
monitor.startMonitoring(1L);
|
||||
monitor.startMonitoring(1L, TimeUnit.MILLISECONDS);
|
||||
assertNotNull(monitor.getFuture());
|
||||
assertNotNull(monitor.getTimeout());
|
||||
|
||||
|
|
Loading…
Reference in New Issue