def test_prepare_inits_daemon_variable(self, tcp_server, caplog):
        with self._tcp_patcher(tcp_server), caplog.atLevel(logging.DEBUG):
            daemon = hooks_daemon.HttpHooksCallbackDaemon()
        assert daemon._daemon == tcp_server

        assert_message_in_log(
            caplog.records(),
            'Preparing callback daemon and registering hook object',
            levelno=logging.DEBUG,
            module='hooks_daemon')
    def test_prepare_inits_hooks_uri_and_logs_it(self, tcp_server, caplog):
        with self._tcp_patcher(tcp_server), caplog.atLevel(logging.DEBUG):
            daemon = hooks_daemon.HttpHooksCallbackDaemon()

        _, port = tcp_server.server_address
        expected_uri = '{}:{}'.format(daemon.IP_ADDRESS, port)
        assert daemon.hooks_uri == expected_uri

        assert_message_in_log(caplog.records(),
                              'Hooks uri is: {}'.format(expected_uri),
                              levelno=logging.DEBUG,
                              module='hooks_daemon')
    def test_run_logs(self, tcp_server, caplog):

        with self._tcp_patcher(tcp_server):
            daemon = hooks_daemon.HttpHooksCallbackDaemon()

        with self._thread_patcher(mock.Mock()), caplog.atLevel(logging.DEBUG):
            daemon._run()

        assert_message_in_log(
            caplog.records(),
            'Running event loop of callback daemon in background thread',
            levelno=logging.DEBUG,
            module='hooks_daemon')
    def test_log_message_writes_to_debug_log(self, caplog):
        ip_port = ('0.0.0.0', 8888)
        handler = hooks_daemon.HooksHttpHandler(MockRequest('POST /'), ip_port,
                                                mock.Mock())
        fake_date = '1/Nov/2015 00:00:00'
        date_patcher = mock.patch.object(handler,
                                         'log_date_time_string',
                                         return_value=fake_date)
        with date_patcher, caplog.atLevel(logging.DEBUG):
            handler.log_message('Some message %d, %s', 123, 'string')

        expected_message = '{} - - [{}] Some message 123, string'.format(
            ip_port[0], fake_date)
        assert_message_in_log(caplog.records(),
                              expected_message,
                              levelno=logging.DEBUG,
                              module='hooks_daemon')
    def test_prepare_inits_pyro4_and_registers_hooks(self, caplog):
        pyro4_daemon = mock.Mock()

        with self._pyro4_patcher(pyro4_daemon), caplog.atLevel(logging.DEBUG):
            daemon = hooks_daemon.Pyro4HooksCallbackDaemon()

        assert daemon._daemon == pyro4_daemon

        assert pyro4_daemon.register.call_count == 1
        args, kwargs = pyro4_daemon.register.call_args
        assert len(args) == 1
        assert isinstance(args[0], hooks_daemon.Hooks)

        assert_message_in_log(
            caplog.records(),
            'Preparing callback daemon and registering hook object',
            levelno=logging.DEBUG,
            module='hooks_daemon')
    def test_stop_cleans_up_the_connection(self, tcp_server, caplog):
        thread = mock.Mock()

        with self._tcp_patcher(tcp_server):
            daemon = hooks_daemon.HttpHooksCallbackDaemon()

        with self._thread_patcher(thread), caplog.atLevel(logging.DEBUG):
            with daemon:
                assert daemon._daemon == tcp_server
                assert daemon._callback_thread == thread

        assert daemon._daemon is None
        assert daemon._callback_thread is None
        tcp_server.shutdown.assert_called_with()
        thread.join.assert_called_once_with()

        assert_message_in_log(caplog.records(),
                              'Waiting for background thread to finish.',
                              levelno=logging.DEBUG,
                              module='hooks_daemon')
    def test_stop_cleans_up_the_connection(self, caplog):
        thread = mock.Mock()
        pyro4_daemon = mock.Mock()

        with self._pyro4_patcher(pyro4_daemon):
            daemon = hooks_daemon.Pyro4HooksCallbackDaemon()

        with self._thread_patcher(thread), caplog.atLevel(logging.DEBUG):
            with daemon:
                assert daemon._daemon == pyro4_daemon
                assert daemon._callback_thread == thread

        assert daemon._daemon is None
        assert daemon._callback_thread is None
        pyro4_daemon.close.assert_called_with()
        thread.join.assert_called_once_with()

        assert_message_in_log(caplog.records(),
                              'Waiting for background thread to finish.',
                              levelno=logging.DEBUG,
                              module='hooks_daemon')