def test_predict_dummy_server_raise(flask_dummy_server, fake_input_dir, tmp_output_dir): with pytest.raises(Exception): predict(flask_dummy_server, 0, fake_input_dir, tmp_output_dir, timeout=10)
def test_identical_predict(flask_prediction_server, fake_input_dir, tmp_output_dir, tmpdir): second_output_dir = str(tmpdir) for step in [0, 1, 50]: predict(flask_prediction_server, step, fake_input_dir, tmp_output_dir) for step in reversed([0, 1, 50]): predict(flask_prediction_server, step, fake_input_dir, second_output_dir) for step in [0, 1, 50]: assert filecmp.cmp( os.path.join(tmp_output_dir, 'Y{}.h5'.format(step)), os.path.join(second_output_dir, 'Y{}.h5'.format(step)))
def test_launch_server_empty_code_dir(flask_prediction_server, code_dir, fake_input_dir, model_dir, tmp_output_dir, str_tmpdir): # Check no server is running try: send_shutdown('http://localhost:4130') except requests.exceptions.ConnectionError: pass except Exception as e: raise e # Launch server configs = {'code_dir': str_tmpdir, 'model_dir': model_dir} # Test with pytest.raises(Exception): try: process = start_prediction_server(configs, gpu_count=0, port=4130, return_process=True) predict('localhost:4130', 0, fake_input_dir, tmp_output_dir, timeout=30) assert os.path.isfile(os.path.join(tmp_output_dir, 'Y0.h5')) except Exception as e: print('Launch server empty model_dir. Raising exception {}'.format( e)) raise e # Stop server try: send_shutdown('http://localhost:4130') process.terminate() except: pass
def test_predict(flask_prediction_server, eval_step, fake_input_dir, tmp_output_dir): predict(flask_prediction_server, eval_step, fake_input_dir, tmp_output_dir) assert os.path.isfile( os.path.join(tmp_output_dir, 'Y{}.h5'.format(eval_step)))
def test_predict_output_dir_not_created(flask_prediction_server, fake_input_dir, tmp_output_dir): with pytest.raises(Exception): predict(flask_prediction_server, 0, fake_input_dir, os.path.join(tmp_output_dir, 'not_created_yet/'))
def test_predict_wrong_input_dir(flask_prediction_server, tmp_output_dir, tmpdir): with pytest.raises(Exception): predict(flask_prediction_server, 0, str(tmpdir), tmp_output_dir)
try: wait_flask_alive(timeout=0.1, check_interval=0.01) except: # Prepare model for prediction prepare_model(configs) # Launch server if use flask start_prediction_server_function(configs, gpu_count=args.gpu_count, port=4130) # Predict server_full_address = full_address('127.0.0.1', port=4130) try: predict(server_full_address, configs['eval_step'], configs['input_dir'], configs['output_dir'], timeout=600) except: flask_log_file = os.path.join(configs['code_dir'], 'flask_log.txt') flask_error_log_file = os.path.join(configs['code_dir'], 'flask_error_log.txt') if os.path.isfile(flask_log_file): with open(flask_log_file, 'r') as f: log = "".join(f.readlines()[-4:]) else: log = 'Could not open flask log file. Flask had an exception before the logging handler.' if os.path.isfile(flask_error_log_file): with open(flask_error_log_file, 'r') as f: error_log = f.read()