Esempio n. 1
0
 def _request(self, method, path, **kwargs):
     """Make a request to the Git hosting API."""
     # Fetch the current timeout before starting the timeline action,
     # since making a database query inside this action will result in an
     # OverlappingActionError.
     get_default_timeout_function()()
     timeline = get_request_timeline(get_current_browser_request())
     action = timeline.start("git-hosting-%s" % method,
                             "%s %s" % (path, json.dumps(kwargs)))
     try:
         response = urlfetch(urljoin(self.endpoint, path),
                             method=method,
                             **kwargs)
     except TimeoutError:
         # Re-raise this directly so that it can be handled specially by
         # callers.
         raise
     except requests.RequestException:
         raise
     except Exception:
         _, val, tb = sys.exc_info()
         reraise(RequestExceptionWrapper,
                 RequestExceptionWrapper(*val.args), tb)
     finally:
         action.finish()
     if response.content:
         return response.json()
     else:
         return None
Esempio n. 2
0
 def test_reduced_timeout(self):
     """reduced_timeout caps the available timeout in various ways."""
     self.addCleanup(set_default_timeout_function, None)
     with reduced_timeout(1.0):
         self.assertIsNone(get_default_timeout_function()())
     with reduced_timeout(1.0, default=5.0):
         self.assertEqual(5.0, get_default_timeout_function()())
     set_default_timeout_function(lambda: 5.0)
     with reduced_timeout(1.0):
         self.assertEqual(4.0, get_default_timeout_function()())
     with reduced_timeout(1.0, default=5.0, webapp_max=2.0):
         self.assertEqual(4.0, get_default_timeout_function()())
     with reduced_timeout(1.0, default=5.0, webapp_max=6.0):
         self.assertEqual(4.0, get_default_timeout_function()())
     with reduced_timeout(6.0, default=5.0, webapp_max=2.0):
         self.assertEqual(5.0, get_default_timeout_function()())
     LaunchpadTestRequest()
     set_request_started()
     try:
         with reduced_timeout(1.0):
             self.assertEqual(4.0, get_default_timeout_function()())
         with reduced_timeout(1.0, default=5.0, webapp_max=2.0):
             self.assertEqual(2.0, get_default_timeout_function()())
         with reduced_timeout(1.0, default=5.0, webapp_max=6.0):
             self.assertEqual(4.0, get_default_timeout_function()())
         with reduced_timeout(6.0, default=5.0, webapp_max=2.0):
             self.assertEqual(2.0, get_default_timeout_function()())
     finally:
         clear_request_started()
Esempio n. 3
0
 def test_override_timeout(self):
     """override_timeout temporarily overrides the default timeout."""
     self.addCleanup(set_default_timeout_function, None)
     with override_timeout(1.0):
         self.assertEqual(1.0, get_default_timeout_function()())
     set_default_timeout_function(lambda: 5.0)
     with override_timeout(1.0):
         self.assertEqual(1.0, get_default_timeout_function()())
Esempio n. 4
0
 def test_default_timeout(self):
     """default_timeout sets the default timeout if none is set."""
     self.addCleanup(set_default_timeout_function, None)
     with default_timeout(1.0):
         self.assertEqual(1.0, get_default_timeout_function()())
     set_default_timeout_function(lambda: 5.0)
     with default_timeout(1.0):
         self.assertEqual(5.0, get_default_timeout_function()())
Esempio n. 5
0
 def setUp(self):
     super(HTTPWalker_ListDir, self).setUp()
     self.addCleanup(reset_logging)
     original_timeout_function = get_default_timeout_function()
     set_default_timeout_function(lambda: 60.0)
     self.addCleanup(
         set_default_timeout_function, original_timeout_function)
Esempio n. 6
0
 def runJob(self, job, fallback):
     original_timeout_function = get_default_timeout_function()
     if job.lease_expires is not None:
         set_default_timeout_function(lambda: job.getTimeout())
     try:
         super(BaseJobRunner, self).runJob(IRunnableJob(job), fallback)
     finally:
         set_default_timeout_function(original_timeout_function)
Esempio n. 7
0
 def _request(self,
              method,
              branch_id,
              quoted_tail,
              as_json=False,
              **kwargs):
     """Make a request to the Loggerhead API."""
     # Fetch the current timeout before starting the timeline action,
     # since making a database query inside this action will result in an
     # OverlappingActionError.
     get_default_timeout_function()()
     timeline = get_request_timeline(get_current_browser_request())
     components = [BRANCH_ID_ALIAS_PREFIX, str(branch_id)]
     if as_json:
         components.append("+json")
     components.append(quoted_tail)
     path = "/" + "/".join(components)
     action = timeline.start("branch-hosting-%s" % method,
                             "%s %s" % (path, json.dumps(kwargs)))
     try:
         response = urlfetch(urljoin(self.endpoint, path),
                             method=method,
                             **kwargs)
     except TimeoutError:
         # Re-raise this directly so that it can be handled specially by
         # callers.
         raise
     except requests.RequestException:
         raise
     except Exception:
         _, val, tb = sys.exc_info()
         reraise(RequestExceptionWrapper,
                 RequestExceptionWrapper(*val.args), tb)
     finally:
         action.finish()
     if as_json:
         if response.content:
             return response.json()
         else:
             return None
     else:
         return response.content
Esempio n. 8
0
    def test_timeout_uses_default(self):
        """If the timeout parameter isn't provided, it will default to the value
        returned by the function installed as "default_timeout_function". A
        function is used because it's useful for the timeout value to be
        determined dynamically. For example, if you want to limit the
        overall processing to 30s and you already did 14s, you want that timeout
        to be 16s.

        By default, there is no default_timeout_function.
        """
        self.assertIs(None, get_default_timeout_function())
 def mockRequests(self, method, set_default_timeout=True, **kwargs):
     with responses.RequestsMock() as requests_mock:
         requests_mock.add(method, re.compile(r".*"), **kwargs)
         original_timeout_function = get_default_timeout_function()
         if set_default_timeout:
             set_default_timeout_function(lambda: 60.0)
         try:
             yield
         finally:
             set_default_timeout_function(original_timeout_function)
         self.requests = [call.request for call in requests_mock.calls]
Esempio n. 10
0
    def test_timeout_uses_default(self):
        """If the timeout parameter isn't provided, it will default to the value
        returned by the function installed as "default_timeout_function". A
        function is used because it's useful for the timeout value to be
        determined dynamically. For example, if you want to limit the
        overall processing to 30s and you already did 14s, you want that timeout
        to be 16s.

        By default, there is no default_timeout_function.
        """
        self.assertIs(None, get_default_timeout_function())
Esempio n. 11
0
 def text(self):
     if self.diff_text is None:
         return ''
     else:
         with reduced_timeout(
                 0.01, webapp_max=2.0,
                 default=LIBRARIAN_SERVER_DEFAULT_TIMEOUT):
             timeout = get_default_timeout_function()()
         self.diff_text.open(timeout)
         try:
             return self.diff_text.read(config.diff.max_read_size)
         finally:
             self.diff_text.close()
 def test_retrieveKey_raises_GPGKeyTemporarilyNotFoundError_for_timeout(
         self):
     # If the keyserver responds too slowly, GPGHandler.retrieveKey()
     # raises GPGKeyTemporarilyNotFoundError.
     self.useFixture(KeyServerTac())
     old_timeout_function = get_default_timeout_function()
     set_default_timeout_function(lambda: 0.01)
     try:
         gpghandler = getUtility(IGPGHandler)
         self.assertRaises(GPGKeyTemporarilyNotFoundError,
                           gpghandler.retrieveKey, 'non-existent-fp')
         # An OOPS report is generated for the timeout.
         error_report = self.oopses[-1]
         self.assertEqual('TimeoutError', error_report['type'])
         self.assertEqual('timeout exceeded.', error_report['value'])
     finally:
         set_default_timeout_function(old_timeout_function)
 def test_retrieveKey_raises_GPGKeyTemporarilyNotFoundError_for_timeout(
     self):
     # If the keyserver responds too slowly, GPGHandler.retrieveKey()
     # raises GPGKeyTemporarilyNotFoundError.
     self.useFixture(KeyServerTac())
     old_timeout_function = get_default_timeout_function()
     set_default_timeout_function(lambda: 0.01)
     try:
         gpghandler = getUtility(IGPGHandler)
         self.assertRaises(
             GPGKeyTemporarilyNotFoundError, gpghandler.retrieveKey,
             'non-existent-fp')
         # An OOPS report is generated for the timeout.
         error_report = self.oopses[-1]
         self.assertEqual('TimeoutError', error_report['type'])
         self.assertEqual('timeout exceeded.', error_report['value'])
     finally:
         set_default_timeout_function(old_timeout_function)
Esempio n. 14
0
    def test_runJob_sets_default_timeout_function(self):
        """runJob sets a default timeout function for urlfetch."""
        class RecordDefaultTimeoutJob(NullJob):
            def __init__(self):
                super(RecordDefaultTimeoutJob, self).__init__("")

            def run(self):
                self.default_timeout = get_default_timeout_function()()

        original_timeout_function = get_default_timeout_function()
        set_default_timeout_function(None)
        try:
            job = RecordDefaultTimeoutJob()
            job.job.acquireLease()
            JobRunner([job]).runJob(job, None)
            self.assertEqual(JobStatus.COMPLETED, job.job.status)
            self.assertThat(job.default_timeout, GreaterThan(0))
        finally:
            set_default_timeout_function(original_timeout_function)
Esempio n. 15
0
    def test_timeout(self):
        # The time permitted to get the diff from the librarian may be None,
        # or 2. If there is not 2 seconds left in the request, the number will
        # be 0.01 smaller or the actual remaining time.
        class DiffWithFakeText(Diff):
            diff_text = FakeMethod()

        diff = DiffWithFakeText()
        diff.diff_text.open = FakeMethod()
        diff.diff_text.read = FakeMethod()
        diff.diff_text.close = FakeMethod()
        value = None
        original_timeout_function = get_default_timeout_function()
        set_default_timeout_function(lambda: value)
        try:
            LaunchpadTestRequest()
            set_request_started()
            try:
                diff.text
                self.assertEqual(
                    LIBRARIAN_SERVER_DEFAULT_TIMEOUT,
                    diff.diff_text.open.calls[-1][0][0])
                value = 3.1
                diff.text
                self.assertEqual(2.0, diff.diff_text.open.calls[-1][0][0])
                value = 1.11
                diff.text
                self.assertEqual(1.1, diff.diff_text.open.calls[-1][0][0])
                value = 0.11
                diff.text
                self.assertEqual(0.1, diff.diff_text.open.calls[-1][0][0])
                value = 0.01
                diff.text
                self.assertEqual(0.01, diff.diff_text.open.calls[-1][0][0])
            finally:
                clear_request_started()
        finally:
            set_default_timeout_function(original_timeout_function)
Esempio n. 16
0
 def run(self):
     self.default_timeout = get_default_timeout_function()()