예제 #1
0
    def test_spawn_on_push(self):
        handler = QueueHandler('name', self.proxies, self.registry)
        request = mock.Mock()
        handler.push(request)

        sem = self.semaphore
        assert sem.__enter__.call_count == sem.__exit__.call_count
        assert self.concurrency_utils.spawn.call_count == 1
예제 #2
0
    def test_forwarding_raise_exception(self):
        """
        Test catching exception raised by forward method.
        """
        req1 = mock.Mock()
        req1.path_info = 'path1'
        req2 = mock.Mock()
        req2.path_info = 'path2'
        resp = mock.Mock()
        resp.status_code = 200
        req1.get_response.side_effect = [KeyError(), resp]
        req2.get_response.return_value = resp
        self.queue[:] = [req1, req2]
        proxies = {
            'path1': '',
        }
        QueueHandler('name', proxies, self.registry)

        worker = self.concurrency_utils.spawn.mock_calls[0][1][0]
        worker()
        assert self.queue
        assert req1.get_response.mock_calls[0][1] == ('',)
        assert req1.get_response.call_count == 1
        assert len(req2.get_response.mock_calls) == 0
        assert self.concurrency_utils.sleep.mock_calls == []
예제 #3
0
    def test_forwarding_two_requests_with_fail(self):
        """
        Test forwarding requests until any response will came from
        remote (in this case 200).
        """
        req1 = mock.Mock()
        req1.path_info = 'path1'
        req2 = mock.Mock()
        req2.path_info = 'path2'
        resp = mock.Mock()
        resp.status_code = 200
        req1.get_response.side_effect = [IOError(), resp]
        req2.get_response.return_value = resp
        self.queue[:] = [req1, req2]
        QueueHandler('name', self.proxies, self.registry)

        worker = self.concurrency_utils.spawn.mock_calls[0][1][0]
        worker()
        assert not self.queue
        assert req1.get_response.mock_calls[0][1] == (self.path1_proxy,)
        assert req1.get_response.mock_calls[1][1] == (self.path1_proxy,)
        assert req1.get_response.call_count == 2
        assert req2.get_response.mock_calls[0][1] == (self.path2_proxy,)
        assert req2.get_response.call_count == 1
        assert self.concurrency_utils.sleep.mock_calls[0][1] == (1,)
        assert self.concurrency_utils.sleep.mock_calls[1][1] == (2,)
        assert self.concurrency_utils.sleep.call_count == 2
예제 #4
0
    def test_spawn_form_constructor(self):
        self.queue[:] = [object()]
        QueueHandler('name', self.proxies, self.registry)

        sem = self.semaphore
        assert sem.__enter__.call_count == sem.__exit__.call_count
        assert self.concurrency_utils.spawn.call_count == 1
예제 #5
0
    def test_forwarding(self):
        req = mock.Mock()
        req.path_info = 'path1'
        resp = mock.Mock()
        resp.status_code = 200
        req.get_response.return_value = resp
        self.queue[:] = [req]
        QueueHandler('name', self.proxies, self.registry)

        worker = self.concurrency_utils.spawn.mock_calls[0][1][0]
        worker()
        assert not self.queue
        assert req.get_response.mock_calls[0][1] == (self.path1_proxy,)
        assert self.concurrency_utils.sleep.call_count == 0
예제 #6
0
    def test_max_backoff(self):
        req1 = mock.Mock()
        req1.path_info = 'path1'
        resp = mock.Mock()
        resp.status_code = 200
        req1.get_response.side_effect = [
            IOError(), IOError(),
            IOError(), IOError(), resp
        ]
        self.queue[:] = [req1]
        QueueHandler('name', self.proxies, self.registry)

        worker = self.concurrency_utils.spawn.mock_calls[0][1][0]
        worker()
        assert not self.queue
        assert self.concurrency_utils.sleep.mock_calls[0][1] == (1, )
        assert self.concurrency_utils.sleep.mock_calls[1][1] == (2, )
        assert self.concurrency_utils.sleep.mock_calls[2][1] == (4, )
        assert self.concurrency_utils.sleep.mock_calls[3][1] == (4, )
        assert self.concurrency_utils.sleep.call_count == 4
예제 #7
0
    def test_forwarding_until_400(self):
        """
        Test forwarding requests until any response will came from
        remote (in this case 400).
        """
        req = mock.Mock()
        req.path_info = 'path1'
        resp1 = mock.Mock()
        resp1.status_code = 400
        req.get_response.side_effect = [IOError(), IOError(), resp1]
        self.queue[:] = [req]
        QueueHandler('name', self.proxies, self.registry)

        worker = self.concurrency_utils.spawn.mock_calls[0][1][0]
        worker()
        assert not self.queue
        assert req.get_response.mock_calls[0][1] == (self.path1_proxy,)
        assert req.get_response.mock_calls[1][1] == (self.path1_proxy,)
        assert req.get_response.mock_calls[2][1] == (self.path1_proxy,)
        assert req.get_response.call_count == 3
        assert self.concurrency_utils.sleep.mock_calls[0][1] == (1,)
        assert self.concurrency_utils.sleep.mock_calls[1][1] == (2,)
        assert self.concurrency_utils.sleep.call_count == 2
예제 #8
0
    def test_forwarding_fail(self):
        req = mock.Mock()
        req.path_info = 'path1'
        resp1 = mock.Mock()
        resp1.status_code = 400
        resp2 = mock.Mock()
        resp2.status_code = 200
        req.get_response.side_effect = [resp1, IOError(), resp1, resp2]
        self.queue[:] = [req]
        QueueHandler('name', self.proxies, self.registry)

        worker = self.concurrency_utils.spawn.mock_calls[0][1][0]
        worker()
        assert not self.queue
        assert req.get_response.mock_calls[0][1] == (self.path1_proxy, )
        assert req.get_response.mock_calls[1][1] == (self.path1_proxy, )
        assert req.get_response.mock_calls[2][1] == (self.path1_proxy, )
        assert req.get_response.mock_calls[3][1] == (self.path1_proxy, )
        assert req.get_response.call_count == 4
        assert self.concurrency_utils.sleep.mock_calls[0][1] == (1, )
        assert self.concurrency_utils.sleep.mock_calls[1][1] == (2, )
        assert self.concurrency_utils.sleep.mock_calls[2][1] == (4, )
        assert self.concurrency_utils.sleep.call_count == 3
예제 #9
0
    def test_push(self):
        handler = QueueHandler('name', self.proxies, self.registry)
        request = mock.Mock()
        handler.push(request)

        assert self.queue[0] == request