コード例 #1
0
ファイル: queue_test.py プロジェクト: BroetchenEK/psq
    def test_creation(self):
        # Test the case where queue needs to create the topic.
        pubsub = Mock()
        topic = Mock()
        topic.exists.return_value = False
        pubsub.topic.return_value = topic

        q = Queue(pubsub)

        assert pubsub.topic.called
        assert topic.exists.called
        assert q.topic == topic

        sub = Mock()
        sub.exists.return_value = False

        # Test the case where it needs to create the subcription.
        with patch('gcloud.pubsub.Subscription') as SubscriptionMock:
            SubscriptionMock.return_value = sub
            rsub = q._get_or_create_subscription()

            assert rsub == sub
            assert SubscriptionMock.called_with('psq-default-shared', topic)
            assert sub.exists.called
            assert sub.create.called

        # Test case where subscription exists and it should re-use it.
        with patch('gcloud.pubsub.Subscription') as SubscriptionMock:
            sub.reset_mock()
            SubscriptionMock.return_value = sub
            sub.exists.return_value = True
            rsub = q._get_or_create_subscription()

            assert rsub == sub
            assert not sub.create.called
コード例 #2
0
ファイル: queue_test.py プロジェクト: quanpands/WEB_GCP-psq
def test_constructor_creates_topic():
    publisher_client = make_publisher_client()
    subscriber_client = make_subscriber_client()

    publisher_client.get_topic.side_effect = (google.cloud.exceptions.NotFound(
        None, None))

    q = Queue(publisher_client, subscriber_client, 'test-project')

    publisher_client.create_topic.assert_called_once_with(q._get_topic_path())
コード例 #3
0
ファイル: queue_test.py プロジェクト: quanpands/WEB_GCP-psq
def test__pubsub_message_callback_bad_value():
    callback = mock.Mock()
    message = mock.create_autospec(
        google.cloud.pubsub_v1.subscriber.message.Message, instance=True)
    message.data = b'bad'

    Queue._pubsub_message_callback(callback, message)

    assert not callback.called
    assert message.ack.called
コード例 #4
0
ファイル: queue_test.py プロジェクト: BroetchenEK/psq
    def test_queue(self):
        # Test queueing tasks.
        with patch('psq.queue.Queue._get_or_create_topic'):
            q = Queue(Mock())
            q.storage.put_task = Mock()

            r = q.enqueue(sum, 1, 2, arg='c')
            assert q.topic.publish.called
            assert q.storage.put_task.called

            t = q.storage.put_task.call_args[0][0]
            assert t.f == sum
            assert t.args == (1, 2)
            assert t.kwargs == {'arg': 'c'}

            assert r.task_id == t.id
コード例 #5
0
ファイル: queue_test.py プロジェクト: harsha31/psq
def test_queue():
    # Test queueing tasks.
    with patch('psq.queue.Queue._get_or_create_topic'):
        q = Queue(Mock())
        q.storage.put_task = Mock()

        r = q.enqueue(sum, 1, 2, arg='c')
        assert q.topic.publish.called
        assert q.storage.put_task.called

        t = q.storage.put_task.call_args[0][0]
        assert t.f == sum
        assert t.args == (1, 2)
        assert t.kwargs == {'arg': 'c'}

        assert r.task_id == t.id
コード例 #6
0
ファイル: queue_test.py プロジェクト: quanpands/WEB_GCP-psq
def test_constructor_existing_topic():
    publisher_client = make_publisher_client()
    subscriber_client = make_subscriber_client()

    Queue(publisher_client, subscriber_client, 'test-project')

    publisher_client.create_topic.assert_not_called()
コード例 #7
0
ファイル: queue_test.py プロジェクト: harsha31/psq
def test_get_or_create_subscription():
    pubsub = Mock()
    topic = Mock()
    topic.exists.return_value = True
    pubsub.topic.return_value = topic

    q = Queue(pubsub)

    sub = Mock()
    sub.exists.return_value = False

    # Test the case where it needs to create the subcription.
    with patch('google.cloud.pubsub.Subscription') as SubscriptionMock:
        SubscriptionMock.return_value = sub
        rsub = q._get_or_create_subscription()

        assert rsub == sub
        assert SubscriptionMock.called_with('psq-default-shared', topic)
        assert sub.exists.called
        assert sub.create.called

    # Test case where subscription exists and it should re-use it.
    with patch('google.cloud.pubsub.Subscription') as SubscriptionMock:
        sub.reset_mock()
        SubscriptionMock.return_value = sub
        sub.exists.return_value = True
        rsub = q._get_or_create_subscription()

        assert rsub == sub
        assert not sub.create.called

    # Test case where subscription gets created after we check that it
    # doesn't exist.

    with patch('google.cloud.pubsub.Subscription') as SubscriptionMock:
        sub.reset_mock()
        SubscriptionMock.return_value = sub
        sub.exists.return_value = False
        sub.create.side_effect = google.cloud.exceptions.Conflict('')
        rsub = q._get_or_create_subscription()

        assert sub.create.called
        assert rsub == sub
