def test_retry_raises_at_last_try(self, logger, mock_sleep): def func(): self.call_count += 1 raise FlashError(message="", return_code=1) with self.assertRaises(FlashError) as cm: retry(logger=logger, func=func, func_args=(), conditions=[1], retries=5) self.assertEqual(cm.exception.return_code, 1) self.assertEqual(self.call_count, 5)
def test_retry_has_no_condition_by_default(self, logger, mock_sleep): def func(): self.call_count += 1 raise FlashError(message="", return_code=self.call_count) with self.assertRaises(FlashError) as cm: retry(logger=logger, func=func, func_args=()) self.assertEqual(cm.exception.return_code, 1) self.assertEqual(self.call_count, 1)
def test_retry_breaks_when_condition_doesnt_match(self, logger, mock_sleep): def func(): self.call_count += 1 raise FlashError(message="", return_code=self.call_count) with self.assertRaises(FlashError) as cm: retry(logger=logger, func=func, func_args=(), conditions=[1]) self.assertEqual(cm.exception.return_code, 2) self.assertEqual(self.call_count, 2)
def test_retry_returns_last_return_code(self, logger, mock_sleep): def func(): self.call_count += 1 raise FlashError(message="", return_code=self.call_count) with self.assertRaises(FlashError) as cm: retry(logger=logger, func=func, func_args=(), conditions=[1, 2]) self.assertEqual(cm.exception.return_code, 3) self.assertEqual(self.call_count, DEFAULT_RETRY_AMOUNT)
def test_retries_on_condition(self, logger): def func(): self.call_count += 1 raise FlashError(message="", return_code=1) with self.assertRaises(FlashError) as cm: retry(logger=logger, func=func, func_args=(), conditions=[1]) self.assertEqual(cm.exception.return_code, 1) self.assertEqual(self.call_count, DEFAULT_RETRY_AMOUNT)
def test_sleep_is_called_on_retries(self, logger, mock_sleep): def func(): self.call_count += 1 raise FlashError(message="", return_code=1) with self.assertRaises(FlashError) as cm: retry(logger=logger, func=func, func_args=(), conditions=[1], retries=5) self.assertEqual(cm.exception.return_code, 1) self.assertEqual(self.call_count, 5) # Check that sleep is called with expected time in correct order self.assertEqual(mock_sleep.call_args_list[0][0][0], 1 ** 2) self.assertEqual(mock_sleep.call_args_list[1][0][0], 2 ** 2) self.assertEqual(mock_sleep.call_args_list[2][0][0], 3 ** 2) self.assertEqual(mock_sleep.call_args_list[3][0][0], 4 ** 2)
def flash(self, source, target, method, no_reset): """copy file to the destination :param source: binary to be flashed :param target: target to be flashed :param method: method to use when flashing :param no_reset: do not reset flashed board at all """ if not isinstance(source, six.string_types): return if method == 'pyocd': self.logger.debug("pyOCD selected for flashing") return self.try_pyocd_flash(source, target) if method == 'edbg': raise FlashError(message="edbg is not supported for Mbed devices", return_code=EXIT_CODE_EGDB_NOT_SUPPORTED) return retry( logger=self.logger, func=self.try_drag_and_drop_flash, func_args=(source, target, no_reset), retries=FlasherMbed.DRAG_AND_DROP_FLASH_RETRIES, conditions=[EXIT_CODE_OS_ERROR, EXIT_CODE_DAPLINK_TRANSIENT_ERROR, EXIT_CODE_DAPLINK_SOFTWARE_ERROR])
def test_retry_succeeds_first_try(self, logger, mock_sleep): def func(): self.call_count += 1 return 0 return_code = retry(logger=logger, func=func, func_args=(), conditions=[1]) self.assertEqual(return_code, 0) self.assertEqual(self.call_count, 1)