Merge pull request #871 from abiquo/tests-timeout

Refactored unit tests to avoid recurrent timeouts
This commit is contained in:
Adrian Cole 2012-10-10 05:55:08 -07:00
commit 2badf2241d
1 changed files with 10 additions and 161 deletions

View File

@ -19,15 +19,8 @@
package org.jclouds.abiquo.internal; package org.jclouds.abiquo.internal;
import static org.jclouds.abiquo.config.AbiquoProperties.ASYNC_TASK_MONITOR_DELAY;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.jclouds.abiquo.events.handlers.BlockingEventHandler;
import org.jclouds.abiquo.events.monitor.MonitorEvent;
import org.jclouds.abiquo.features.services.MonitoringService; import org.jclouds.abiquo.features.services.MonitoringService;
import org.jclouds.abiquo.monitor.MonitorStatus; import org.jclouds.abiquo.monitor.MonitorStatus;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -39,22 +32,9 @@ import com.google.common.base.Function;
* *
* @author Ignasi Barrera * @author Ignasi Barrera
*/ */
// Since these tests block the thread, mark them as failed after the given timeout @Test(groups = "unit", testName = "BaseMonitoringServiceTest")
@Test(groups = "unit", testName = "BaseMonitoringServiceTest", timeOut = 10000L)
public class BaseMonitoringServiceTest extends BaseInjectionTest public class BaseMonitoringServiceTest extends BaseInjectionTest
{ {
// The polling interval used in tests (in ms)
private static final long TEST_MONITOR_POLLING = 100L;
@Override
protected Properties buildProperties()
{
// Use a small monitor polling interval in tests (in ms)
Properties props = super.buildProperties();
props.setProperty(ASYNC_TASK_MONITOR_DELAY, String.valueOf(TEST_MONITOR_POLLING));
return props;
}
public void testAllPropertiesInjected() public void testAllPropertiesInjected()
{ {
BaseMonitoringService service = BaseMonitoringService service =
@ -81,104 +61,15 @@ public class BaseMonitoringServiceTest extends BaseInjectionTest
service.awaitCompletion(new MockMonitor(), new Object[] {}); service.awaitCompletion(new MockMonitor(), new Object[] {});
} }
public void testAwaitCompletion()
{
BaseMonitoringService service = monitoringService();
service.awaitCompletion(new MockMonitor(), new Object());
}
public void testAwaitCompletionMultipleTasks()
{
BaseMonitoringService service = monitoringService();
service.awaitCompletion(new MockMonitor(), new Object(), new Object());
}
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)
public void testMonitorWithNullCompletecondition() public void testMonitorWithNullCompleteCondition()
{ {
monitoringService().monitor(null, (Object[]) null); monitoringService().monitor(null, (Object[]) null);
} }
@Test(expectedExceptions = IllegalArgumentException.class) public void testMonitorWithoutTasks()
public void testBlockingHandlerWithoutArguments()
{ {
new BlockingEventHandler<Object>(); monitoringService().monitor(new MockMonitor());
}
public void testMonitor()
{
BaseMonitoringService service = monitoringService();
Object monitoredObject = new Object();
CountingHandler handler = new CountingHandler(monitoredObject);
service.register(handler);
service.monitor(new MockMonitor(), monitoredObject);
handler.lock();
service.unregister(handler);
assertEquals(handler.numCompletes, 1);
assertEquals(handler.numFailures, 0);
assertEquals(handler.numTimeouts, 0);
}
public void testMonitorMultipleTasks()
{
BaseMonitoringService service = monitoringService();
Object monitoredObject1 = new Object();
Object monitoredObject2 = new Object();
CountingHandler handler = new CountingHandler(monitoredObject1, monitoredObject2);
service.register(handler);
service.monitor(new MockMonitor(), monitoredObject1, monitoredObject2);
handler.lock();
service.unregister(handler);
assertEquals(handler.numCompletes, 2);
assertEquals(handler.numFailures, 0);
assertEquals(handler.numTimeouts, 0);
}
public void testMonitorReachesTimeout()
{
BaseMonitoringService service = monitoringService();
Object monitoredObject = new Object();
CountingHandler handler = new CountingHandler(monitoredObject);
service.register(handler);
service.monitor(TEST_MONITOR_POLLING + 10L, TimeUnit.MILLISECONDS,
new MockInfiniteMonitor(), monitoredObject);
handler.lock();
service.unregister(handler);
assertEquals(handler.numCompletes, 0);
assertEquals(handler.numFailures, 0);
assertEquals(handler.numTimeouts, 1);
}
public void testMonitorMultipleTasksReachesTimeout()
{
BaseMonitoringService service = monitoringService();
Object monitoredObject1 = new Object();
Object monitoredObject2 = new Object();
CountingHandler handler = new CountingHandler(monitoredObject1, monitoredObject2);
service.register(handler);
service.monitor(TEST_MONITOR_POLLING + 10L, TimeUnit.MILLISECONDS,
new MockInfiniteMonitor(), monitoredObject1, monitoredObject2);
handler.lock();
service.unregister(handler);
assertEquals(handler.numCompletes, 0);
assertEquals(handler.numFailures, 0);
assertEquals(handler.numTimeouts, 2);
} }
public void testDelegateToVirtualMachineMonitor() public void testDelegateToVirtualMachineMonitor()
@ -196,6 +87,11 @@ public class BaseMonitoringServiceTest extends BaseInjectionTest
assertNotNull(monitoringService().getAsyncTaskMonitor()); assertNotNull(monitoringService().getAsyncTaskMonitor());
} }
public void testDelegateToConversioMonitor()
{
assertNotNull(monitoringService().getConversionMonitor());
}
private BaseMonitoringService monitoringService() private BaseMonitoringService monitoringService()
{ {
return injector.getInstance(BaseMonitoringService.class); return injector.getInstance(BaseMonitoringService.class);
@ -203,57 +99,10 @@ public class BaseMonitoringServiceTest extends BaseInjectionTest
private static class MockMonitor implements Function<Object, MonitorStatus> private static class MockMonitor implements Function<Object, MonitorStatus>
{ {
private int finishAfterCount;
public MockMonitor()
{
this.finishAfterCount = 1; // Simulate task completion after one refresh
}
@Override @Override
public MonitorStatus apply(final Object object) public MonitorStatus apply(final Object object)
{ {
return finishAfterCount-- <= 0 ? MonitorStatus.DONE : MonitorStatus.CONTINUE; return MonitorStatus.DONE;
}
}
private static class MockInfiniteMonitor implements Function<Object, MonitorStatus>
{
@Override
public MonitorStatus apply(final Object object)
{
return MonitorStatus.CONTINUE;
}
}
private static class CountingHandler extends BlockingEventHandler<Object>
{
public int numCompletes = 0;
public int numFailures = 0;
public int numTimeouts = 0;
public CountingHandler(final Object... lockedObjects)
{
super(lockedObjects);
}
@Override
protected void doBeforeRelease(final MonitorEvent<Object> event)
{
switch (event.getType())
{
case COMPLETED:
numCompletes++;
break;
case FAILED:
numFailures++;
break;
case TIMEOUT:
numTimeouts++;
break;
}
} }
} }