示例#1
0
async def test_modify(test_channel):
    producer = tests.EmptyTestProducer(test_channel)
    await test_channel.register_producer(producer)
    producer_2 = tests.EmptyTestProducer(test_channel)
    await test_channel.register_producer(producer_2)
    with mock.patch.object(producer, 'modify',
                           new=mock.AsyncMock()) as mocked_producer_1_modify:
        with mock.patch.object(
                producer_2, 'modify',
                new=mock.AsyncMock()) as mocked_producer_2_modify:
            await channels.get_chan(tests.EMPTY_TEST_CHANNEL).modify()
            await tests.mock_was_called_once(mocked_producer_1_modify)
            await tests.mock_was_called_once(mocked_producer_2_modify)
示例#2
0
async def test_should_resume_producers_with_priority_consumers(test_channel):
    producer = tests.EmptyTestProducer(test_channel)
    await test_channel.register_producer(producer)
    consumer_1 = await channels.get_chan(
        tests.EMPTY_TEST_CHANNEL
    ).new_consumer(
        tests.empty_test_callback,
        priority_level=async_channel.ChannelConsumerPriorityLevels.HIGH.value)
    consumer_2 = await channels.get_chan(
        tests.EMPTY_TEST_CHANNEL
    ).new_consumer(
        tests.empty_test_callback,
        priority_level=async_channel.ChannelConsumerPriorityLevels.MEDIUM.value
    )
    consumer_3 = await channels.get_chan(
        tests.EMPTY_TEST_CHANNEL
    ).new_consumer(tests.empty_test_callback,
                   priority_level=async_channel.ChannelConsumerPriorityLevels.
                   OPTIONAL.value)
    test_channel.is_paused = True
    if not os.getenv('CYTHON_IGNORE'):
        assert test_channel._should_resume_producers()
    await test_channel.remove_consumer(consumer_1)
    await test_channel.remove_consumer(consumer_2)
    await test_channel.remove_consumer(consumer_3)
示例#3
0
async def test_flush(test_channel):
    producer = tests.EmptyTestProducer(test_channel)
    await test_channel.register_producer(producer)
    producer2 = tests.EmptyTestProducer(test_channel)
    await test_channel.register_producer(producer2)
    producer3 = tests.EmptyTestProducer(test_channel)
    test_channel.internal_producer = producer3

    assert producer3.channel is test_channel
    for producer in test_channel.producers:
        assert producer.channel is test_channel

    test_channel.flush()
    assert test_channel.internal_producer.channel is None
    for producer in test_channel.producers:
        assert producer.channel is None
示例#4
0
async def test_register_producer(test_channel):
    assert channels.get_chan(tests.EMPTY_TEST_CHANNEL).producers == []
    producer = tests.EmptyTestProducer(None)
    await channels.get_chan(tests.EMPTY_TEST_CHANNEL
                            ).register_producer(producer)
    assert channels.get_chan(tests.EMPTY_TEST_CHANNEL).producers == [producer]
    channels.get_chan(tests.EMPTY_TEST_CHANNEL).unregister_producer(producer)
    assert channels.get_chan(tests.EMPTY_TEST_CHANNEL).producers == []
示例#5
0
async def init_consumer_test():
    class TestChannel(channels.Channel):
        PRODUCER_CLASS = tests.EmptyTestProducer
        CONSUMER_CLASS = tests.EmptyTestConsumer

    channels.del_chan(tests.TEST_CHANNEL)
    await util.create_channel_instance(TestChannel, channels.set_chan)
    producer = tests.EmptyTestProducer(channels.get_chan(tests.TEST_CHANNEL))
    await producer.run()
    return await channels.get_chan(tests.TEST_CHANNEL
                                   ).new_consumer(tests.empty_test_callback)
示例#6
0
async def test_producer_is_running():
    class TestChannel(channels.Channel):
        PRODUCER_CLASS = tests.EmptyTestProducer

    channels.del_chan(tests.TEST_CHANNEL)
    await util.create_channel_instance(TestChannel, channels.set_chan)
    producer = tests.EmptyTestProducer(channels.get_chan(tests.TEST_CHANNEL))
    assert not producer.is_running
    await producer.run()
    assert producer.is_running
    await channels.get_chan(tests.TEST_CHANNEL).stop()
    assert not producer.is_running
