def test_exceptions_inside_log_in_smbo(smbo_run_mock, backend, dask_client): # Below importing and shutdown is a workaround, to make sure # we reset the port to collect messages. Randomly, when running # this test with multiple other test at the same time causes this # test to fail. This resets the singletons of the logging class import logging logging.shutdown() automl = autosklearn.automl.AutoML( backend, 20, 5, metric=accuracy, dask_client=dask_client, ) dataset_name = 'test_exceptions_inside_log' # Create a custom exception to prevent other errors to slip in class MyException(Exception): pass X_train, Y_train, X_test, Y_test = putil.get_dataset('iris') # The first call is on dummy predictor failure message = str(np.random.randint(100)) + '_run_smbo' smbo_run_mock.side_effect = MyException(message) with pytest.raises(MyException): automl.fit( X_train, Y_train, task=MULTICLASS_CLASSIFICATION, dataset_name=dataset_name, ) # make sure that the logfile was created import shutil shutil.copytree(backend.temporary_directory, '/tmp/trydebug') logger_name = 'AutoML(%d):%s' % (1, dataset_name) logfile = os.path.join(backend.temporary_directory, logger_name + '.log') assert os.path.exists(logfile), automl._clean_logger() with open(logfile) as f: assert message in f.read(), automl._clean_logger() # Speed up the closing after forced crash automl._clean_logger()
def test_exceptions_inside_log_in_smbo(smbo_run_mock, backend, dask_client): # Below importing and shutdown is a workaround, to make sure # we reset the port to collect messages. Randomly, when running # this test with multiple other test at the same time causes this # test to fail. This resets the singletons of the logging class import logging logging.shutdown() automl = autosklearn.automl.AutoML( backend, 20, 5, metric=accuracy, dask_client=dask_client, ) dataset_name = 'test_exceptions_inside_log' # Create a custom exception to prevent other errors to slip in class MyException(Exception): pass X_train, Y_train, X_test, Y_test = putil.get_dataset('iris') # The first call is on dummy predictor failure message = str(np.random.randint(100)) + '_run_smbo' smbo_run_mock.side_effect = MyException(message) with pytest.raises(MyException): automl.fit( X_train, Y_train, task=MULTICLASS_CLASSIFICATION, dataset_name=dataset_name, ) # make sure that the logfile was created logger_name = 'AutoML(%d):%s' % (1, dataset_name) logger = logging.getLogger(logger_name) logfile = os.path.join(backend.temporary_directory, logger_name + '.log') assert os.path.exists(logfile), print_debug_information(automl) + str(automl._clean_logger()) # Give some time for the error message to be printed in the # log file found_message = False for incr_tolerance in range(5): with open(logfile) as f: lines = f.readlines() if any(message in line for line in lines): found_message = True break else: time.sleep(incr_tolerance) # Speed up the closing after forced crash automl._clean_logger() if not found_message: pytest.fail("Did not find {} in the log file {} for logger {}/{}/{}".format( message, print_debug_information(automl), vars(automl._logger.logger), vars(logger), vars(logging.getLogger()) ))