def wait_for_experiment_state(experiment_id: int, target_state: str, max_wait_secs: int=conf.DEFAULT_MAX_WAIT_SECS, log_every: int=60) -> None:
    for seconds_waited in range(max_wait_secs):
        try:
            state = experiment_state(experiment_id)
        except api.errors.MasterNotFoundException:
            logging.warning('Network failure ignored when polling for state of experiment {}'.format(experiment_id))
            time.sleep(1)
            continue
        if (state == target_state):
            return
        if is_terminal_state(state):
            if (state != target_state):
                report_failed_experiment(experiment_id)
            pytest.fail(f'Experiment {experiment_id} terminated in {state} state, expected {target_state}')
        if ((seconds_waited > 0) and ((seconds_waited % log_every) == 0)):
            print(f'Waited {seconds_waited} seconds for experiment {experiment_id} (currently {state}) to reach {target_state}')
        time.sleep(1)
    else:
        if (target_state == 'COMPLETED'):
            cancel_experiment(experiment_id)
        report_failed_experiment(experiment_id)
        pytest.fail('Experiment did not reach target state {} after {} seconds'.format(target_state, max_wait_secs))
def wait_for_experiment_state(experiment_id: int, target_state: str, max_wait_secs: int=conf.DEFAULT_MAX_WAIT_SECS, log_every: int=60) -> None:
    for seconds_waited in range(max_wait_secs):
        try:
            state = experiment_state(experiment_id)
        except api.errors.MasterNotFoundException:
            logging.warning('Network failure ignored when polling for state of experiment {}'.format(experiment_id))
            time.sleep(1)
            continue
        if (state == target_state):
            return
        if is_terminal_state(state):
            if (state != target_state):
                report_failed_experiment(experiment_id)
            pytest.fail(f'Experiment {experiment_id} terminated in {state} state, expected {target_state}')
        if ((seconds_waited > 0) and ((seconds_waited % log_every) == 0)):
            print(f'Waited {seconds_waited} seconds for experiment {experiment_id} (currently {state}) to reach {target_state}')
        time.sleep(1)
    else:
        if (target_state == 'COMPLETED'):
            cancel_experiment(experiment_id)
        report_failed_experiment(experiment_id)
        pytest.fail('Experiment did not reach target state {} after {} seconds'.format(target_state, max_wait_secs))