'Received more reports of parsers timing out, and after that
w3af showing always "The parser took more than X seconds to complete
parsing of" for all calls to the parser.
Want to test how well the the parser recovers from many timeouts.'

def test_many_parsers_timing_out(self):

    mmpdp = 'w3af.core.data.parsers.mp_document_parser.%s'
    kmpdp = (mmpdp % 'MultiProcessingDocumentParser.%s')
    modp = 'w3af.core.data.parsers.document_parser.%s'
    with patch((mmpdp % 'om.out')) as om_mock:
        with patch((kmpdp % 'PARSER_TIMEOUT'), new_callable=PropertyMock) as timeout_mock:
            with patch((kmpdp % 'MAX_WORKERS'), new_callable=PropertyMock) as max_workers_mock:
                with patch((modp % 'DocumentParser.PARSERS'), new_callable=PropertyMock) as parsers_mock:
                    html_trigger_delay = '<html>DelayedParser!</html>%s'
                    html_ok = '<html>foo-</html>%s'
                    timeout_mock.return_value = 1
                    max_workers_mock.return_value = 5
                    parsers_mock.return_value = [DelayedParser, HTMLParser]
                    ITERATIONS = 25
                    for i in xrange(ITERATIONS):
                        http_resp = _build_http_response((html_trigger_delay % i), u'text/html')
                        try:
                            self.mpdoc.get_document_parser_for(http_resp)
                        except BaseFrameworkException:
                            self._is_timeout_exception_message(om_mock, http_resp)
                        else:
                            self.assertTrue(False)
                    for i in xrange(ITERATIONS):
                        html = random.choice([html_trigger_delay, html_ok])
                        http_resp = _build_http_response((html % i), u'text/html')
                        try:
                            parser = self.mpdoc.get_document_parser_for(http_resp)
                        except BaseFrameworkException:
                            self._is_timeout_exception_message(om_mock, http_resp)
                        else:
                            self.assertIsInstance(parser._parser, HTMLParser)
                    for i in xrange(ITERATIONS):
                        http_resp = _build_http_response((html_ok % i), u'text/html')
                        parser = self.mpdoc.get_document_parser_for(http_resp)
                        self.assertIsInstance(parser._parser, HTMLParser)
