def _start_dispatch(self):
        """Start a thread to dispatch requests queued up by callbacks.

        .. note::

            This assumes, but does not check, that ``_dispatch_thread``
            is :data:`None`.

        Spawns a thread to run :meth:`dispatch_callback` and sets the
        "dispatch thread" member on the current policy.
        """
        _LOGGER.debug('Starting callback requests worker.')
        dispatch_worker = _helper_threads.QueueCallbackWorker(
            self._request_queue,
            self.dispatch_callback,
        )
        # Create and start the helper thread.
        thread = threading.Thread(
            name=_CALLBACK_WORKER_NAME,
            target=dispatch_worker,
        )
        thread.daemon = True
        thread.start()
        _LOGGER.debug('Started helper thread %s', thread.name)
        self._dispatch_thread = thread
Пример #2
0
def test_queue_callback_worker_exception():
    queue_ = queue.Queue()
    callback = mock.Mock(spec=(), side_effect=(Exception, ))
    qct = _helper_threads.QueueCallbackWorker(queue_, callback)

    # Set up an appropriate mock for the queue, and call the queue callback
    # thread.
    with mock.patch.object(queue.Queue, 'get') as get:
        item1 = ('action', mock.sentinel.A)
        get.side_effect = (item1, _helper_threads.STOP)
        qct()

        # Assert that we got the expected calls.
        assert get.call_count == 2
        callback.assert_called_once_with('action', mock.sentinel.A)
Пример #3
0
def test_queue_callback_worker():
    queue_ = queue.Queue()
    callback = mock.Mock(spec=())
    qct = _helper_threads.QueueCallbackWorker(queue_, callback)

    # Set up an appropriate mock for the queue, and call the queue callback
    # thread.
    with mock.patch.object(queue.Queue, 'get') as get:
        get.side_effect = (
            mock.sentinel.A,
            _helper_threads.STOP,
            queue.Empty())
        qct()

        # Assert that we got the expected calls.
        assert get.call_count == 3
        callback.assert_called_once_with([mock.sentinel.A])