コード例 #1
0
    def test_register_on_stop_callback(self):
        self.callback_called = False

        def fake_callback():
            self.callback_called = True

        run_state = scalyr_util.RunState()
        run_state.register_on_stop_callback(fake_callback)
        run_state.stop()
        self.assertTrue(self.callback_called)
コード例 #2
0
    def create_test_monitors_manager(
        config_monitors=None,
        platform_monitors=None,
        extra_toplevel_config=None,
        null_logger=False,
        fake_clock=False,
    ):
        """Create a test MonitorsManager

        @param config_monitors: config monitors
        @param platform_monitors: platform monitors
        @param extra_toplevel_config: dict of extra top-level key value objects
        @param null_logger: If True, set all monitors to log to Nullhandler
        @param fake_clock: If non-null, the manager and all it's monitors' _run_state's will use the provided fake_clock
        """
        monitors_json_array = JsonArray()
        if config_monitors:
            for entry in config_monitors:
                monitors_json_array.add(JsonObject(content=entry))

        extras = {"monitors": monitors_json_array}
        if extra_toplevel_config:
            extras.update(extra_toplevel_config)

        config = ScalyrTestUtils.create_configuration(
            extra_toplevel_config=extras)
        config.parse()

        if not platform_monitors:
            platform_monitors = []
        # noinspection PyTypeChecker
        test_manager = MonitorsManager(config, FakePlatform(platform_monitors))

        if null_logger:
            # Override Agent Logger to prevent writing to disk
            for monitor in test_manager.monitors:
                monitor._logger = FakeAgentLogger("fake_agent_logger")

        if fake_clock:
            for monitor in test_manager.monitors + [test_manager]:
                monitor._run_state = scalyr_util.RunState(
                    fake_clock=fake_clock)

        # AGENT-113 set all monitors and the monitors_manager threads to daemon to eliminate occasionally hanging tests
        test_manager.setDaemon(True)
        for monitor in test_manager.monitors:
            if isinstance(monitor, threading.Thread):
                monitor.setDaemon(True)

        return test_manager, config