Committed by
Gerrit Code Review
Added test file
Change-Id: Ie1a24924c6fe6d9a522640c7155976524b755ee0
Showing
3 changed files
with
52 additions
and
20 deletions
| ... | @@ -41,11 +41,12 @@ import static org.slf4j.LoggerFactory.getLogger; | ... | @@ -41,11 +41,12 @@ import static org.slf4j.LoggerFactory.getLogger; |
| 41 | @Component(immediate = true) | 41 | @Component(immediate = true) |
| 42 | public class ComponentConfigLoader { | 42 | public class ComponentConfigLoader { |
| 43 | 43 | ||
| 44 | - private static final int RETRY_DELAY = 5_000; // millis between retries | ||
| 45 | private static final String CFG_JSON = "../config/component-cfg.json"; | 44 | private static final String CFG_JSON = "../config/component-cfg.json"; |
| 46 | - | ||
| 47 | static File cfgFile = new File(CFG_JSON); | 45 | static File cfgFile = new File(CFG_JSON); |
| 48 | 46 | ||
| 47 | + protected int retryDelay = 5_000; // millis between retries | ||
| 48 | + protected int stopRetryTime = 60_000; // deadline in millis | ||
| 49 | + | ||
| 49 | private final Logger log = getLogger(getClass()); | 50 | private final Logger log = getLogger(getClass()); |
| 50 | 51 | ||
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| ... | @@ -53,17 +54,19 @@ public class ComponentConfigLoader { | ... | @@ -53,17 +54,19 @@ public class ComponentConfigLoader { |
| 53 | 54 | ||
| 54 | private ObjectNode root; | 55 | private ObjectNode root; |
| 55 | private final Set<String> pendingComponents = Sets.newHashSet(); | 56 | private final Set<String> pendingComponents = Sets.newHashSet(); |
| 57 | + private long initialTimestamp; | ||
| 56 | 58 | ||
| 59 | + // TimerTask object that calls the load configuration for each component | ||
| 60 | + // in the pending components set and cancels itself if the set is empty or | ||
| 61 | + // after a set period of time. | ||
| 62 | + protected final TimerTask loader = new TimerTask() { | ||
| 57 | 63 | ||
| 58 | - /* TimerTask object that calls the load configuration for each component in the | ||
| 59 | - pending components set and cancels itself if the set is mpty. | ||
| 60 | - */ | ||
| 61 | - private final TimerTask loader = new TimerTask() { | ||
| 62 | @Override | 64 | @Override |
| 63 | public void run() { | 65 | public void run() { |
| 64 | ImmutableSet.copyOf(pendingComponents) | 66 | ImmutableSet.copyOf(pendingComponents) |
| 65 | .forEach(k -> loadConfig(k, (ObjectNode) root.path(k))); | 67 | .forEach(k -> loadConfig(k, (ObjectNode) root.path(k))); |
| 66 | - if (pendingComponents.isEmpty()) { | 68 | + if (pendingComponents.isEmpty() |
| 69 | + || System.currentTimeMillis() - initialTimestamp >= stopRetryTime) { | ||
| 67 | this.cancel(); | 70 | this.cancel(); |
| 68 | } | 71 | } |
| 69 | } | 72 | } |
| ... | @@ -71,19 +74,20 @@ public class ComponentConfigLoader { | ... | @@ -71,19 +74,20 @@ public class ComponentConfigLoader { |
| 71 | 74 | ||
| 72 | @Activate | 75 | @Activate |
| 73 | protected void activate() { | 76 | protected void activate() { |
| 77 | + initialTimestamp = System.currentTimeMillis(); | ||
| 74 | this.loadConfigs(); | 78 | this.loadConfigs(); |
| 75 | log.info("Started"); | 79 | log.info("Started"); |
| 76 | } | 80 | } |
| 77 | - /* loads the configurations for each component from the file in | 81 | + |
| 78 | - ../config/component-cfg.json, adds them to a set and schedules a task to try | 82 | + // Loads the configurations for each component from the file in |
| 79 | - and load them. | 83 | + // ../config/component-cfg.json, adds them to a set and schedules a task |
| 80 | - */ | 84 | + // to try and load them. |
| 81 | private void loadConfigs() { | 85 | private void loadConfigs() { |
| 82 | try { | 86 | try { |
| 83 | if (cfgFile.exists()) { | 87 | if (cfgFile.exists()) { |
| 84 | root = (ObjectNode) new ObjectMapper().readTree(cfgFile); | 88 | root = (ObjectNode) new ObjectMapper().readTree(cfgFile); |
| 85 | root.fieldNames().forEachRemaining(pendingComponents::add); | 89 | root.fieldNames().forEachRemaining(pendingComponents::add); |
| 86 | - SharedExecutors.getTimer().schedule(loader, 0, RETRY_DELAY); | 90 | + SharedExecutors.getTimer().schedule(loader, 0, retryDelay); |
| 87 | log.info("Loaded initial component configuration from {}", cfgFile); | 91 | log.info("Loaded initial component configuration from {}", cfgFile); |
| 88 | } | 92 | } |
| 89 | } catch (Exception e) { | 93 | } catch (Exception e) { |
| ... | @@ -91,10 +95,9 @@ public class ComponentConfigLoader { | ... | @@ -91,10 +95,9 @@ public class ComponentConfigLoader { |
| 91 | cfgFile, e); | 95 | cfgFile, e); |
| 92 | } | 96 | } |
| 93 | } | 97 | } |
| 94 | - /* | 98 | + |
| 95 | - * loads a configuration for a single component and removes it from the | 99 | + // Loads a configuration for a single component and removes it from the |
| 96 | - * components set | 100 | + // components set. |
| 97 | - */ | ||
| 98 | private void loadConfig(String component, ObjectNode config) { | 101 | private void loadConfig(String component, ObjectNode config) { |
| 99 | if (configService.getComponentNames().contains(component)) { | 102 | if (configService.getComponentNames().contains(component)) { |
| 100 | config.fieldNames() | 103 | config.fieldNames() | ... | ... |
| ... | @@ -24,12 +24,13 @@ import org.onosproject.cfg.ComponentConfigAdapter; | ... | @@ -24,12 +24,13 @@ import org.onosproject.cfg.ComponentConfigAdapter; |
| 24 | 24 | ||
| 25 | import java.io.File; | 25 | import java.io.File; |
| 26 | import java.io.IOException; | 26 | import java.io.IOException; |
| 27 | +import java.lang.reflect.Field; | ||
| 27 | import java.util.Set; | 28 | import java.util.Set; |
| 29 | +import java.util.TimerTask; | ||
| 28 | 30 | ||
| 29 | import static com.google.common.io.ByteStreams.toByteArray; | 31 | import static com.google.common.io.ByteStreams.toByteArray; |
| 30 | import static com.google.common.io.Files.write; | 32 | import static com.google.common.io.Files.write; |
| 31 | -import static org.junit.Assert.assertEquals; | 33 | +import static org.junit.Assert.*; |
| 32 | -import static org.junit.Assert.assertNull; | ||
| 33 | import static org.onlab.junit.TestTools.assertAfter; | 34 | import static org.onlab.junit.TestTools.assertAfter; |
| 34 | 35 | ||
| 35 | /** | 36 | /** |
| ... | @@ -55,6 +56,8 @@ public class ComponentConfigLoaderTest { | ... | @@ -55,6 +56,8 @@ public class ComponentConfigLoaderTest { |
| 55 | loader = new ComponentConfigLoader(); | 56 | loader = new ComponentConfigLoader(); |
| 56 | service = new TestConfigService(); | 57 | service = new TestConfigService(); |
| 57 | loader.configService = service; | 58 | loader.configService = service; |
| 59 | + loader.retryDelay = 50; | ||
| 60 | + loader.stopRetryTime = 200; | ||
| 58 | } | 61 | } |
| 59 | 62 | ||
| 60 | /* | 63 | /* |
| ... | @@ -70,13 +73,34 @@ public class ComponentConfigLoaderTest { | ... | @@ -70,13 +73,34 @@ public class ComponentConfigLoaderTest { |
| 70 | /* | 73 | /* |
| 71 | * Tests that the component is null if the file has a bad configuration format | 74 | * Tests that the component is null if the file has a bad configuration format |
| 72 | * for which it yielded an exception. Can't test the exception because it happens | 75 | * for which it yielded an exception. Can't test the exception because it happens |
| 73 | - * in a different thread, | 76 | + * in a different thread. |
| 74 | */ | 77 | */ |
| 75 | @Test | 78 | @Test |
| 76 | public void badConfig() throws IOException { | 79 | public void badConfig() throws IOException { |
| 77 | stageTestResource("badConfig.json"); | 80 | stageTestResource("badConfig.json"); |
| 78 | loader.activate(); | 81 | loader.activate(); |
| 79 | - assertAfter(1_000, () -> assertNull("incorrect component", service.component)); | 82 | + assertAfter(1_000, () -> assertNull("incorrect configuration", service.component)); |
| 83 | + | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + /* | ||
| 87 | + * Tests that tasks stops itself after the stopRetryTime if the component was | ||
| 88 | + * not loaded. | ||
| 89 | + */ | ||
| 90 | + @Test | ||
| 91 | + public void noComponentForConfig() throws IOException { | ||
| 92 | + stageTestResource("badComponent.json"); | ||
| 93 | + loader.activate(); | ||
| 94 | + assertAfter(loader.stopRetryTime + loader.retryDelay, () -> { | ||
| 95 | + try { | ||
| 96 | + Field state = TimerTask.class.getDeclaredField("state"); | ||
| 97 | + state.setAccessible(true); | ||
| 98 | + assertEquals("incorrect component", state.getInt(loader.loader), 3); | ||
| 99 | + } catch (Exception e) { | ||
| 100 | + e.printStackTrace(); | ||
| 101 | + fail(); | ||
| 102 | + } | ||
| 103 | + }); | ||
| 80 | 104 | ||
| 81 | } | 105 | } |
| 82 | 106 | ... | ... |
-
Please register or login to post a comment