コード例 #8
0
ファイル: queue_test.py プロジェクト: GoogleCloudPlatform/psq
def test_get_or_create_subscription():
    pubsub = Mock()
    topic = Mock()
    topic.exists.return_value = True
    pubsub.topic.return_value = topic

    q = Queue(pubsub)

    sub = Mock()
    sub.exists.return_value = False

    # Test the case where it needs to create the subcription.
    with patch("google.cloud.pubsub.Subscription") as SubscriptionMock:
        SubscriptionMock.return_value = sub
        rsub = q._get_or_create_subscription()

        assert rsub == sub
        assert SubscriptionMock.called_with("psq-default-shared", topic)
        assert sub.exists.called
        assert sub.create.called

    # Test case where subscription exists and it should re-use it.
    with patch("google.cloud.pubsub.Subscription") as SubscriptionMock:
        sub.reset_mock()
        SubscriptionMock.return_value = sub
        sub.exists.return_value = True
        rsub = q._get_or_create_subscription()

        assert rsub == sub
        assert not sub.create.called

    # Test case where subscription gets created after we check that it
    # doesn't exist.

    with patch("google.cloud.pubsub.Subscription") as SubscriptionMock:
        sub.reset_mock()
        SubscriptionMock.return_value = sub
        sub.exists.return_value = False
        sub.create.side_effect = google.cloud.exceptions.Conflict("")
        rsub = q._get_or_create_subscription()

        assert sub.create.called
        assert rsub == sub
コード例 #9
0
ファイル: queue_test.py プロジェクト: BroetchenEK/psq
    def test_dequeue(self):
        # Test dequeueing (fetching) tasks.
        with patch('psq.queue.Queue._get_or_create_topic'):
            q = Queue(Mock())
            t = Task('1', sum, (1, 2), {'arg': 'c'})
            sub_mock = Mock()
            q._get_or_create_subscription = Mock(return_value=sub_mock)

            # No messages
            sub_mock.pull.return_value = []

            tasks = q.dequeue()

            assert sub_mock.pull.called
            assert not tasks

            # One Message
            sub_mock.pull.reset_mock()
            sub_mock.pull.return_value = [
                ('ack_id', MockMessage(dumps(t)))]

            tasks = q.dequeue()

            assert sub_mock.pull.called
            sub_mock.acknowledge.assert_called_once_with(['ack_id'])
            assert tasks[0].id == t.id
            assert tasks[0].f == t.f
            assert tasks[0].args == t.args
            assert tasks[0].kwargs == t.kwargs

            # Bad message
            sub_mock.pull.reset_mock()
            sub_mock.acknowledge.reset_mock()
            sub_mock.pull.return_value = [
                ('ack_id', MockMessage('this is a bad pickle string'))]

            tasks = q.dequeue()

            assert not tasks
            assert sub_mock.pull.called
            sub_mock.acknowledge.assert_called_once_with(['ack_id'])
コード例 #10
0
ファイル: queue_test.py プロジェクト: harsha31/psq
def test_context():
    # Test queue-local context.
    pubsub = Mock()
    pubsub.topic.return_value = Mock()
    q = Queue(pubsub)

    with q.queue_context():
        assert current_queue == q

    # Test additional context manager.
    spy = Mock()

    @contextmanager
    def extra_context():
        spy()
        yield

    q.extra_context = extra_context

    with q.queue_context():
        assert spy.called
コード例 #11
0
ファイル: queue_test.py プロジェクト: BroetchenEK/psq
    def test_context(self):
        # Test queue-local context.
        pubsub = Mock()
        pubsub.topic.return_value = Mock()
        q = Queue(pubsub)

        with q.queue_context():
            assert current_queue == q

        # Test additional context manager.
        spy = Mock()

        @contextmanager
        def extra_context():
            spy()
            yield

        q.extra_context = extra_context

        with q.queue_context():
            assert spy.called
コード例 #12
0
ファイル: queue_test.py プロジェクト: harsha31/psq
def test_creation_existing_topic():
    # Test the case where queue needs to create the topic.
    pubsub = Mock()
    topic = Mock()
    topic.exists.return_value = False
    topic.create.side_effect = google.cloud.exceptions.Conflict('')
    pubsub.topic.return_value = topic

    q = Queue(pubsub)

    assert pubsub.topic.called
    assert topic.create.called
    assert q.topic == topic
