@Test
public void testConsoleManagerWhenExceedingLimit() {
// Get all console
int initialSize = manager.getAvailableConsoleSize();
SingleConsole availableConsole = null;
for (int i = 1; i <= initialSize; i++) {
availableConsole = manager.getAvailableConsole(
ConsolePropertiesFactory.createEmptyConsoleProperties());
}
final SingleConsole lastConsole = availableConsole;
assertThat(manager.getAvailableConsoleSize(), is(0));
StopWatch elapseTime = new StopWatch();
elapseTime.start();
// Try to get more console, it will take time
try {
manager.getAvailableConsole(ConsolePropertiesFactory.createEmptyConsoleProperties());
fail("should throw Exception");
} catch (NGrinderRuntimeException e) {
}
elapseTime.stop();
assertThat(elapseTime.getTotalTimeSeconds(), lessThan(3000D));
// Let's try the case when console is returned back.
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
manager.returnBackConsole("test", lastConsole);
} catch (InterruptedException e) {
}
}
});
elapseTime = new StopWatch();
elapseTime.start();
thread.start();
// Try to get more console, it will return console just after console is
// returned back
SingleConsole anotherConsole = manager.getAvailableConsole(
ConsolePropertiesFactory.createEmptyConsoleProperties());
elapseTime.stop();
assertThat(elapseTime.getTotalTimeSeconds(), lessThan(3000D));
assertThat(manager.getAvailableConsoleSize(), is(0));
manager.returnBackConsole("test", anotherConsole);