def test_tmgr_base_heartbeat(): sid = 'test.0000' rmgr = BaseRmgr({}, sid, None, {}) os.environ['ENTK_HB_INTERVAL'] = '30' tmgr = BaseTmgr(sid=sid, pending_queue=['pending-1'], completed_queue=['completed-1'], rmgr=rmgr, mq_hostname=hostname, port=port, rts=None) tmgr._hb_terminate = threading.Event() tmgr._hb_thread = threading.Thread(target=tmgr._heartbeat, name='heartbeat') tmgr._hb_thread.start() proc = Process(target=func_for_heartbeat_test, args=(hostname, port, tmgr._hb_request_q, tmgr._hb_response_q)) proc.start() proc.join() tmgr._hb_thread.join()
def test_tmgr_base_heartbeat(): sid = 'test.0000' rmgr = BaseRmgr({}, sid, None, {}) os.environ['ENTK_HB_INTERVAL'] = '30' tmgr = BaseTmgr(sid=sid, pending_queue=['pending-1'], completed_queue=['completed-1'], rmgr=rmgr, mq_hostname=hostname, port=port, rts=None) tmgr._hb_terminate = threading.Event() tmgr._hb_thread = threading.Thread(target=tmgr._heartbeat, name='heartbeat') tmgr._hb_thread.start() proc = Process(target=func_for_heartbeat_test, args=(hostname, port, tmgr._hb_request_q, tmgr._hb_response_q)) proc.start() proc.join() tmgr._hb_thread.join()
def test_start_heartbeat(self, mocked_init, mocked_Logger, mocked_Profiler): rmq_params = mock.MagicMock(spec=ConnectionParameters) rmgr = mock.MagicMock(spec=Base_ResourceManager) tmgr = Tmgr('test_tmgr', ['pending_queues'], ['completed_queues'], rmgr, rmq_params, 'test_rts') global_boolean = False def _heartbeat_side_effect(): nonlocal global_boolean global_boolean = True tmgr._log = mocked_Logger tmgr._prof = mocked_Profiler tmgr._heartbeat = mock.MagicMock(side_effect=_heartbeat_side_effect) tmgr._uid = 'tmgr.0000' tmgr._hb_terminate = None tmgr._hb_thread = None tmgr.start_heartbeat() try: self.assertTrue(global_boolean) self.assertIsInstance(tmgr._hb_terminate, mt.Event) self.assertIsInstance(tmgr._hb_thread, mt.Thread) finally: if tmgr._hb_thread.is_alive(): tmgr._hb_thread.join()
def test_tmgr_base_terminate_heartbeat(): credentials = pika.PlainCredentials(username, password) rmq_conn_params = pika.ConnectionParameters(host=hostname, port=port, credentials=credentials) sid = 'test.0005' rmgr = BaseRmgr({}, sid, None, {}) tmgr = BaseTmgr(sid=sid, pending_queue=['pending-1'], completed_queue=['completed-1'], rmgr=rmgr, rmq_conn_params=rmq_conn_params, rts=None) tmgr._hb_terminate = mt.Event() assert tmgr.start_heartbeat() assert not tmgr._hb_terminate.is_set() assert tmgr._hb_thread.is_alive() tmgr.terminate_heartbeat() time.sleep(5) assert not tmgr._hb_thread assert tmgr._hb_terminate.is_set()
def test_tmgr_base_heartbeat(): credentials = pika.PlainCredentials(username, password) rmq_conn_params = pika.ConnectionParameters(host=hostname, port=port, credentials=credentials) sid = 'test.0003' rmgr = BaseRmgr({}, sid, None, {}) tmgr = BaseTmgr(sid=sid, pending_queue=['pending-1'], completed_queue=['completed-1'], rmgr=rmgr, rmq_conn_params=rmq_conn_params, rts=None) tmgr._hb_terminate = mt.Event() tmgr._hb_thread = mt.Thread(target=tmgr._heartbeat, name='heartbeat') tmgr._hb_thread.start() proc = mp.Process(target=func_for_heartbeat_test, args=(hostname, port, username, password, tmgr._hb_request_q, tmgr._hb_response_q)) proc.start() proc.join() tmgr._hb_thread.join()
def test_tmgr_base_check_heartbeat(): sid = 'test.0007' rmgr = BaseRmgr({}, sid, None, {}) tmgr = BaseTmgr(sid=sid, pending_queue=['pending-1'], completed_queue=['completed-1'], rmgr=rmgr, mq_hostname=hostname, port=port, rts=None) tmgr._hb_thread = mt.Thread(target=tmgr._heartbeat, name='heartbeat') tmgr._hb_terminate = mt.Event() tmgr._hb_thread.start() assert tmgr.check_heartbeat() tmgr.terminate_heartbeat()
def test_tmgr_base_check_heartbeat(): rmq_conn_params = pika.ConnectionParameters(host=hostname, port=port) sid = 'test.0007' rmgr = BaseRmgr({}, sid, None, {}) tmgr = BaseTmgr(sid=sid, pending_queue=['pending-1'], completed_queue=['completed-1'], rmgr=rmgr, rmq_conn_params=rmq_conn_params, rts=None) tmgr._hb_thread = mt.Thread(target=tmgr._heartbeat, name='heartbeat') tmgr._hb_terminate = mt.Event() tmgr._hb_thread.start() assert tmgr.check_heartbeat() tmgr.terminate_heartbeat()
def test_tmgr_base_check_heartbeat(): sid = 'test.0000' rmgr = BaseRmgr({}, sid, None, {}) os.environ['ENTK_HB_INTERVAL'] = '30' tmgr = BaseTmgr(sid=sid, pending_queue=['pending-1'], completed_queue=['completed-1'], rmgr=rmgr, mq_hostname=hostname, port=port, rts=None) tmgr._hb_thread = threading.Thread(target=tmgr._heartbeat, name='heartbeat') tmgr._hb_terminate = threading.Event() tmgr._hb_thread.start() assert tmgr.check_heartbeat() tmgr.terminate_heartbeat()
def test_tmgr_base_check_heartbeat(): sid = 'test.0000' rmgr = BaseRmgr({}, sid, None, {}) os.environ['ENTK_HB_INTERVAL'] = '30' tmgr = BaseTmgr(sid=sid, pending_queue=['pending-1'], completed_queue=['completed-1'], rmgr=rmgr, mq_hostname=hostname, port=port, rts=None) tmgr._hb_thread = threading.Thread(target=tmgr._heartbeat, name='heartbeat') tmgr._hb_terminate = threading.Event() tmgr._hb_thread.start() assert tmgr.check_heartbeat() tmgr.terminate_heartbeat()
def test_tmgr_base_terminate_heartbeat(): sid = 'test.0005' rmgr = BaseRmgr({}, sid, None, {}) tmgr = BaseTmgr(sid=sid, pending_queue=['pending-1'], completed_queue=['completed-1'], rmgr=rmgr, mq_hostname=hostname, port=port, rts=None) tmgr._hb_terminate = mt.Event() assert tmgr.start_heartbeat() assert not tmgr._hb_terminate.is_set() assert tmgr._hb_thread.is_alive() tmgr.terminate_heartbeat() time.sleep(5) assert not tmgr._hb_thread assert tmgr._hb_terminate.is_set()
def test_tmgr_base_terminate_heartbeat(): sid = 'test.0000' rmgr = BaseRmgr({}, sid, None, {}) os.environ['ENTK_HB_INTERVAL'] = '30' tmgr = BaseTmgr(sid=sid, pending_queue=['pending-1'], completed_queue=['completed-1'], rmgr=rmgr, mq_hostname=hostname, port=port, rts=None) tmgr._hb_terminate = threading.Event() assert tmgr.start_heartbeat() assert not tmgr._hb_terminate.is_set() assert tmgr._hb_thread.is_alive() tmgr.terminate_heartbeat() sleep(30) assert not tmgr._hb_thread assert tmgr._hb_terminate.is_set()
def test_tmgr_base_terminate_heartbeat(): sid = 'test.0000' rmgr = BaseRmgr({}, sid, None, {}) os.environ['ENTK_HB_INTERVAL'] = '30' tmgr = BaseTmgr(sid=sid, pending_queue=['pending-1'], completed_queue=['completed-1'], rmgr=rmgr, mq_hostname=hostname, port=port, rts=None) tmgr._hb_terminate = threading.Event() assert tmgr.start_heartbeat() assert not tmgr._hb_terminate.is_set() assert tmgr._hb_thread.is_alive() tmgr.terminate_heartbeat() sleep(30) assert not tmgr._hb_thread assert tmgr._hb_terminate.is_set()
def test_terminate_heartbeat(self, mocked_init, mocked_Logger, mocked_Profiler, mocked_BlockingConnection): mocked_BlockingConnection.channel = mock.MagicMock( spec=pika.BlockingConnection.channel) mocked_BlockingConnection.close = mock.MagicMock(return_value=None) mocked_BlockingConnection.channel.queue_delete = mock.MagicMock( return_value=None) mocked_BlockingConnection.channel.queue_declare = mock.MagicMock( return_value=None) mocked_BlockingConnection.close = mock.MagicMock(return_value=None) rmq_params = mock.MagicMock(spec=ConnectionParameters) rmgr = mock.MagicMock(spec=Base_ResourceManager) tmgr = Tmgr('test_tmgr', ['pending_queues'], ['completed_queues'], rmgr, rmq_params, 'test_rts') tmgr._rmq_conn_params = rmq_params tmgr._hb_request_q = 'test_tmgr-hb-request' tmgr._hb_response_q = 'test_tmgr-hb-response' tmgr._log = mocked_Logger tmgr._prof = mocked_Profiler tmgr._uid = 'tmgr.0000' tmgr.check_heartbeat = mock.MagicMock(return_value=True) tmgr.check_manager = mock.MagicMock(return_value=True) tmgr._hb_terminate = mt.Event() tmgr._hb_thread = mt.Thread(target=_tmgr_side_effect, name='test_tmgr', args=(tmgr._hb_terminate, )) tmgr._hb_thread.start() tmgr.terminate_heartbeat() self.assertIsNone(tmgr._hb_thread)
def test_heartbeat(self, mocked_init, mocked_Profiler): hostname = os.environ.get('RMQ_HOSTNAME', 'localhost') port = int(os.environ.get('RMQ_PORT', '5672')) username = os.environ.get('RMQ_USERNAME', 'guest') password = os.environ.get('RMQ_PASSWORD', 'guest') credentials = pika.PlainCredentials(username, password) rmq_conn_params = pika.ConnectionParameters(host=hostname, port=port, credentials=credentials) tmgr = BaseTmgr(None, None, None, None, None, None) tmgr._uid = 'tmgr.0000' tmgr._log = ru.Logger('radical.entk.manager.base', level='DEBUG') tmgr._prof = mocked_Profiler tmgr._hb_interval = 0.1 tmgr._hb_terminate = mt.Event() tmgr._hb_request_q = 'tmgr-hb-request' tmgr._hb_response_q = 'tmgr-hb-response' tmgr._rmq_conn_params = rmq_conn_params mq_connection = pika.BlockingConnection(rmq_conn_params) mq_channel = mq_connection.channel() mq_channel.queue_declare(queue='tmgr-hb-request') mq_channel.queue_declare(queue='tmgr-hb-response') tmgr._log.info('Starting test') master_thread = mt.Thread(target=tmgr._heartbeat, name='tmgr_heartbeat') master_thread.start() time.sleep(0.1) body = None try: for _ in range(5): while body is None: _, props, body = mq_channel.basic_get( queue='tmgr-hb-request') self.assertEqual(body, b'request') nprops = pika.BasicProperties( correlation_id=props.correlation_id) mq_channel.basic_publish(exchange='', routing_key='tmgr-hb-response', properties=nprops, body='response') self.assertTrue(master_thread.is_alive()) body = None time.sleep(0.5) self.assertFalse(master_thread.is_alive()) master_thread.join() mq_channel.queue_delete(queue='tmgr-hb-request') mq_channel.queue_delete(queue='tmgr-hb-response') mq_channel.queue_declare(queue='tmgr-hb-request') mq_channel.queue_declare(queue='tmgr-hb-response') master_thread = mt.Thread(target=tmgr._heartbeat, name='tmgr_heartbeat') master_thread.start() body = None while body is None: _, props, body = mq_channel.basic_get(queue='tmgr-hb-request') mq_channel.basic_publish(exchange='', routing_key='tmgr-hb-response', body='response') time.sleep(0.2) self.assertFalse(master_thread.is_alive()) except Exception as ex: tmgr._hb_terminate.set() master_thread.join() mq_channel.queue_delete(queue='tmgr-hb-request') mq_channel.queue_delete(queue='tmgr-hb-response') mq_channel.close() mq_connection.close() raise ex else: tmgr._hb_terminate.set() master_thread.join() mq_channel.queue_delete(queue='tmgr-hb-request') mq_channel.queue_delete(queue='tmgr-hb-response') mq_channel.close() mq_connection.close()