def test_invalid_strategy(self): with raises(BlueSkyConfigurationError) as e_info: io.wait_for_availability({ "strategy": "foo", "time": 0.1, "max_attempts": 2 }) assert e_info.value.args[0] == io.INVALID_WAIT_STRATEGY_MSG
def test_no_exception(self, monkeypatch): self.monkeypatch_sleep(monkeypatch) io.wait_for_availability({}) def no_exception(): return 1 assert no_exception() == 1 assert self.sleepcount == 0 assert self.total_sleep == 0.0
def test_bskur_error(self, monkeypatch): self.monkeypatch_sleep(monkeypatch) io.wait_for_availability({}) def bskur_error(): raise BlueSkyUnavailableResourceError() with raises(BlueSkyUnavailableResourceError) as e_info: bskur_error() assert self.sleepcount == 0 assert self.total_sleep == 0.0
def test_other_error(self, monkeypatch): self.monkeypatch_sleep(monkeypatch) io.wait_for_availability({}) def other_error(): raise RuntimeError() with raises(RuntimeError) as e_info: other_error() assert self.sleepcount == 0 assert self.total_sleep == 0.0
def test_invalid_max_attempts(self): with raises(BlueSkyConfigurationError) as e_info: io.wait_for_availability({ "strategy": "fixed", "time": 0.1, "max_attempts": -2 }) assert e_info.value.args[0] == io.INVALID_WAIT_MAX_ATTEMPTS_MSG with raises(BlueSkyConfigurationError) as e_info: io.wait_for_availability({ "strategy": "fixed", "time": 0.1, "max_attempts": 2.4 }) assert e_info.value.args[0] == io.INVALID_WAIT_MAX_ATTEMPTS_MSG
def _load_source(source): # File loaders raise BlueSkyUnavailableResourceError in the # constructor, while API loaders raise it in 'load'. So, # we need to decorate both the construction and load with # 'io.wait_for_availability' wait_dec = io.wait_for_availability(source.get('wait')) loader = wait_dec(_import_loader(source))(**source) return wait_dec(loader.load)()
def test_partial_config(self): with raises(BlueSkyConfigurationError) as e_info: io.wait_for_availability({"strategy": "fixed"}) assert e_info.value.args[0] == io.INVALID_WAIT_CONFIG_MSG with raises(BlueSkyConfigurationError) as e_info: io.wait_for_availability({"time": 1}) assert e_info.value.args[0] == io.INVALID_WAIT_CONFIG_MSG with raises(BlueSkyConfigurationError) as e_info: io.wait_for_availability({"max_attempts": 10}) assert e_info.value.args[0] == io.INVALID_WAIT_CONFIG_MSG with raises(BlueSkyConfigurationError) as e_info: io.wait_for_availability({"strategy": "fixed", "time": 1}) assert e_info.value.args[0] == io.INVALID_WAIT_CONFIG_MSG with raises(BlueSkyConfigurationError) as e_info: io.wait_for_availability({"strategy": "fixed", "max_attempts": 3}) assert e_info.value.args[0] == io.INVALID_WAIT_CONFIG_MSG with raises(BlueSkyConfigurationError) as e_info: io.wait_for_availability({"time": 1, "max_attempts": 3}) assert e_info.value.args[0] == io.INVALID_WAIT_CONFIG_MSG
def test_no_valid_config_options(self): with raises(BlueSkyConfigurationError) as e_info: io.wait_for_availability({"foo": "bar"}) assert e_info.value.args[0] == io.INVALID_WAIT_CONFIG_MSG
io.wait_for_availability({}) def other_error(): raise RuntimeError() with raises(RuntimeError) as e_info: other_error() assert self.sleepcount == 0 assert self.total_sleep == 0.0 fixed_wait = io.wait_for_availability({ "strategy": "fixed", "time": 2.0, "max_attempts": 5 }) class TestWaitForAvailabilityFixed(TestWaitForAvailabilityBase): def test_no_exception(self, monkeypatch): self.monkeypatch_sleep(monkeypatch) @fixed_wait def successful(): self.counter += 1 return 2 assert successful() == 2 assert self.counter == 1