Skip to content

Commit

Permalink
Add test for dynamic updates, keep timestamps as floats
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-bates committed May 2, 2019
1 parent 563b6be commit 7212ab8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
4 changes: 2 additions & 2 deletions traitlets/config/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def __init__(self, **kwargs):
else:
self.classes.insert(0, self.__class__)

self.last_config_update = int(time.time())
self.last_config_update = time.time()
self.dynamic_configurables = {}

@observe('config')
Expand Down Expand Up @@ -832,7 +832,7 @@ def _config_files_updated(self):
"""
updated = False
for file in self._loaded_config_files:
mod_time = int(os.path.getmtime(file))
mod_time = os.path.getmtime(file)
if mod_time > self.last_config_update:
self.log.debug("Config file was updated: {}!".format(file))
self.last_config_update = mod_time
Expand Down
40 changes: 39 additions & 1 deletion traitlets/config/tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ def test_warn_autocorrect(self):
self.assertIn("warn_typo", stream.getvalue())
self.assertIn("warn_tpyo", stream.getvalue())


def test_flatten_flags(self):
cfg = Config()
cfg.MyApp.log_level = logging.WARN
Expand Down Expand Up @@ -547,6 +546,45 @@ def test_subcommands_instanciation(self):
self.assertIs(app.subapp.parent, app)
self.assertIs(app.subapp.subapp.parent, app.subapp) # Set by factory.

def test_dynamic_updates(self):
app = MyApp()
app.log = logging.getLogger()
name = 'config.py'
with TemporaryDirectory('_1') as td1:
with open(pjoin(td1, name), 'w') as f:
f.writelines([
"c.MyApp.running = True\n",
"c.MyApp.Bar.b = 1\n"
])

app.load_config_file(name, path=[td1])
app.init_bar()
app.add_dynamic_configurable("MyApp", app)
app.add_dynamic_configurable("Bar", app.bar)
with self.assertRaises(RuntimeError):
app.add_dynamic_configurable("Bogus", app.log)

app.start()
self.assertEqual(app.running, True)
self.assertEqual(app.bar.b, 1)

# update config file
with open(pjoin(td1, name), 'w') as f:
f.writelines([
"c.MyApp.running = False\n",
"c.MyApp.Bar.b = 2\n"
])

# trigger reload and verify updates
app.update_dynamic_configurables()
self.assertEqual(app.running, False)
self.assertEqual(app.bar.b, 2)

# repeat to ensure no unexpected changes occurred
app.update_dynamic_configurables()
self.assertEqual(app.running, False)
self.assertEqual(app.bar.b, 2)


class Root(Application):
subcommands = {
Expand Down

0 comments on commit 7212ab8

Please sign in to comment.