def test_alert_publish(volttron_instance1): """ Tests the heartbeat message that it has the status. :param volttron_instance1: :return: """ global subscription_results subscription_results.clear() alert_prefix = 'alerts' new_agent = volttron_instance1.build_agent(identity='alert1') status = Status.build(BAD_STATUS, "Too many connections!") new_agent.vip.pubsub.subscribe(peer='pubsub', prefix='', callback=onmessage) gevent.sleep(0.3) orig_status = new_agent.vip.health.send_alert("too_many", status) poll_gevent_sleep(2, lambda: messages_contains_prefix(alert_prefix, subscription_results)) print("THE SUBSCRIPTIONS ARE: {}".format(subscription_results)) if not messages_contains_prefix(alert_prefix, subscription_results): pytest.fail('prefix not found') headers = subscription_results['alerts/Agent']['headers'] message = subscription_results['alerts/Agent']['message'] assert "too_many", headers['alert_key'] passed_status = Status.from_json(message) assert status.status == passed_status.status assert status.context == passed_status.context assert status.last_updated == passed_status.last_updated
def test_alert_publish(volttron_instance): """ Tests the heartbeat message that it has the status. :param volttron_instance: :return: """ global subscription_results subscription_results.clear() alert_prefix = 'alerts' new_agent = volttron_instance.build_agent(identity='alert1') status = Status.build(BAD_STATUS, "Too many connections!") new_agent.vip.pubsub.subscribe(peer='pubsub', prefix='', callback=onmessage) gevent.sleep(0.3) orig_status = new_agent.vip.health.send_alert("too_many", status) poll_gevent_sleep(2, lambda: messages_contains_prefix(alert_prefix, subscription_results)) print("THE SUBSCRIPTIONS ARE: {}".format(subscription_results)) if not messages_contains_prefix(alert_prefix, subscription_results): pytest.fail('prefix not found') headers = subscription_results['alerts/Agent']['headers'] message = subscription_results['alerts/Agent']['message'] assert "too_many", headers['alert_key'] passed_status = Status.from_json(message) assert status.status == passed_status.status assert status.context == passed_status.context assert status.last_updated == passed_status.last_updated
def test_publish_from_message_handler(volttron_instance): """ Tests the ability to change a status by sending a different status code. This test also tests that the heartbeat is received. :param volttron_instance: :return: """ test_topic = "testtopic1/test" new_agent1 = volttron_instance.build_agent( identity='test_publish1', agent_class=_publish_from_handler_test_agent) new_agent2 = volttron_instance.build_agent(identity='test_publish2') # new_agent1.setup_callback("") new_agent2.vip.pubsub.publish("pubsub", test_topic, headers={}, message="Test message").get() poll_gevent_sleep( 2, lambda: messages_contains_prefix(test_topic, new_agent1. subscription_results)) assert new_agent1.subscription_results[test_topic][ "message"] == "Test message"
def test_heartbeat_sending_status(volttron_instance): """ Tests the heartbeat message that it has the status. :param volttron_instance: :return: """ global subscription_results subscription_results.clear() agent_prefix = 'heartbeat/Agent' new_agent = volttron_instance.build_agent(identity='test3') orig_status = new_agent.vip.health.get_status() new_agent.vip.pubsub.subscribe(peer='pubsub', prefix=agent_prefix, callback=onmessage) new_agent.vip.heartbeat.start() poll_gevent_sleep(2, lambda: messages_contains_prefix(agent_prefix, subscription_results)) print subscription_results message = subscription_results[agent_prefix]['message'] headers = subscription_results[agent_prefix]['headers'] d = message assert headers[DATE] is not None assert d["last_updated"] is not None assert orig_status["status"] == d["status"] assert orig_status["context"] == d["context"]
def test_heartbeat_sending_status(volttron_instance1): """ Tests the heartbeat message that it has the status. :param volttron_instance1: :return: """ global subscription_results subscription_results.clear() agent_prefix = 'heartbeat/Agent' new_agent = volttron_instance1.build_agent(identity='test3') orig_status = Status.from_json(new_agent.vip.health.get_status()) new_agent.vip.pubsub.subscribe(peer='pubsub', prefix=agent_prefix, callback=onmessage) new_agent.vip.heartbeat.start() poll_gevent_sleep( 2, lambda: messages_contains_prefix(agent_prefix, subscription_results)) message = subscription_results[agent_prefix]['message'] headers = subscription_results[agent_prefix]['headers'] d = Status.from_json(message) assert headers[DATE] is not None assert d.last_updated is not None assert orig_status.status == d.status assert orig_status.context == d.context
def test_can_set_status(volttron_instance1): """ Tests the ability to change a status by sending a different status code. This test also tests that the heartbeat is received. :param volttron_instance1: :return: """ global subscription_results subscription_results.clear() new_agent = volttron_instance1.build_agent(identity='test_status') new_agent.vip.heartbeat.start() orig_status = Status.from_json(new_agent.vip.health.get_status()) assert orig_status.status == STATUS_GOOD assert orig_status.context is None assert orig_status.last_updated is not None print('original status: {}'.format(orig_status.as_json())) new_context = { 'foo': 'A test something when wrong', 'woah': ['blah', 'blah'] } agent_prefix = 'heartbeat/Agent' new_agent.vip.pubsub.subscribe(peer='pubsub', prefix=agent_prefix, callback=onmessage) gevent.sleep(1) new_agent.vip.health.set_status(STATUS_BAD, new_context) poll_gevent_sleep( 2, lambda: messages_contains_prefix(agent_prefix, subscription_results)) new_status = Status.from_json(new_agent.vip.health.get_status()) print('new status: {}'.format(new_status.as_json())) assert new_status.status == STATUS_BAD assert new_status.context == new_context assert new_status.last_updated is not None print("OLD IS: {}".format(orig_status.last_updated)) print("NEW IS: {}".format(new_status.last_updated)) old_date = parse_timestamp_string(orig_status.last_updated) new_date = parse_timestamp_string(new_status.last_updated) assert old_date < new_date
def test_multiplatform_pubsub(request, multi_platform_connection): p1_publisher, p2_listener, p3_listener = multi_platform_connection def callback2(peer, sender, bus, topicdr, headers, message): print message assert message == [{'point': 'value'}] def callback3(peer, sender, bus, topic, headers, message): print message def callback4(peer, sender, bus, topic, headers, message): print message def callback5(peer, sender, bus, topic, headers, message): print message p2_listener.vip.pubsub.subscribe(peer='pubsub', prefix='devices', callback=onmessage, all_platforms=True) gevent.sleep(2) p3_listener.vip.pubsub.subscribe(peer='pubsub', prefix='devices', callback=onmessage) print "publish" prefix = 'devices' for i in range(10): p1_publisher.vip.pubsub.publish(peer='pubsub', topic='devices/campus/building1', message=[{ 'point': 'value' }]) # gevent.sleep(0.1) poll_gevent_sleep( 2, lambda: messages_contains_prefix(prefix, subscription_results)) message = subscription_results['devices/campus/building1']['message'] assert message == [{'point': 'value'}] gevent.sleep(5)
def test_can_set_status(volttron_instance1): """ Tests the ability to change a status by sending a different status code. This test also tests that the heartbeat is received. :param volttron_instance1: :return: """ global subscription_results subscription_results.clear() new_agent = volttron_instance1.build_agent(identity='test_status') new_agent.vip.heartbeat.start() orig_status = Status.from_json(new_agent.vip.health.get_status()) assert orig_status.status == STATUS_GOOD assert orig_status.context is None assert orig_status.last_updated is not None print('original status: {}'.format(orig_status.as_json())) new_context = {'foo': 'A test something when wrong', 'woah': ['blah', 'blah']} agent_prefix = 'heartbeat/Agent' new_agent.vip.pubsub.subscribe(peer='pubsub', prefix=agent_prefix, callback=onmessage) gevent.sleep(1) new_agent.vip.health.set_status(STATUS_BAD, new_context) poll_gevent_sleep(2, lambda: messages_contains_prefix(agent_prefix, subscription_results)) new_status = Status.from_json(new_agent.vip.health.get_status()) print('new status: {}'.format(new_status.as_json())) assert new_status.status == STATUS_BAD assert new_status.context == new_context assert new_status.last_updated is not None print("OLD IS: {}".format(orig_status.last_updated)) print("NEW IS: {}".format(new_status.last_updated)) old_date = parse_timestamp_string(orig_status.last_updated) new_date = parse_timestamp_string(new_status.last_updated) assert old_date < new_date
def test_heartbeat_sending_status(volttron_instance1): """ Tests the heartbeat message that it has the status. :param volttron_instance1: :return: """ global subscription_results subscription_results.clear() agent_prefix = 'heartbeat/Agent' new_agent = volttron_instance1.build_agent(identity='test3') orig_status = Status.from_json(new_agent.vip.health.get_status()) new_agent.vip.pubsub.subscribe(peer='pubsub', prefix=agent_prefix, callback=onmessage) new_agent.vip.heartbeat.start() poll_gevent_sleep(2, lambda: messages_contains_prefix(agent_prefix, subscription_results)) message = subscription_results[agent_prefix]['message'] headers = subscription_results[agent_prefix]['headers'] d = Status.from_json(message) assert headers[DATE] is not None assert d.last_updated is not None assert orig_status.status == d.status assert orig_status.context == d.context
def test_multiplatform_pubsub(request, multi_platform_connection): p1_publisher, p2_listener, p3_listener = multi_platform_connection p2_listener.vip.pubsub.subscribe(peer='pubsub', prefix='devices', callback=onmessage, all_platforms=True) p3_listener.vip.pubsub.subscribe(peer='pubsub', prefix='devices', callback=onmessage) gevent.sleep(1) prefix = 'devices' for i in range(10): p1_publisher.vip.pubsub.publish(peer='pubsub', topic='devices/campus/building1', message=[{ 'point': 'value' }]) poll_gevent_sleep( 5, lambda: messages_contains_prefix(prefix, subscription_results)) message = subscription_results['devices/campus/building1']['message'] assert message == [{'point': 'value'}]
def test_multiplatform_2_publishers(request, five_platform_connection): subscription_results2 = {} subscription_results3 = {} subscription_results4 = {} subscription_results5 = {} p1_publisher, p2_listener, p3_listener, p4_listener, p5_publisher = five_platform_connection def callback2(peer, sender, bus, topic, headers, message): subscription_results2[topic] = {'headers': headers, 'message': message} print("platform2 sub results [{}] = {}".format( topic, subscription_results2[topic])) def callback3(peer, sender, bus, topic, headers, message): subscription_results3[topic] = {'headers': headers, 'message': message} print("platform3 sub results [{}] = {}".format( topic, subscription_results3[topic])) def callback4(peer, sender, bus, topic, headers, message): subscription_results4[topic] = {'headers': headers, 'message': message} print("platform4 sub results [{}] = {}".format( topic, subscription_results4[topic])) def callback5(peer, sender, bus, topic, headers, message): subscription_results5[topic] = {'headers': headers, 'message': message} print("platform4 sub results [{}] = {}".format( topic, subscription_results5[topic])) p2_listener.vip.pubsub.subscribe(peer='pubsub', prefix='devices', callback=callback2, all_platforms=True) p3_listener.vip.pubsub.subscribe(peer='pubsub', prefix='devices', callback=callback3, all_platforms=True) gevent.sleep(2) p4_listener.vip.pubsub.subscribe(peer='pubsub', prefix='analysis', callback=callback4, all_platforms=True) p5_publisher.vip.pubsub.subscribe(peer='pubsub', prefix='analysis', callback=callback5) gevent.sleep(2) print "publish" prefix = 'devices' for i in range(5): p1_publisher.vip.pubsub.publish(peer='pubsub', topic='devices/campus/building1', message=[{ 'point': 'value' }]) poll_gevent_sleep( 1, lambda: messages_contains_prefix(prefix, subscription_results2)) message = subscription_results2['devices/campus/building1']['message'] assert message == [{'point': 'value'}] message = subscription_results3['devices/campus/building1']['message'] assert message == [{'point': 'value'}] prefix = 'analysis' for i in range(5): p5_publisher.vip.pubsub.publish( peer='pubsub', topic='analysis/airside/campus/building1', message=[{ 'result': 'pass' }]) # gevent.sleep(0.1) poll_gevent_sleep( 2, lambda: messages_contains_prefix(prefix, subscription_results3)) message = subscription_results4['analysis/airside/campus/building1'][ 'message'] assert message == [{'result': 'pass'}] message = subscription_results5['analysis/airside/campus/building1'][ 'message'] assert message == [{'result': 'pass'}]