示例#7
0
async def internal_consumer():
    class TestInternalConsumer(channel_consumer.InternalConsumer):
        async def perform(self, kwargs):
            pass

    class TestChannel(channels.Channel):
        PRODUCER_CLASS = tests.EmptyTestProducer
        CONSUMER_CLASS = TestInternalConsumer

    channels.del_chan(tests.TEST_CHANNEL)
    await util.create_channel_instance(TestChannel, channels.set_chan)
    producer = tests.EmptyTestProducer(channels.get_chan(tests.TEST_CHANNEL))
    await producer.run()
    yield TestInternalConsumer()
    await channels.get_chan(tests.TEST_CHANNEL).stop()
示例#8
0
async def test_resume_producer():
    class TestSupervisedConsumer(channel_consumer.SupervisedConsumer):
        pass

    class TestChannel(channels.Channel):
        PRODUCER_CLASS = tests.EmptyTestProducer
        CONSUMER_CLASS = TestSupervisedConsumer

    channels.del_chan(tests.TEST_CHANNEL)
    await util.create_channel_instance(TestChannel, channels.set_chan)
    producer = tests.EmptyTestProducer(channels.get_chan(tests.TEST_CHANNEL))
    await producer.run()
    await channels.get_chan(tests.TEST_CHANNEL).new_consumer(tests.empty_test_callback)
    await channels.get_chan(tests.TEST_CHANNEL).new_consumer(tests.empty_test_callback)
    await channels.get_chan(tests.TEST_CHANNEL).new_consumer(tests.empty_test_callback)
    await producer.send({"data": "test"})
    await producer.wait_for_processing()
    await channels.get_chan(tests.TEST_CHANNEL).stop()
示例#9
0
async def test_supervised_consumer():
    class TestSupervisedConsumer(channel_consumer.SupervisedConsumer):
        pass

    class TestChannel(channels.Channel):
        PRODUCER_CLASS = tests.EmptyTestProducer
        CONSUMER_CLASS = TestSupervisedConsumer

    channels.del_chan(tests.TEST_CHANNEL)
    await util.create_channel_instance(TestChannel, channels.set_chan)
    producer = tests.EmptyTestProducer(channels.get_chan(tests.TEST_CHANNEL))
    await producer.run()
    consumer = await channels.get_chan(tests.TEST_CHANNEL
                                       ).new_consumer(tests.empty_test_callback
                                                      )
    await channels.get_chan(tests.TEST_CHANNEL
                            ).get_internal_producer().send({})
    await consumer.queue.join()
    await channels.get_chan(tests.TEST_CHANNEL).stop()
示例#10
0
async def test_send_producer_with_consumer():
    class TestConsumer(channel_consumer.Consumer):
        pass

    class TestChannel(channels.Channel):
        PRODUCER_CLASS = tests.EmptyTestProducer
        CONSUMER_CLASS = TestConsumer

    async def callback(data):
        assert data == "test"
        await channels.get_chan(tests.TEST_CHANNEL).stop()

    channels.del_chan(tests.TEST_CHANNEL)
    await util.create_channel_instance(TestChannel, channels.set_chan)
    await channels.get_chan(tests.TEST_CHANNEL).new_consumer(callback)

    producer = tests.EmptyTestProducer(channels.get_chan(tests.TEST_CHANNEL))
    await producer.run()
    await producer.send({"data": "test"})
示例#11
0
async def test_should_resume_producers_when_already_resumed(test_channel):
    producer = tests.EmptyTestProducer(test_channel)
    await test_channel.register_producer(producer)
    test_channel.is_paused = False
    if not os.getenv('CYTHON_IGNORE'):
        assert not test_channel._should_resume_producers()
示例#12
0
async def test_should_resume_producers_with_no_consumers(test_channel):
    producer = tests.EmptyTestProducer(test_channel)
    await test_channel.register_producer(producer)
    test_channel.is_paused = True
    if not os.getenv('CYTHON_IGNORE'):
        assert not test_channel._should_resume_producers()