andrea
Committed by Gerrit Code Review

Added test file

Change-Id: Ie1a24924c6fe6d9a522640c7155976524b755ee0
......@@ -41,11 +41,12 @@ import static org.slf4j.LoggerFactory.getLogger;
@Component(immediate = true)
public class ComponentConfigLoader {
private static final int RETRY_DELAY = 5_000; // millis between retries
private static final String CFG_JSON = "../config/component-cfg.json";
static File cfgFile = new File(CFG_JSON);
protected int retryDelay = 5_000; // millis between retries
protected int stopRetryTime = 60_000; // deadline in millis
private final Logger log = getLogger(getClass());
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
......@@ -53,17 +54,19 @@ public class ComponentConfigLoader {
private ObjectNode root;
private final Set<String> pendingComponents = Sets.newHashSet();
private long initialTimestamp;
// TimerTask object that calls the load configuration for each component
// in the pending components set and cancels itself if the set is empty or
// after a set period of time.
protected final TimerTask loader = new TimerTask() {
/* TimerTask object that calls the load configuration for each component in the
pending components set and cancels itself if the set is mpty.
*/
private final TimerTask loader = new TimerTask() {
@Override
public void run() {
ImmutableSet.copyOf(pendingComponents)
.forEach(k -> loadConfig(k, (ObjectNode) root.path(k)));
if (pendingComponents.isEmpty()) {
if (pendingComponents.isEmpty()
|| System.currentTimeMillis() - initialTimestamp >= stopRetryTime) {
this.cancel();
}
}
......@@ -71,19 +74,20 @@ public class ComponentConfigLoader {
@Activate
protected void activate() {
initialTimestamp = System.currentTimeMillis();
this.loadConfigs();
log.info("Started");
}
/* loads the configurations for each component from the file in
../config/component-cfg.json, adds them to a set and schedules a task to try
and load them.
*/
// Loads the configurations for each component from the file in
// ../config/component-cfg.json, adds them to a set and schedules a task
// to try and load them.
private void loadConfigs() {
try {
if (cfgFile.exists()) {
root = (ObjectNode) new ObjectMapper().readTree(cfgFile);
root.fieldNames().forEachRemaining(pendingComponents::add);
SharedExecutors.getTimer().schedule(loader, 0, RETRY_DELAY);
SharedExecutors.getTimer().schedule(loader, 0, retryDelay);
log.info("Loaded initial component configuration from {}", cfgFile);
}
} catch (Exception e) {
......@@ -91,10 +95,9 @@ public class ComponentConfigLoader {
cfgFile, e);
}
}
/*
* loads a configuration for a single component and removes it from the
* components set
*/
// Loads a configuration for a single component and removes it from the
// components set.
private void loadConfig(String component, ObjectNode config) {
if (configService.getComponentNames().contains(component)) {
config.fieldNames()
......
......@@ -24,12 +24,13 @@ import org.onosproject.cfg.ComponentConfigAdapter;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Set;
import java.util.TimerTask;
import static com.google.common.io.ByteStreams.toByteArray;
import static com.google.common.io.Files.write;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.*;
import static org.onlab.junit.TestTools.assertAfter;
/**
......@@ -55,6 +56,8 @@ public class ComponentConfigLoaderTest {
loader = new ComponentConfigLoader();
service = new TestConfigService();
loader.configService = service;
loader.retryDelay = 50;
loader.stopRetryTime = 200;
}
/*
......@@ -70,13 +73,34 @@ public class ComponentConfigLoaderTest {
/*
* Tests that the component is null if the file has a bad configuration format
* for which it yielded an exception. Can't test the exception because it happens
* in a different thread,
* in a different thread.
*/
@Test
public void badConfig() throws IOException {
stageTestResource("badConfig.json");
loader.activate();
assertAfter(1_000, () -> assertNull("incorrect component", service.component));
assertAfter(1_000, () -> assertNull("incorrect configuration", service.component));
}
/*
* Tests that tasks stops itself after the stopRetryTime if the component was
* not loaded.
*/
@Test
public void noComponentForConfig() throws IOException {
stageTestResource("badComponent.json");
loader.activate();
assertAfter(loader.stopRetryTime + loader.retryDelay, () -> {
try {
Field state = TimerTask.class.getDeclaredField("state");
state.setAccessible(true);
assertEquals("incorrect component", state.getInt(loader.loader), 3);
} catch (Exception e) {
e.printStackTrace();
fail();
}
});
}
......
{
"org.onosproject.proxyarp.ProxyArp2": {
"testProperty": true
}
}
\ No newline at end of file