def test_insert_params_fails_not_reserved(self, monkeypatch): """Test that failed insertion because of duplicated trials will not reserve the original trial """ mock_space_iterate(monkeypatch) with create_experiment(config, base_trial) as (cfg, experiment, client): with pytest.raises(DuplicateKeyError): client.insert(dict(x=1), reserve=True) assert client._pacemakers == {}
def test_suggest(self, monkeypatch): """Verify that suggest reserved availabe trials.""" mock_space_iterate(monkeypatch) with create_experiment(config, base_trial) as (cfg, experiment, client): trial = client.suggest() assert trial.status == "reserved" assert trial.params["x"] == 1 assert len(experiment.fetch_trials()) == 5 assert client._pacemakers[trial.id].is_alive() client._pacemakers.pop(trial.id).stop()
def test_insert_existing_params(self, monkeypatch): """Test that duplicated trials cannot be saved in storage""" mock_space_iterate(monkeypatch) with create_experiment(config, base_trial) as (cfg, experiment, client): with pytest.raises(DuplicateKeyError) as exc: client.insert(dict(x=1)) assert ( "A trial with params {'x': 1} already exist for experiment supernaekei-v1" == str(exc.value) ) assert client._pacemakers == {}
def test_suggest_race_condition(self, monkeypatch): """Verify that race conditions to register new trials is handled""" mock_space_iterate(monkeypatch) new_value = 50.0 # algo will suggest once an already existing trial def amnesia(num=1): """Suggest a new value and then always suggest the same""" if amnesia.count == 0: value = [0] else: value = [new_value] amnesia.count += 1 return [value] amnesia.count = 0 with create_experiment(config, base_trial, statuses=["completed"]) as ( cfg, experiment, client, ): monkeypatch.setattr(experiment.algorithms, "suggest", amnesia) assert len(experiment.fetch_trials()) == 1 trial = client.suggest() assert trial.status == "reserved" assert trial.params["x"] == new_value assert amnesia.count == 2 assert len(experiment.fetch_trials()) == 2 assert client._pacemakers[trial.id].is_alive() client._pacemakers.pop(trial.id).stop()
def test_suggest_race_condition(self, monkeypatch): """Verify that race conditions to register new trials is handled""" mock_space_iterate(monkeypatch) new_value = 50.0 with create_experiment(config, base_trial, statuses=["completed"]) as ( cfg, experiment, client, ): # algo will suggest once an already existing trial def amnesia(num=1): """Suggest a new value and then always suggest the same""" return [format_trials.tuple_to_trial([0], experiment.space)] monkeypatch.setattr(experiment.algorithms, "suggest", amnesia) assert len(experiment.fetch_trials()) == 1 with pytest.raises(WaitingForTrials): trial = client.suggest() assert len(experiment.fetch_trials()) == 1