Commit b95a8986 authored by aninhacostaribeiro's avatar aninhacostaribeiro
Browse files

New tests for Default GreenStrategy

parent 92fa2db7
......@@ -11,3 +11,5 @@ host.name = agent
green.TurnOffCommand = pm-suspend
green.sleepingTime = 300000
green.threadTime = 60
\ No newline at end of file
......@@ -12,3 +12,5 @@ greenstrategy.gracetime = 1800
greenstrategy.expirationtime = 300
wol.broadcast.address = 123.123.255.255
green.threadTime = 60
\ No newline at end of file
......@@ -34,8 +34,19 @@ public class DefaultGreenStrategy implements GreenStrategy {
private long graceTime;
private long expirationTime;
private int threadTime;
private Runnable runnable = new Runnable() {
@Override
public void run() {
try {
checkExpiredHosts();
sendIdleHostsToBed();
} catch (Exception e) {
LOGGER.warn("Exception thrown at the main thread", e);
}
}
};
private ScheduledExecutorService executorService = Executors
protected ScheduledExecutorService executorService = Executors
.newScheduledThreadPool(1);
public DefaultGreenStrategy(Properties prop) {
......@@ -70,6 +81,14 @@ public class DefaultGreenStrategy implements GreenStrategy {
protected void setExpirationTime(long lostHostTime) {
this.expirationTime = lostHostTime;
}
protected void setExecutorService(ScheduledExecutorService executorService) {
this.executorService = executorService;
}
protected void setRunnable(Runnable runnable) {
this.runnable = runnable;
}
protected void setAllHosts(List<Host> hosts) {
this.hostsAwake = hosts;
......@@ -78,6 +97,9 @@ public class DefaultGreenStrategy implements GreenStrategy {
protected void setDateWrapper(DateWrapper dateWrapper) {
this.dateWrapper = dateWrapper;
}
protected int getThreadTime() {
return threadTime;
}
public void setCommunicationComponent(ServerCommunicationComponent gscc) {
this.scc = gscc;
......@@ -215,16 +237,8 @@ public class DefaultGreenStrategy implements GreenStrategy {
}
public void start() {
executorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
try {
checkExpiredHosts();
sendIdleHostsToBed();
} catch (Exception e) {
LOGGER.warn("Exception thrown at the main thread", e);
}
}
}, 0, threadTime, TimeUnit.SECONDS);
executorService.scheduleWithFixedDelay(this.runnable, 0, threadTime, TimeUnit.SECONDS);
}
}
......@@ -3,6 +3,9 @@ package org.fogbowcloud.green.server.core.greenStrategy;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.fogbowcloud.green.server.communication.ServerCommunicationComponent;
import org.fogbowcloud.green.server.core.plugins.openstack.OpenStackInfoPlugin;
......@@ -23,6 +26,49 @@ public class TestDefaultGreenStrategy {
Mockito.when(date.getTime()).thenReturn(Time);
return date;
}
private Properties createBasicProperties() {
Properties prop = new Properties();
prop.put("openstack.endpoint", "endpoint");
prop.put("openstack.username", "username");
prop.put("openstack.password", "password");
prop.put("openstack.tenant", "tenant");
prop.put("greenstrategy.gracetime", "1");
prop.put("greenstrategy.expirationtime", "1");
return prop;
}
@Test
public void testPropertiesWithoutThreadTime() {
Properties prop = createBasicProperties();
DefaultGreenStrategy dgs = new DefaultGreenStrategy(prop);
Assert.assertEquals(60, dgs.getThreadTime());
}
@Test
public void testPropertiesWithThreadTime() {
Properties prop = createBasicProperties();
prop.put("green.threadTime", "10");
DefaultGreenStrategy dgs = new DefaultGreenStrategy(prop);
Assert.assertEquals(10, dgs.getThreadTime());
}
@Test
public void testStartMethod() {
Properties prop = createBasicProperties();
final DefaultGreenStrategy dgs = new DefaultGreenStrategy(prop);
Runnable runnable = new Runnable() {
@Override
public void run() {
}
};
dgs.setRunnable(runnable);
ScheduledExecutorService executorService = Mockito.mock(ScheduledExecutorService.class);
dgs.setExecutorService(executorService);
dgs.start();
Mockito.verify(executorService).scheduleWithFixedDelay(runnable, 0, 60, TimeUnit.SECONDS);
}
@Test
public void testCheckExpiredHots() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment