Example #1
0
  def test_retry_on_exception_eventual_success(self, mock_sleep):
    broken_func = mock.Mock()
    broken_func.side_effect = [IOError('broken'), IOError('broken'), 'works now']

    retry_on_exception(broken_func, 3, (IOError,))

    self.assertEquals(broken_func.call_count, 3)
Example #2
0
  def test_retry_on_exception_not_caught(self, mock_sleep):
    broken_func = mock.Mock()
    broken_func.side_effect = IOError('broken')

    with self.assertRaises(IOError):
      retry_on_exception(broken_func, 3, (TypeError,))

    self.assertEquals(broken_func.call_count, 1)
Example #3
0
    def test_retry_default_backoff(self, mock_sleep):
        broken_func = mock.Mock()
        broken_func.side_effect = OSError('broken')

        with self.assertRaises(OSError):
            retry_on_exception(broken_func, 4, (OSError, ))

        mock_sleep.assert_has_calls((mock.call(0), mock.call(0), mock.call(0)))
Example #4
0
    def test_retry_on_exception(self, mock_sleep):
        broken_func = unittest.mock.Mock()
        broken_func.side_effect = OSError('broken')

        with self.assertRaises(OSError):
            retry_on_exception(broken_func, 3, (OSError, ))

        self.assertEqual(broken_func.call_count, 3)
Example #5
0
    def test_retry_on_exception_not_caught(self, mock_sleep):
        broken_func = mock.Mock()
        broken_func.side_effect = OSError('broken')

        with self.assertRaises(OSError):
            retry_on_exception(broken_func, 3, (TypeError, ))

        self.assertEquals(broken_func.call_count, 1)
Example #6
0
  def test_retry_on_exception(self, mock_sleep):
    broken_func = mock.Mock()
    broken_func.side_effect = OSError('broken')

    with self.assertRaises(OSError):
      retry_on_exception(broken_func, 3, (OSError,))

    self.assertEquals(broken_func.call_count, 3)
Example #7
0
    def test_retry_on_exception_eventual_success(self, mock_sleep):
        broken_func = mock.Mock()
        broken_func.side_effect = [
            OSError('broken'),
            OSError('broken'), 'works now'
        ]

        retry_on_exception(broken_func, 3, (OSError, ))

        self.assertEquals(broken_func.call_count, 3)
Example #8
0
    def test_retry_on_exception_eventual_success(self, mock_sleep):
        broken_func = unittest.mock.Mock()
        broken_func.side_effect = [
            OSError("broken"),
            OSError("broken"), "works now"
        ]

        retry_on_exception(broken_func, 3, (OSError, ))

        self.assertEqual(broken_func.call_count, 3)
Example #9
0
  def test_retry_default_backoff(self, mock_sleep):
    broken_func = mock.Mock()
    broken_func.side_effect = IOError('broken')

    with self.assertRaises(IOError):
      retry_on_exception(broken_func, 4, (IOError,))

    mock_sleep.assert_has_calls((
      mock.call(0),
      mock.call(0),
      mock.call(0)
    ))
Example #10
0
    def test_retry_on_exception_immediate_success(self):
        working_func = mock.Mock()
        working_func.return_value = 'works'

        self.assertEquals(retry_on_exception(working_func, 3, (OSError, )),
                          'works')
        self.assertEquals(working_func.call_count, 1)
Example #11
0
  def launch(self):
    """Launch and synchronously write metadata.

    This is possible due to watchman's built-in async server startup - no double-forking required.
    """
    cmd = self._construct_cmd((self._watchman_path, 'get-pid'),
                              state_file=self._state_file,
                              sock_file=self._sock_file,
                              log_file=self._log_file,
                              log_level=str(self._log_level))
    self._logger.debug('watchman cmd is: {}'.format(' '.join(cmd)))
    self._maybe_init_metadata()

    # Watchman is launched via its cli. By running the 'get-pid' command on the client we implicitly
    # launch the Watchman daemon. This approach is somewhat error-prone - in some cases the client
    # can successfully trigger the launch of the Watchman daemon, but fail to return successfully
    # for the 'get-pid' result due to server <-> daemon socket issues - these can look like:
    #
    #   2016-04-01T17:31:23,820: [cli] unable to talk to your watchman
    #                                  on .../watchman.sock! (Permission denied)
    #
    # This results in a subprocess execution failure and leaves us with no pid information to write
    # to the metadata directory - while in reality a Watchman daemon is actually running but now
    # untracked. To safeguard against this, we retry the (idempotent) 'get-pid' command a few times
    # to give the server-side socket setup a few chances to quiesce before potentially orphaning it.

    get_output = functools.partial(self.get_subprocess_output, cmd)
    output = retry_on_exception(get_output, 3, (self.ExecutionError,), lambda n: n * .5)

    # Parse the watchman PID from the cli output.
    pid = self._parse_pid_from_output(output)

    # Write the process metadata to disk.
    self.write_pid(pid)
    self.write_socket(self._sock_file)
Example #12
0
  def launch(self):
    """Launch and synchronously write metadata.

    This is possible due to watchman's built-in async server startup - no double-forking required.
    """
    cmd = self._construct_cmd((self._watchman_path, 'get-pid'),
                              state_file=self._state_file,
                              sock_file=self._sock_file,
                              log_file=self._log_file,
                              log_level=str(self._log_level))
    self._logger.debug('watchman cmd is: {}'.format(' '.join(cmd)))
    self._maybe_init_metadata()

    # Watchman is launched via its cli. By running the 'get-pid' command on the client we implicitly
    # launch the Watchman daemon. This approach is somewhat error-prone - in some cases the client
    # can successfully trigger the launch of the Watchman daemon, but fail to return successfully
    # for the 'get-pid' result due to server <-> daemon socket issues - these can look like:
    #
    #   2016-04-01T17:31:23,820: [cli] unable to talk to your watchman
    #                                  on .../watchman.sock! (Permission denied)
    #
    # This results in a subprocess execution failure and leaves us with no pid information to write
    # to the metadata directory - while in reality a Watchman daemon is actually running but now
    # untracked. To safeguard against this, we retry the (idempotent) 'get-pid' command a few times
    # to give the server-side socket setup a few chances to quiesce before potentially orphaning it.

    get_output = functools.partial(self.get_subprocess_output, cmd)
    output = retry_on_exception(get_output, 3, (self.ExecutionError,), lambda n: n * .5)

    # Parse the watchman PID from the cli output.
    pid = self._parse_pid_from_output(output)

    # Write the process metadata to disk.
    self.write_pid(pid)
    self.write_socket(self._sock_file)
Example #13
0
    def test_retry_on_exception_immediate_success(self):
        working_func = unittest.mock.Mock()
        working_func.return_value = "works"

        self.assertEqual(retry_on_exception(working_func, 3, (OSError, )),
                         "works")
        self.assertEqual(working_func.call_count, 1)
Example #14
0
  def test_retry_on_exception_immediate_success(self):
    working_func = mock.Mock()
    working_func.return_value = 'works'

    self.assertEquals(retry_on_exception(working_func, 3, (IOError,)), 'works')
    self.assertEquals(working_func.call_count, 1)