コード例 #13
0
ファイル: queue_test.py プロジェクト: GoogleCloudPlatform/psq
def test_dequeue():
    # Test dequeueing (fetching) tasks.
    with patch("psq.queue.Queue._get_or_create_topic"):
        q = Queue(Mock())
        t = Task("1", sum, (1, 2), {"arg": "c"})
        sub_mock = Mock()
        q._get_or_create_subscription = Mock(return_value=sub_mock)

        # No messages
        sub_mock.pull.return_value = []

        tasks = q.dequeue()

        assert sub_mock.pull.called
        assert not tasks

        # One Message
        sub_mock.pull.reset_mock()
        sub_mock.pull.return_value = [("ack_id", MockMessage(dumps(t)))]

        tasks = q.dequeue()

        assert sub_mock.pull.called
        sub_mock.acknowledge.assert_called_once_with(["ack_id"])
        assert tasks[0].id == t.id
        assert tasks[0].f == t.f
        assert tasks[0].args == t.args
        assert tasks[0].kwargs == t.kwargs

        # Bad message
        sub_mock.pull.reset_mock()
        sub_mock.acknowledge.reset_mock()
        sub_mock.pull.return_value = [("ack_id", MockMessage("this is a bad pickle string"))]

        tasks = q.dequeue()

        assert not tasks
        assert sub_mock.pull.called
        sub_mock.acknowledge.assert_called_once_with(["ack_id"])
コード例 #14
0
ファイル: queue_test.py プロジェクト: harsha31/psq
def test_creation():
    # Test the case where queue needs to create the topic.
    pubsub = Mock()
    topic = Mock()
    topic.exists.return_value = False
    pubsub.topic.return_value = topic

    q = Queue(pubsub)

    assert pubsub.topic.called
    assert topic.create.called
    assert q.topic == topic

    sub = Mock()
    sub.exists.return_value = False
コード例 #15
0
ファイル: queue_test.py プロジェクト: harsha31/psq
def test_dequeue():
    # Test dequeueing (fetching) tasks.
    with patch('psq.queue.Queue._get_or_create_topic'):
        q = Queue(Mock())
        t = Task('1', sum, (1, 2), {'arg': 'c'})
        sub_mock = Mock()
        q._get_or_create_subscription = Mock(return_value=sub_mock)

        # No messages
        sub_mock.pull.return_value = []

        tasks = q.dequeue()

        assert sub_mock.pull.called
        assert not tasks

        # One Message
        sub_mock.pull.reset_mock()
        sub_mock.pull.return_value = [('ack_id', MockMessage(dumps(t)))]

        tasks = q.dequeue()

        assert sub_mock.pull.called
        sub_mock.acknowledge.assert_called_once_with(['ack_id'])
        assert tasks[0].id == t.id
        assert tasks[0].f == t.f
        assert tasks[0].args == t.args
        assert tasks[0].kwargs == t.kwargs

        # Bad message
        sub_mock.pull.reset_mock()
        sub_mock.acknowledge.reset_mock()
        sub_mock.pull.return_value = [
            ('ack_id', MockMessage('this is a bad pickle string'))
        ]

        tasks = q.dequeue()

        assert not tasks
        assert sub_mock.pull.called
        sub_mock.acknowledge.assert_called_once_with(['ack_id'])
コード例 #16
0
def test_string_function():
    q = Queue(pubsub=None, storage=TestStorage(), async=False)
    r = q.enqueue('psq.queue_test.dummy_queue_func')
    assert r.result() == "Hello"
コード例 #17
0
ファイル: queue_test.py プロジェクト: harsha31/psq
def test_synchronous_fail():
    q = Queue(pubsub=None, storage=TestStorage(), async=False)
    r = q.enqueue(sum, "2")
    with pytest.raises(TypeError):
        r.result()
コード例 #18
0
ファイル: queue_test.py プロジェクト: harsha31/psq
def test_synchronous_success():
    q = Queue(pubsub=None, storage=TestStorage(), async=False)
    r = q.enqueue(sum, [1, 2])
    assert r.result() == 3
コード例 #19
0
ファイル: queue_test.py プロジェクト: harsha31/psq
def test_cleanup():
    pubsub = Mock()
    pubsub.topic.return_value = Mock()
    q = Queue(pubsub)

    q.cleanup()
コード例 #20
0
ファイル: queue_test.py プロジェクト: quanpands/WEB_GCP-psq
def make_queue(**kwargs):
    publisher_client = make_publisher_client()
    subscriber_client = make_subscriber_client()
    return Queue(publisher_client, subscriber_client, 'test-project', **kwargs)
コード例 #21
0
ファイル: queue_test.py プロジェクト: GoogleCloudPlatform/psq
def test_synchronous_success():
    q = Queue(pubsub=None, storage=TestStorage(), async=False)
    r = q.enqueue(sum, [1, 2])
    assert r.result() == 3
コード例 #22
0
ファイル: queue_test.py プロジェクト: BroetchenEK/psq
    def test_cleanup(self):
        pubsub = Mock()
        pubsub.topic.return_value = Mock()
        q = Queue(pubsub)

        q.cleanup()
コード例 #23
0
ファイル: queue_test.py プロジェクト: GoogleCloudPlatform/psq
def test_synchronous_fail():
    q = Queue(pubsub=None, storage=TestStorage(), async=False)
    r = q.enqueue(sum, "2")
    with pytest.raises(TypeError):
        r.result()