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,
            max_items=self.flow_control.max_request_batch_size,
            max_latency=self.flow_control.max_request_batch_latency)
        # 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
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])
def test_queue_callback_worker_max_items():
    queue_ = queue.Queue()
    callback = mock.Mock(spec=())
    qct = helper_threads.QueueCallbackWorker(queue_, callback, max_items=1)

    # 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, mock.sentinel.B,
                           helper_threads.STOP, queue.Empty())
        qct()

        # Assert that we got the expected calls.
        assert get.call_count == 3
        callback.assert_has_calls(
            [mock.call([(mock.sentinel.A)]),
             mock.call([(mock.sentinel.B)])])
    def start(self):
        """Start a thread to dispatch requests queued up by callbacks.
        Spawns a thread to run :meth:`dispatch_callback`.
        """
        with self._operational_lock:
            if self._thread is not None:
                raise ValueError("Dispatcher is already running.")

            worker = helper_threads.QueueCallbackWorker(
                self._queue,
                self.dispatch_callback,
                max_items=_MAX_BATCH_SIZE,
                max_latency=_MAX_BATCH_LATENCY,
            )
            # Create and start the helper thread.
            thread = threading.Thread(name=_CALLBACK_WORKER_NAME,
                                      target=worker)
            thread.daemon = True
            thread.start()
            _LOGGER.debug("Started helper thread %s", thread.name)
            self._thread = thread
    def start(self):
        """Start a thread to dispatch requests queued up by callbacks.
        Spawns a thread to run :meth:`dispatch_callback`.
        """
        if self._thread is not None:
            raise ValueError('Dispatcher is already running.')

        worker = helper_threads.QueueCallbackWorker(
            self._queue,
            self.dispatch_callback,
            max_items=self._subscriber.flow_control.max_request_batch_size,
            max_latency=self._subscriber.flow_control.max_request_batch_latency
        )
        # Create and start the helper thread.
        thread = threading.Thread(
            name=_CALLBACK_WORKER_NAME,
            target=worker,
        )
        thread.daemon = True
        thread.start()
        _LOGGER.debug('Started helper thread %s', thread.name)
        self._thread = thread