def test_background_submission_thread(self): msg = GenericMessage(type="json", message="""{"slug": "something1234"}""") pub = MockTopicPublisher() influx = InfluxMock() p = Processor(watched_topic="/director/scene", msg_slot="message.slug", inactivity_resubmission=1, influxdb_client=influx, debug_pub=pub) # do the thread testing without ROS intervening # without rospy.init_node ... and without threading too ... # just invoke the thread worker method # nothing has been processed by the processor assert p.time_of_last_in_msg is None assert p.last_in_msg is None # as if the message was received on the ROS topic p.process(msg) # check that it was correctly processed assert len(p.debug_pub.messages) == 1 assert len(p.influxdb_client.messages) == 1 assert p.debug_pub.messages[0].field_name == "message.slug" assert p.debug_pub.messages[0].metadata == "something1234" assert "/director/scene" in p.influxdb_client.messages[0] # how run the thread worker - directly, wait before - there is a time check time.sleep(2) p._resubmit_worker() assert len(p.debug_pub.messages) == 2 assert len(p.influxdb_client.messages) == 2 assert p.debug_pub.messages[1].field_name == "message.slug" assert p.debug_pub.messages[1].metadata == "something1234" assert "/director/scene" in p.influxdb_client.messages[1]
def test_get_data_for_influx(self): # prepare message for testing, as if it was received (incoming message) slot = json.loads(real_in_msg_director_scene)["message"] in_msg = GenericMessage(type="json", message=json.dumps(slot)) # get outgoing message (to /lg_stats/debug) - via Processor instance p = Processor(watched_topic="/director/scene", msg_slot="message.slug", strategy="default") out_msg = p._get_outbound_message(in_msg) whole_field_name = "message.slug" # depends on the particular message type above # now finally test the method of submitters: influx = InfluxDirect.get_data_for_influx(out_msg, 'test') assert isinstance(influx, dict) assert influx["tags"]["topic"] == p.watched_topic assert influx["tags"]["field_name"] == whole_field_name assert influx["tags"][ "metadata"] == "bbb94866-2216-41a2-83b4-13ba35a3e9dc__scene-3-1" # real InfluxTelegraf.get_timestamp requires ROS init_node, this is a work-around InfluxTelegraf.get_timestamp = staticmethod(InfluxMock.get_timestamp) influx = InfluxTelegraf.get_data_for_influx(out_msg, 'test') assert isinstance(influx, str) assert p.watched_topic in influx assert influx.find("field_name=\"%s\"" % whole_field_name) > -1 assert influx.find("metadata=\"%s\"" % out_msg.metadata) > -1 # this mock just calls InfluxTelegraf - do the same assertions influx = InfluxMock.get_data_for_influx(out_msg, 'test') assert isinstance(influx, str) assert p.watched_topic in influx assert influx.find("field_name=\"%s\"" % whole_field_name) > -1 assert influx.find("metadata=\"%s\"" % out_msg.metadata) > -1
def test_get_data_for_influx(self): # prepare message for testing, as if it was received (incoming message) slot = json.loads(real_in_msg_director_scene)["message"] in_msg = GenericMessage(type="json", message=json.dumps(slot)) # get outgoing message (to /lg_stats/debug) - via Processor instance p = Processor(watched_topic="/director/scene", msg_slot="message.slug", strategy="default") out_msg = p._get_outbound_message(in_msg) whole_field_name = "message.slug" # depends on the particular message type above # now finally test the method of submitters: influx = InfluxDirect.get_data_for_influx(out_msg, 'test') assert isinstance(influx, dict) assert influx["tags"]["topic"] == p.watched_topic assert influx["tags"]["field_name"] == whole_field_name assert influx["tags"]["metadata"] == "bbb94866-2216-41a2-83b4-13ba35a3e9dc__scene-3-1" # real InfluxTelegraf.get_timestamp requires ROS init_node, this is a work-around InfluxTelegraf.get_timestamp = staticmethod(InfluxMock.get_timestamp) influx = InfluxTelegraf.get_data_for_influx(out_msg, 'test') assert isinstance(influx, str) assert p.watched_topic in influx assert influx.find("field_name=\"%s\"" % whole_field_name) > -1 assert influx.find("metadata=\"%s\"" % out_msg.metadata) > -1 # this mock just calls InfluxTelegraf - do the same assertions influx = InfluxMock.get_data_for_influx(out_msg, 'test') assert isinstance(influx, str) assert p.watched_topic in influx assert influx.find("field_name=\"%s\"" % whole_field_name) > -1 assert influx.find("metadata=\"%s\"" % out_msg.metadata) > -1
def test_message_comparison(self): """ """ p = Processor(msg_slot="application", watched_topic="/lol/rofl") msg1 = Session(application="someapplication1") msg2 = Session(application="someapplication2") assert p._compare_messages(msg1, msg1) assert not p._compare_messages(msg1, msg2)
def test_process(self): pub = MockTopicPublisher() influx = InfluxMock() p = Processor(msg_slot="application", watched_topic="/lol/mock", influxdb_client=influx, debug_pub=pub) msg1 = Session(application="someapplication1") assert p.last_in_msg is None p.process(msg1) assert p.last_in_msg == msg1
def test_publish(self): pub = MockTopicPublisher() p = Processor(watched_topic="/lol/rofl", msg_slot="application", debug_pub=pub, resolution=200) msg1 = Session(application="someapplication1") out = p._get_outbound_message(msg1) p.debug_pub.publish(out) assert isinstance(pub.messages[0], Event) assert pub.messages[0].field_name == "application" assert pub.messages[0].metadata == "someapplication1"
def test_get_slot_value(self): # prepare message for testing, as if it was received (incoming message) in_msg = GenericMessage(type="json", message="{}") p = Processor(watched_topic="/director/scene", msg_slot="message.slug") # test the same on a lower level # empty messages are fine - we just ignore them - dont make them raise exceptions # pytest.raises(EmptyIncomingMessage, p._get_slot_value, in_msg) subslot = json.loads(real_in_msg_director_scene)["message"] in_msg = GenericMessage(type="json", message=json.dumps(subslot)) assert subslot['slug'] == p._get_slot_value(in_msg) in_msg = Session(application="someapplication1") p = Processor(watched_topic="loll", msg_slot="application") assert p._get_slot_value(in_msg) == "someapplication1"
def test_empty_message_not_ignored(self): """ Empty message - consider just empty slot of of the slotted incoming message. Such shall be ignored. """ # prepare message for testing, as if it was received (incoming message) influx = InfluxMock() pub = MockTopicPublisher() in_msg = GenericMessage(type="json", message="{}") p = Processor(watched_topic="/director/scene", influxdb_client=influx, debug_pub=pub, msg_slot="message.slug") p.process(in_msg) # we want to submit empty messages assert p.last_in_msg is not None assert p.time_of_last_in_msg is not None
def test_basic(self): p = Processor(watched_topic='/some/topic', resolution=200, msg_slot='test.me') assert p.resolution == 200