def test_logs_each_but_last_exception_when_raised(self): qos = WithQoS(self.obj, max_tries=2) self.obj.method.side_effect = [ValueError, ValueError] with mock.patch('logging.exception') as log_mock: with self.assertRaises(ValueError): qos.method() log_mock.assert_called_once_with('caught ValueError; will retry')
def test_method_is_called_once_when_max_tries_is_1_even_if_exception_is_raised( self): qos = WithQoS(self.obj, max_tries=1) self.obj.method.side_effect = ValueError with self.assertRaises(ValueError): qos.method() self.assertEqual(self.obj.method.call_args_list, [mock.call()])
def test_waits_for_wait_time_after_each_but_last_try(self): qos = WithQoS(self.obj, max_tries=2, wait_time=5) self.obj.method.side_effect = [ValueError, ValueError] with mock.patch('time.sleep') as sleep_mock: with self.assertRaises(ValueError): qos.method() sleep_mock.assert_called_once_with(5)
def test_method_is_called_twice_when_max_tries_is_2_and_exceptions_are_raised( self): qos = WithQoS(self.obj, max_tries=2) self.obj.method.side_effect = [ValueError, ValueError] with self.assertRaises(ValueError): qos.method() self.assertEqual(self.obj.method.call_args_list, [mock.call(), mock.call()])
def test_method_is_called_twice_with_correct_arguments_when_exception_on_first_call( self): qos = WithQoS(self.obj) self.obj.method.side_effect = [ValueError, None] qos.method('a', b='b') self.assertEqual(self.obj.method.call_args_list, [mock.call('a', b='b'), mock.call('a', b='b')])
# Config. config = parse_config('config.ini', 'config_local.ini') # Logging. setup_logging(config, os.path.basename(__file__)) logging.info('script started (`' + ' '.join(sys.argv) + '`)') # Quality of Service. qos_enabled = config.getboolean('qos', 'enabled') qos_max_tries = int(config['qos']['max_tries']) qos_wait_time = int(config['qos']['wait_time']) # Database. db = DB(config['db']['conn_url']) if qos_enabled: db = WithQoS(db, qos_max_tries, qos_wait_time) # Arguments. args = parse_args() # Command runner. cmd_runner = CmdRunner() # Repository. retdec_repo_dir = config['daemon']['retdec_repo_dir'] if not retdec_repo_dir: raise ValueError("'retdec_repo_dir' in [daemon] is not set") elif retdec_repo_dir == config['runner']['retdec_repo_dir']: raise ValueError('runner and daemon cannot share the same repository') repo = Repository(retdec_repo_dir, cmd_runner) if qos_enabled:
def test_method_is_called_not_called_when_max_tries_is_0(self): qos = WithQoS(self.obj, max_tries=0) qos.method() self.assertFalse(self.obj.method.called)
def test_method_returns_correct_return_value(self): qos = WithQoS(self.obj) self.obj.method.return_value = 42 self.assertEqual(qos.method(), 42)
def test_method_is_called_once_with_correct_arguments_when_no_exception( self): qos = WithQoS(self.obj) qos.method('a', b='b') self.assertEqual(self.obj.method.call_args_list, [mock.call('a', b='b')])