Added unit tests for the timeunit field and fixed javadoc

This commit is contained in:
Ignasi Barrera 2012-10-24 19:38:11 +02:00
parent 25bd57379d
commit 7bd6b016ab
2 changed files with 69 additions and 4 deletions

View File

@ -176,8 +176,7 @@ public class BaseMonitoringService implements MonitoringService {
* Performs the periodical monitoring tasks. * Performs the periodical monitoring tasks.
* *
* @author Ignasi Barrera * @author Ignasi Barrera
* @param <T> * @param <T> The type of the object being monitored.
* The type of the object being monitored.
*/ */
@VisibleForTesting @VisibleForTesting
class AsyncMonitor<T> implements Runnable { class AsyncMonitor<T> implements Runnable {
@ -205,10 +204,13 @@ public class BaseMonitoringService implements MonitoringService {
/** /**
* Starts the monitoring job with the given timeout. * Starts the monitoring job with the given timeout.
* *
* @param maxWait * @param maxWait The timeout.
* The timeout. * @param timeUnit The timeunit used in the maxWait parameter.
*/ */
public void startMonitoring(final Long maxWait, TimeUnit timeUnit) { 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); future = scheduler.scheduleWithFixedDelay(this, 0L, pollingDelay, TimeUnit.MILLISECONDS);
timeout = maxWait == null ? null : System.currentTimeMillis() + timeUnit.toMillis(maxWait); timeout = maxWait == null ? null : System.currentTimeMillis() + timeUnit.toMillis(maxWait);
logger.debug("started monitor job for %s with %s timeout", monitoredObject, logger.debug("started monitor job for %s with %s timeout", monitoredObject,

View File

@ -78,6 +78,42 @@ public class AsyncMonitorTest {
verify(schedulerMock); 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());
verify(mockFuture);
verify(schedulerMock);
}
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({ "rawtypes", "unchecked" })
public void testStartMonitoringWithTimeout() { public void testStartMonitoringWithTimeout() {
ScheduledFuture mockFuture = EasyMock.createMock(ScheduledFuture.class); ScheduledFuture mockFuture = EasyMock.createMock(ScheduledFuture.class);
@ -105,6 +141,33 @@ public class AsyncMonitorTest {
verify(schedulerMock); 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" }) @SuppressWarnings({ "rawtypes", "unchecked" })
public void testIsTimeoutWhenNullTimeout() { public void testIsTimeoutWhenNullTimeout() {
ScheduledFuture mockFuture = EasyMock.createMock(ScheduledFuture.class); ScheduledFuture mockFuture = EasyMock.createMock(ScheduledFuture.class);