Esempio n. 1
0
    def __init__(self,
                 client,
                 subscription,
                 flow_control=types.FlowControl(),
                 executor=None,
                 queue=None):
        """Instantiate the policy.

        Args:
            client (~.pubsub_v1.subscriber.client): The subscriber client used
                to create this instance.
            subscription (str): The name of the subscription. The canonical
                format for this is
                ``projects/{project}/subscriptions/{subscription}``.
            flow_control (~google.cloud.pubsub_v1.types.FlowControl): The flow
                control settings.
            executor (~concurrent.futures.ThreadPoolExecutor): (Optional.) A
                ThreadPoolExecutor instance, or anything duck-type compatible
                with it.
            queue (~queue.Queue): (Optional.) A Queue instance, appropriate
                for crossing the concurrency boundary implemented by
                ``executor``.
        """
        # Default the callback to a no-op; it is provided by `.open`.
        self._callback = _do_nothing_callback

        # Default the future to None; it is provided by `.open`.
        self._future = None

        # Create a queue for keeping track of shared state.
        if queue is None:
            queue = queue_mod.Queue()
        self._request_queue = queue

        # Call the superclass constructor.
        super(Policy, self).__init__(
            client=client,
            flow_control=flow_control,
            subscription=subscription,
        )

        # Also maintain a request queue and an executor.
        if executor is None:
            executor_kwargs = {}
            if sys.version_info >= (3, 6):
                executor_kwargs['thread_name_prefix'] = (
                    'ThreadPoolExecutor-SubscriberPolicy')
            executor = futures.ThreadPoolExecutor(max_workers=10,
                                                  **executor_kwargs)
        self._executor = executor
        _LOGGER.debug('Creating callback requests thread (not starting).')
        self._callback_requests = _helper_threads.QueueCallbackThread(
            self._request_queue,
            self.on_callback_request,
        )
Esempio n. 2
0
def test_queue_callback_thread():
    queue_ = queue.Queue()
    callback = mock.Mock(spec=())
    qct = _helper_threads.QueueCallbackThread(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)
        qct()

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