예제 #1
0
    async def test_should_reset_event_when_event_is_set_and_put_nowait_is_called(
            self):
        queue = Queue()
        queue._finished.set()
        queue.put_nowait(2)

        assert not queue._finished.is_set()
예제 #2
0
    async def test_should_work_when_adding_item_with_put_method(self):
        queue = Queue(size=3)
        await queue.put(2)
        await queue.put('foo')

        assert 2 == queue.length
        assert 2 == queue._tasks_in_progress
예제 #3
0
    async def test_should_raise_error_when_task_done_is_called_without_adding_items_in_queue(
            self):
        async with Queue(size=1) as queue:
            with pytest.raises(ValueError) as exc_info:
                queue.task_done()

            assert 'task_done method was calling too many times without adding items in queue' == str(
                exc_info.value)
예제 #4
0
 async def test_should_work_with_default_initialization(self):
     queue = Queue()
     assert 1 == queue.maxsize
     assert 0 == queue.length
     assert [] == queue._items
     assert 0 == queue._tasks_in_progress
     assert isinstance(queue._send_channel, MemoryObjectSendStream)
     assert isinstance(queue._receive_channel, MemoryObjectReceiveStream)
     assert isinstance(queue._finished, anyio.Event)
예제 #5
0
    async def test_should_work_when_using_context_manager(self):
        async with Queue(size=1) as queue:
            queue.put_nowait(2)

        with pytest.raises(anyio.ClosedResourceError):
            queue.put_nowait(3)

        with pytest.raises(anyio.ClosedResourceError):
            queue.get_nowait()
예제 #6
0
    async def test_should_work_when_calling_close_method(self):
        queue = Queue()
        await queue.close()

        with pytest.raises(anyio.ClosedResourceError):
            await queue.put(2)

        with pytest.raises(anyio.ClosedResourceError):
            await queue.get()
예제 #7
0
    async def test_should_raise_would_block_error_when_buffer_is_full_and_put_nowait_is_used(
            self):
        queue = Queue()
        queue.put_nowait(1)

        with pytest.raises(anyio.WouldBlock):
            queue.put_nowait(2)
예제 #8
0
    async def test_should_follow_unprocessed_url(self, chrome_driver):
        url = 'http://foo.com'
        response = SeleniumResponse(
            driver=chrome_driver,
            handle='4',
            reachable_urls={'http://bar.com'},
            followed_urls=set(),
            queue=Queue(size=1),
        )
        await response.follow(url)

        assert {'http://bar.com'} == response._reachable_urls
        assert {url} == response._followed_urls
        assert 1 == response._queue.length
예제 #9
0
 async def test_should_not_follow_already_processed_url(
         self, mocker, chrome_driver, reachable_urls, followed_urls):
     logger_mock = mocker.patch('logging.Logger.debug')
     url = 'http://foo.com'
     response = SeleniumResponse(
         driver=chrome_driver,
         handle='4',
         reachable_urls=reachable_urls,
         followed_urls=followed_urls,
         queue=Queue(),
     )
     await response.follow(url)
     assert mocker.call(
         'url %s has already been processed, nothing to do here',
         url) in logger_mock.call_args_list
예제 #10
0
    async def test_should_follow_unprocessed_url(self):
        url = 'http://foo.com'
        request = httpx.Request('GET', url)
        httpx_response = httpx.Response(200, request=request)
        queue = Queue(2)
        response = StaticResponse(reachable_urls={'http://bar.com'},
                                  followed_urls=set(),
                                  queue=queue,
                                  httpx_response=httpx_response)
        await response.follow(url)
        queue_length = queue.length
        await queue.close()

        assert {'http://bar.com'} == response._reachable_urls
        assert {url} == response._followed_urls
        assert 1 == queue_length
예제 #11
0
    async def test_should_work_when_using_task_done_and_join_methods(self):
        async with Queue(size=1) as queue:
            await queue.put(2)
            await queue.get()
            assert 1 == queue._tasks_in_progress

            with anyio.move_on_after(1) as cancel_scope:
                await queue.join()

            assert cancel_scope.cancel_called
            queue.task_done()
            assert 0 == queue._tasks_in_progress

            with anyio.move_on_after(1) as cancel_scope:
                await queue.join()

            assert not cancel_scope.cancel_called
예제 #12
0
    async def test_should_not_follow_already_processed_url(
            self, mocker, reachable_urls, followed_urls):
        logger_mock = mocker.patch('logging.Logger.debug')
        url = 'http://foo.com'
        request = httpx.Request('GET', url)
        httpx_response = httpx.Response(200, request=request)
        queue = Queue()
        response = StaticResponse(reachable_urls=reachable_urls,
                                  followed_urls=followed_urls,
                                  queue=queue,
                                  httpx_response=httpx_response)

        await response.follow(url)
        await queue.close()

        assert mocker.call(
            'url %s has already been processed, nothing to do here',
            url) in logger_mock.call_args_list
예제 #13
0
    async def test_should_work_when_using_get_nowait_method(self):
        queue = Queue(3, items=[2, 'foo'])

        assert 2 == queue.get_nowait()
        assert 'foo' == queue.get_nowait()
예제 #14
0
 async def test_should_raise_error_when_size_is_less_than_items_length(
         self):
     with pytest.raises(anyio.WouldBlock):
         Queue(size=1, items=[2, 'foo'])
예제 #15
0
    async def test_should_raise_would_block_error_when_using_get_nowait_on_empty_buffer(
            self):
        queue = Queue()

        with pytest.raises(anyio.WouldBlock):
            queue.get_nowait()
예제 #16
0
    async def test_should_raise_error_when_size_is_negative(self):
        with pytest.raises(ValueError) as exc_info:
            Queue(size=-1)

        assert 'size must not be less than 1 but you provide: -1' == str(
            exc_info.value)
예제 #17
0
 async def test_should_work_when_initializing_with_size_and_items(self):
     items = [2, 'foo']
     queue = Queue(size=3, items=items)
     assert 3 == queue.maxsize
     assert 2 == queue.length
     assert items == queue._items
예제 #18
0
 async def test_length_property_works_with_infinity_size(self):
     queue = Queue(size=math.inf)
     assert 0 == queue.length
     assert queue.maxsize is math.inf
예제 #19
0
 async def test_should_work_when_initializing_items(self):
     items = [2, 'foo']
     queue = Queue(3, items=items)
     assert 3 == queue.maxsize
     assert items == queue._items
     assert len(items) == queue.length == queue._tasks_in_progress
예제 #20
0
 async def test_should_work_when_initializing_buffer_size(self):
     queue = Queue(size=3)
     assert 3 == queue.maxsize
     assert 0 == queue.length