def test_send(self): pd1={'name':'consumer_number_1', 'module':'ion.services.dm.distribution.consumers.forwarding_consumer', 'procclass':'ForwardingConsumer', 'spawnargs':{'attach':self.queue1,\ 'process parameters':{}, 'delivery queues':{'queues':[self.queue2]}}\ } child1 = base_consumer.ConsumerDesc(**pd1) child1_id = yield self.test_sup.spawn_child(child1) dmsg = DataMessageObject() dmsg.notifcation = 'Junk' dmsg.timestamp = pu.currenttime() dmsg = dmsg.encode() yield self.test_sup.send(self.queue1, 'data', dmsg) yield pu.asleep(1) msg_cnt = yield child1.get_msg_count() received = msg_cnt.get('received', {}) sent = msg_cnt.get('sent', {}) self.assertEqual(sent.get(self.queue2), 1) self.assertEqual(received.get(self.queue1), 1)
def test_create_topic(self): # Create and Register a topic topic = PubSubTopicResource.create('Davids Topic',"oceans, oil spill, fun things to do") # Make sure the topic starts out with blank stuff here... self.assertEqual(topic.RegistryBranch,'master') self.assertEqual(topic.RegistryIdentity,'') self.assertEqual(topic.queue.type,'') # Use the service to create a queue and register the topic topic = yield self.pubsub.create_and_register_topic(topic) # Make sure the queue properties were set. self.assertEqual(topic.queue.type,'fanout') #Spawn an baseconsumer and make sure a message is received on the new queue pd1={'name':'consumer_number_1', 'module':'ion.services.dm.distribution.consumers.logging_consumer', 'procclass':'LoggingConsumer', 'spawnargs':{'attach':[topic.queue.name]}} child1 = base_consumer.ConsumerDesc(**pd1) child1_id = yield self.test_sup.spawn_child(child1) msg=DataMessageObject() self.sup.send(topic.queue.name,'data',msg.encode()) yield pu.asleep(1) dc1 = self._get_procinstance(child1_id) #print 'DC1',dc1.receive_cnt rec = dc1.receive_cnt[topic.queue.name] self.assertEqual(rec,1)
def test_send(self): pd1={'name':'consumer_number_1', 'module':'ion.services.dm.distribution.consumers.forwarding_consumer', 'procclass':'ForwardingConsumer', 'spawnargs':{'attach':self.queue1,\ 'process parameters':{}, 'delivery queues':{'queues':[self.queue2]}}\ } child1 = base_consumer.ConsumerDesc(**pd1) child1_id = yield self.test_sup.spawn_child(child1) dmsg = DataMessageObject() dmsg.notifcation = 'Junk' dmsg.timestamp = pu.currenttime() dmsg = dmsg.encode() yield self.test_sup.send(self.queue1, 'data', dmsg) yield pu.asleep(1) msg_cnt = yield child1.get_msg_count() received = msg_cnt.get('received',{}) sent = msg_cnt.get('sent',{}) self.assertEqual(sent.get(self.queue2),1) self.assertEqual(received.get(self.queue1),1)
def test_send_chain(self): pd1={'name':'consumer_number_1', 'module':'ion.services.dm.distribution.consumers.forwarding_consumer', 'procclass':'ForwardingConsumer', 'spawnargs':{'attach':self.queue1,\ 'process parameters':{},\ 'delivery queues':{'queues':[self.queue2]}}\ } child1 = base_consumer.ConsumerDesc(**pd1) child1_id = yield self.test_sup.spawn_child(child1) dmsg = DataMessageObject() dmsg.notifcation = 'Junk' dmsg.timestamp = pu.currenttime() dmsg = dmsg.encode() yield self.test_sup.send(self.queue1, 'data', dmsg) yield pu.asleep(1) msg_cnt = yield child1.get_msg_count() received = msg_cnt.get('received', {}) sent = msg_cnt.get('sent', {}) self.assertEqual(sent.get(self.queue2), 1) self.assertEqual(received.get(self.queue1), 1) #Spawn another process to listen to queue 2 pd2={'name':'consumer_number_2', \ 'module':'ion.services.dm.distribution.consumers.forwarding_consumer', 'procclass':'ForwardingConsumer',\ 'spawnargs':{'attach':self.queue2}} child2 = base_consumer.ConsumerDesc(**pd2) child2_id = yield self.test_sup.spawn_child(child2) # Tell the first consumer to pass results to the second! #res = yield child1.set_params({'queues':[self.queue2]}) yield self.test_sup.send(self.queue1, 'data', dmsg) yield pu.asleep(1) msg_cnt = yield child1.get_msg_count() received = msg_cnt.get('received', {}) sent = msg_cnt.get('sent', {}) self.assertEqual(sent.get(self.queue2), 2) self.assertEqual(received.get(self.queue1), 2) msg_cnt = yield child2.get_msg_count() received = msg_cnt.get('received', {}) sent = msg_cnt.get('sent') self.assertEqual(sent, {}) self.assertEqual(received.get(self.queue2), 1) yield child1.shutdown() yield child2.shutdown()
def test_create_subscription2(self): ''' @Brief Create a subscription! ''' # Create and Register a topic topic1 = PubSubTopicResource.create('Davids Topic',"oceans, oil spill, fun things to do") # Use the service to create a queue and register the topic topic1 = yield self.pubsub.create_and_register_topic(topic1) topic2 = PubSubTopicResource.create('Johns Topic',"oceans, mbari, working really hard") # Use the service to create a queue and register the topic topic2 = yield self.pubsub.create_and_register_topic(topic2) subscription = SubscriptionResource() subscription.topic1 = PubSubTopicResource.create('Davids Topic','') subscription.topic2 = PubSubTopicResource.create('','oceans') subscription.workflow = { 'consumer1': {'module':'ion.services.dm.distribution.consumers.forwarding_consumer', 'consumerclass':'ForwardingConsumer',\ 'attach':'topic1'}, 'consumer2': {'module':'ion.services.dm.distribution.consumers.forwarding_consumer', 'consumerclass':'ForwardingConsumer',\ 'attach':[['consumer1','queues']]} } subscription = yield self.pubsub.create_subscription(subscription) logging.info('Defined subscription: '+str(subscription)) msg=DataMessageObject() self.sup.send(topic1.queue.name,'data',msg.encode()) # Wait for message to be received yield pu.asleep(1) child1_id = self.pubsub.get_child_id('consumer1') dc1 = self._get_procinstance(child1_id) rec = dc1.receive_cnt[topic1.queue.name] self.assertEqual(rec,1) child2_id = self.pubsub.get_child_id('consumer2') dc2 = self._get_procinstance(child2_id) q = subscription.consumer_args['consumer2']['spawnargs']['attach'] rec = dc2.receive_cnt[q[0]] self.assertEqual(rec,1)
def test_create_subscription1(self): ''' @Brief Create a subscription! ''' # Create and Register a topic topic = PubSubTopicResource.create('Davids Topic',"oceans, oil spill, fun things to do") # Use the service to create a queue and register the topic topic = yield self.pubsub.create_and_register_topic(topic) # A condition to search for the topic t_search = PubSubTopicResource.create('Davids Topic','') subscription = SubscriptionResource() subscription.topic1 = t_search subscription.workflow = { 'consumer1': {'module':'ion.services.dm.distribution.consumers.forwarding_consumer', 'consumerclass':'ForwardingConsumer',\ 'attach':'topic1'}} subscription = yield self.pubsub.create_subscription(subscription) logging.info('Defined subscription: '+str(subscription)) msg=DataMessageObject() self.sup.send(topic.queue.name,'data',msg.encode()) # Wait for message to be received yield pu.asleep(1) child_id = self.pubsub.get_child_id('consumer1') dc1 = self._get_procinstance(child_id) rec = dc1.receive_cnt[topic.queue.name] self.assertEqual(rec,1)
def queue_result(self,queue, data=None, notification=''): if isinstance(data, DatasetType): msg = dap_tools.ds2dap_msg(data) elif isinstance(data, str): msg = StringMessageObject() msg.data=data elif isinstance(data, dict): msg = DictionaryMessageObject() msg.data=data elif not data: msg = DataMessageObject() else: raise RuntimeError('Invalid data type passed to send_result in class %s: type:' % (self.__class__.__name__, type(data))) if not notification: notification = self.__class__.__name__ + ' received a message!' msg.notification = notification msg.timestamp = pu.currenttime() self.msgs_to_send.append((queue, msg.encode()))
def test_send_chain(self): pd1={'name':'consumer_number_1', 'module':'ion.services.dm.distribution.consumers.forwarding_consumer', 'procclass':'ForwardingConsumer', 'spawnargs':{'attach':self.queue1,\ 'process parameters':{},\ 'delivery queues':{'queues':[self.queue2]}}\ } child1 = base_consumer.ConsumerDesc(**pd1) child1_id = yield self.test_sup.spawn_child(child1) dmsg = DataMessageObject() dmsg.notifcation = 'Junk' dmsg.timestamp = pu.currenttime() dmsg = dmsg.encode() yield self.test_sup.send(self.queue1, 'data', dmsg) yield pu.asleep(1) msg_cnt = yield child1.get_msg_count() received = msg_cnt.get('received',{}) sent = msg_cnt.get('sent',{}) self.assertEqual(sent.get(self.queue2),1) self.assertEqual(received.get(self.queue1),1) #Spawn another process to listen to queue 2 pd2={'name':'consumer_number_2', \ 'module':'ion.services.dm.distribution.consumers.forwarding_consumer', 'procclass':'ForwardingConsumer',\ 'spawnargs':{'attach':self.queue2}} child2 = base_consumer.ConsumerDesc(**pd2) child2_id = yield self.test_sup.spawn_child(child2) # Tell the first consumer to pass results to the second! #res = yield child1.set_params({'queues':[self.queue2]}) yield self.test_sup.send(self.queue1, 'data', dmsg) yield pu.asleep(1) msg_cnt = yield child1.get_msg_count() received = msg_cnt.get('received',{}) sent = msg_cnt.get('sent',{}) self.assertEqual(sent.get(self.queue2),2) self.assertEqual(received.get(self.queue1),2) msg_cnt = yield child2.get_msg_count() received = msg_cnt.get('received',{}) sent = msg_cnt.get('sent') self.assertEqual(sent,{}) self.assertEqual(received.get(self.queue2),1) yield child1.shutdown() yield child2.shutdown()