Exemple #1
0
def test_reload():

    config_manager.CONFIG_FILE = 'test-config.1.yaml'
    _dir = os.path.dirname(os.path.abspath(__file__))
    config1 = {"logging": {"level": "INFO"}}
    config_file = os.path.join(_dir, config_manager.CONFIG_FILE)

    # write 1
    fileutils.save(config_file, config1)

    loaded_config = config_manager.load(_dir)

    assert config1["logging"]["level"] == loaded_config["logging"]["level"]

    watcher = Watch()

    config_manager.register_handler(watcher.on_change)

    # write 2
    config2 = {"logging": {"level": "WARN"}}
    fileutils.save(config_file, config2)

    # wait for polling to happen
    wait = 3
    while not watcher.changed:
        sleep(.5)
        wait -= 1
        if wait == 0:
            raise Exception("Failed to detect change")

    assert loaded_config["logging"]["level"] == watcher.config["logging"][
        "level"]
    assert loaded_config["logging"]["level"] == config2["logging"]["level"]
Exemple #2
0
def test_callback():

    config_manager.CONFIG_FILE = 'test-config.2.yaml'
    _dir = os.path.dirname(os.path.abspath(__file__))
    config1 = {"test": True}
    config_file = os.path.join(_dir, config_manager.CONFIG_FILE)

    fileutils.save(config_file, config1)
    config_manager.load(_dir)

    watcher = Watch()

    config_manager.register_handler(watcher.on_change)
    fileutils.save(config_file, config1)

    # wait for polling to happen
    wait = 3
    while not watcher.changed:
        sleep(.5)
        wait -= 1
        if wait == 0:
            raise Exception("Failed to detect change")

    assert watcher.changed
    def save(self):
        """Save configuration to file"""
        if self.get() is None:
            return

        fileutils.save(self.get_config_file(), self.get())