def dispatch(command_input): """ currently this will handle the given command_input by testing whether it fulfills a stub configuration. If so it will save a execution (with the expected flag set to true) and send the next answer as defined in the stub configuration object. """ stub_configurations = deserialize_stub_configurations( CONFIGURED_STUBS_FILENAME) logging.info('Got %s', command_input) execution = Execution( command_input.command, command_input.arguments, command_input.stdin) for stub_configuration in stub_configurations: if command_input.fulfills(stub_configuration.command_input): logging.info('Execution fulfills %s', stub_configuration) execution.mark_as_expected() record_execution(execution) answer = stub_configuration.next_answer() serialize_as_dictionaries( CONFIGURED_STUBS_FILENAME, stub_configurations) unlock(lock_handle) if answer.milliseconds_to_wait: time.sleep(answer.milliseconds_to_wait / 1000) send_answer(answer) return unlock(lock_handle) logging.error( 'Given command_input does not fulfill requirements of any stub configuration.') sys.exit(255)
def dispatch(command_input): """ currently this will handle the given command_input by testing whether it fulfills a stub configuration. If so it will save a execution (with the expected flag set to true) and send the next answer as defined in the stub configuration object. """ stub_configurations = deserialize_stub_configurations( CONFIGURED_STUBS_FILENAME) logging.info('Got %s', command_input) execution = Execution(command_input.command, command_input.arguments, command_input.stdin) for stub_configuration in stub_configurations: if command_input.fulfills(stub_configuration.command_input): logging.info('Execution fulfills %s', stub_configuration) execution.mark_as_expected() record_execution(execution) answer = stub_configuration.next_answer() serialize_as_dictionaries(CONFIGURED_STUBS_FILENAME, stub_configurations) unlock(lock_handle) if answer.milliseconds_to_wait: time.sleep(answer.milliseconds_to_wait / 1000) send_answer(answer) return unlock(lock_handle) logging.error( 'Given command_input does not fulfill requirements of any stub configuration.' ) sys.exit(255)
def record_execution(execution): """ loads the list of recent executions from the EXECUTIONS_FILENAME file, appends the given execution to the list, then writes the list back to the file again. To assure only one process is reading and writing the file a file lock is used. """ lock_file_handle = lock() executions = [] if os.path.exists(EXECUTIONS_FILENAME): executions = deserialize_executions(EXECUTIONS_FILENAME) executions.append(execution) serialize_as_dictionaries(EXECUTIONS_FILENAME, executions) logging.info('Recorded %s executions.', len(executions)) unlock(lock_file_handle)
def test_should_unlock(self): file_handle_mock = Mock() unlock(file_handle_mock) self.assertEqual(call(), file_handle_mock.close.call_args)