"""Help functions for Distribution Fee integration test.""" from datetime import datetime from datetime import timedelta from subprocess import Popen import time import boto.swf.layer1 as swf from flows import datastore from flows.distribution_fee import config as flow_config from integration_tests.flows.distribution_fee import config from integration_tests.flows.distribution_fee.fixtures import joined_data def read_test_data(table_name): """Read test data that will be used to populate tables.""" return [ entry[table_name] for entry in joined_data.entries if table_name in entry] def execute_flow(upcs): """Execute Distribution Fee workflow.""" try: try: swf.Layer1().terminate_workflow_execution( config.DOMAIN, flow_config.SWF_WORKFLOW_ID) except swf.SWFResponseError: # if no flow with the same id running pass # start worker process worker_process = Popen( [config.PYTHON_PATH, config.APPLICATION_PATH, 'worker', 'distribution_fee']) # start decider process decider_process = Popen( [config.PYTHON_PATH, config.APPLICATION_PATH, 'decider', 'distribution_fee']) # waiting for activities initialization time.sleep(20) # start execution process command_line = [ config.PYTHON_PATH, config.APPLICATION_PATH, 'execute', 'distribution_fee'] for upc in upcs: command_line.append('-u') command_line.append(upc) exec_process = Popen(command_line) exec_process.wait() # check that log record was created run_id, *_ = datastore.query(""" SELECT workflow_run_id FROM {db_name}.{table_name}""".format( db_name=config.TEST_DS_DB_NAME, table_name=flow_config.TABLE_ETL_LOG)).fetchone() # wait for workflow execution end max_time = datetime.now() + timedelta( seconds=config.FLOW_EXECUTION_TIMEOUT) while True: execution = swf.Layer1().describe_workflow_execution( domain=config.DOMAIN, workflow_id=flow_config.SWF_WORKFLOW_ID, run_id=run_id) time.sleep(1) if execution['executionInfo'].get('executionStatus') != 'CLOSED': time.sleep(10) if datetime.now() > max_time: raise TimeoutError('Theatrical flow execution timeout') else: break except Exception as e: print(e) raise finally: worker_process.kill() decider_process.kill()