def test_terminate_all_deferred_error_retry(self): service_deferred = defer.Deferred() self.fakecore.deferred = service_deferred client_deferred = self.client.terminate_all(rpcwait=True, poll=0.01, retries=3) yield procutils.asleep(0.1) for i in range(3): self.assertEqual(self.fakecore.terminate_all_count, i+1) self.fakecore.deferred = defer.Deferred() service_deferred.errback(Exception("went bad #%d" % (i+1))) service_deferred = self.fakecore.deferred yield procutils.asleep(0.2) self.assertFalse(client_deferred.called) self.assertEqual(self.fakecore.terminate_all_count, i+2) #this last errback should cause client_deferred to errback itself self.fakecore.deferred = defer.Deferred() service_deferred.errback(Exception("went bad for the last time")) yield procutils.asleep(0.03) try: yield client_deferred except Exception,e: log.exception("Expected error, couldn't terminate all after retries: %s", e)
def test_message_during_init(self): child2 = ProcessDesc(name='echo', module='ion.core.test.test_baseprocess') pid2 = yield self.test_sup.spawn_child(child2, init=False) proc2 = self._get_procinstance(pid2) proc2.plc_init = proc2.plc_noinit self.assertEquals(proc2.proc_state, 'NEW') msgName = self.test_sup.get_scoped_name('global', pu.create_guid()) messaging = {'name_type':'worker', 'args':{'scope':'global'}} yield Container.configure_messaging(msgName, messaging) extraRec = Receiver(proc2.proc_name, msgName) extraRec.handle(proc2.receive) extraid = yield spawn(extraRec) logging.info('Created new receiver %s with pid %s' % (msgName, extraid)) yield self.test_sup.send(pid2, 'init',{},{'quiet':True}) logging.info('Sent init to process 1') yield pu.asleep(0.5) self.assertEquals(proc2.proc_state, 'INIT') (cont,hdrs,msg) = yield self.test_sup.rpc_send(msgName,'echo','content123') self.assertEquals(cont['value'], 'content123') logging.info('Process 1 responsive correctly after init') yield pu.asleep(2) self.assertEquals(proc2.proc_state, 'ACTIVE') (cont,hdrs,msg) = yield self.test_sup.rpc_send(msgName,'echo','content123') self.assertEquals(cont['value'], 'content123') logging.info('Process 1 responsive correctly after init')
def test_reconfigure_recovery(self): # test that engine config is correctly using reconfigured engine values # in subsequent starts of the controller. Strategy is: # 1. Start up a controller with some config # 2. Call reconfigure with some more config # 3. Kill controller and restart it, verify engine got the merged # config. store = ControllerStore() spawnargs = {'engine_class' : 'epu.epucontroller.test.test_controller_core.FakeEngine', 'engine_conf' : {'a' : 'a1', 'b' : 'b1'}, 'servicename' : 'TestEPUController', 'store' : store} controller = EPUControllerService(spawnargs=spawnargs) self.controller = controller yield self._spawn_process(controller) client = EPUControllerClient(targetname='TestEPUController') yield client.attach() yield procutils.asleep(1.01) yield client.reconfigure_rpc({'a' : 'a2', 'c' : 'c1'}) yield procutils.asleep(1.01) controller.terminate() controller = EPUControllerService(spawnargs=spawnargs) self.controller = controller yield self._spawn_process(controller) yield procutils.asleep(1.01) self.assertEqual(controller.core.engine.initialize_conf, {'a' : 'a2', 'b': 'b1', 'c' : 'c1'})
def test_terminate_all_deferred_error_retry(self): service_deferred = defer.Deferred() self.fakecore.deferred = service_deferred client_deferred = self.client.terminate_all(rpcwait=True, poll=0.01, retries=3) yield procutils.asleep(0.1) for i in range(3): self.assertEqual(self.fakecore.terminate_all_count, i + 1) self.fakecore.deferred = defer.Deferred() service_deferred.errback(Exception("went bad #%d" % (i + 1))) service_deferred = self.fakecore.deferred yield procutils.asleep(0.2) self.assertFalse(client_deferred.called) self.assertEqual(self.fakecore.terminate_all_count, i + 2) #this last errback should cause client_deferred to errback itself self.fakecore.deferred = defer.Deferred() service_deferred.errback(Exception("went bad for the last time")) yield procutils.asleep(0.03) try: yield client_deferred except Exception, e: log.exception( "Expected error, couldn't terminate all after retries: %s", e)
def test_message_during_init(self): child2 = ProcessDesc(name='echo', module='ion.core.test.test_baseprocess') pid2 = yield self.test_sup.spawn_child(child2, init=False) proc2 = self._get_procinstance(pid2) proc2.plc_init = proc2.plc_noinit self.assertEquals(proc2.proc_state, 'NEW') msgName = self.test_sup.get_scoped_name('global', pu.create_guid()) messaging = {'name_type': 'worker', 'args': {'scope': 'global'}} yield Container.configure_messaging(msgName, messaging) extraRec = Receiver(proc2.proc_name, msgName) extraRec.handle(proc2.receive) extraid = yield spawn(extraRec) logging.info('Created new receiver %s with pid %s' % (msgName, extraid)) yield self.test_sup.send(pid2, 'init', {}, {'quiet': True}) logging.info('Sent init to process 1') yield pu.asleep(0.5) self.assertEquals(proc2.proc_state, 'INIT') (cont, hdrs, msg) = yield self.test_sup.rpc_send(msgName, 'echo', 'content123') self.assertEquals(cont['value'], 'content123') logging.info('Process 1 responsive correctly after init') yield pu.asleep(2) self.assertEquals(proc2.proc_state, 'ACTIVE') (cont, hdrs, msg) = yield self.test_sup.rpc_send(msgName, 'echo', 'content123') self.assertEquals(cont['value'], 'content123') logging.info('Process 1 responsive correctly after init')
def test_pubsub(self): services = [{"name": "data_pubsub", "module": "ion.services.dm.datapubsub", "class": "DataPubsubService"}] sup = yield self._spawn_processes(services) dpsc = DataPubsubClient(sup) topic_name = yield dpsc.define_topic("topic1") logging.info("Service reply: " + str(topic_name)) dc1 = DataConsumer() dc1_id = yield dc1.spawn() yield dc1.attach(topic_name) dmsg = self._get_datamsg({}, [1, 2, 1, 4, 3, 2]) yield sup.send(topic_name, "data", dmsg) # Need to await the delivery of data messages into the (separate) consumers yield pu.asleep(1) self.assertEqual(dc1.receive_cnt, 1) # Create a second data consumer dc2 = DataConsumer() dc2_id = yield dc2.spawn() yield dc2.attach(topic_name) dmsg = self._get_datamsg({}, [1, 2, 1, 4, 3, 2]) yield sup.send(topic_name, "data", dmsg, {}) # Need to await the delivery of data messages into the (separate) consumers yield pu.asleep(1) self.assertEqual(dc1.receive_cnt, 2) self.assertEqual(dc2.receive_cnt, 1)
def test_queuestat(self): subscriber = TestSubscriber() subId = yield self._spawn_process(subscriber) queuestat_client = QueueStatClient(subscriber) yield queuestat_client.watch_queue(self.queuename, str(subId), 'stat') yield pu.asleep(0.3) assert subscriber.queue_length[self.queuename] == 0 yield self._add_messages(5) yield pu.asleep(0.3) assert subscriber.queue_length[self.queuename] == 5 yield self._add_messages(3) yield pu.asleep(0.3) assert subscriber.queue_length[self.queuename] == 8 yield pu.asleep(0.3) yield queuestat_client.unwatch_queue(self.queuename, str(subId), 'stat') yield self._add_messages(3) yield pu.asleep(0.3) assert subscriber.queue_length[self.queuename] == 8
def test_stream(self): #Set up a logging consumer as a listenr pd1={'name':'consumer_number_1', 'module':'ion.services.dm.distribution.consumers.logging_consumer', 'procclass':'LoggingConsumer', 'spawnargs':{'attach':[self.queue1]}} child1 = base_consumer.ConsumerDesc(**pd1) child1_id = yield self.test_sup.spawn_child(child1) dc1 = self._get_procinstance(child1_id) dsp1={'name':'data_stream_1', 'module':'ion.services.dm.util.data_stream_producer', 'procclass':'DataStreamProducer', 'spawnargs':{'delivery queue':self.queue1, 'delivery interval':5}} child2 = ProcessDesc(**dsp1) child2_id = yield self.test_sup.spawn_child(child2) yield pu.asleep(2) rec = dc1.receive_cnt[self.queue1] self.assertEqual(rec,1) yield pu.asleep(5) rec = dc1.receive_cnt[self.queue1] self.assertEqual(rec,2)
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_chainprocess(self): # This test covers a chain of three data consumer processes on three # topics. One process is an event-detector and data-filter, sending # event messages to an event queue and a new data message to a different # data queue services = [{"name": "data_pubsub", "module": "ion.services.dm.datapubsub", "class": "DataPubsubService"}] sup = yield self._spawn_processes(services) dpsc = DataPubsubClient(sup) topic_raw = yield dpsc.define_topic("topic_raw") topic_qc = yield dpsc.define_topic("topic_qc") topic_evt = yield dpsc.define_topic("topic_qcevent") dc1 = DataConsumer() dc1_id = yield dc1.spawn() yield dc1.attach(topic_raw) dp = DataProcess(proc) dc1.set_ondata(dp.get_ondata()) dc2 = DataConsumer() dc2_id = yield dc2.spawn() yield dc2.attach(topic_qc) dc3 = DataConsumer() dc3_id = yield dc3.spawn() yield dc3.attach(topic_evt) # Create an example data message with time dmsg = self._get_datamsg( {}, [(101, 5), (102, 2), (103, 4), (104, 5), (105, -1), (106, 9), (107, 3), (108, 888), (109, 3), (110, 4)] ) yield sup.send(topic_raw, "data", dmsg, {}) # Need to await the delivery of data messages into the consumers yield pu.asleep(2) self.assertEqual(dc1.receive_cnt, 1) self.assertEqual(dc2.receive_cnt, 1) self.assertEqual(dc3.receive_cnt, 2) dmsg = self._get_datamsg( {}, [(111, 8), (112, 6), (113, 4), (114, -2), (115, -1), (116, 5), (117, 3), (118, 1), (119, 4), (120, 5)] ) yield sup.send(topic_raw, "data", dmsg, {}) # Need to await the delivery of data messages into the consumers yield pu.asleep(2) self.assertEqual(dc1.receive_cnt, 2) self.assertEqual(dc2.receive_cnt, 2) self.assertEqual(dc3.receive_cnt, 4)
def test_attach2_send(self): pd1={'name':'consumer_number_1', 'module':'ion.services.dm.distribution.consumers.forwarding_consumer', 'procclass':'ForwardingConsumer', 'spawnargs':{'attach':[self.queue1, self.queue2],\ 'process parameters':{},\ 'delivery queues':{'queues':[self.queue3]}}\ } child1 = base_consumer.ConsumerDesc(**pd1) child1_id = yield self.test_sup.spawn_child(child1) dc1 = self._get_procinstance(child1_id) self.assertIn(self.queue1, dc1.dataReceivers) self.assertIn(self.queue2, dc1.dataReceivers) dmsg = DataMessageObject() dmsg.notifcation = 'Junk' dmsg.timestamp = pu.currenttime() dmsg = dmsg.encode() # Send to queue1 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.queue3), 1) self.assertEqual(received.get(self.queue1), 1) # Send to queue2 yield self.test_sup.send(self.queue2, '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.queue3), 2) self.assertEqual(received.get(self.queue1), 1) self.assertEqual(received.get(self.queue2), 1) yield child1.shutdown()
def test_attach2_send(self): pd1={'name':'consumer_number_1', 'module':'ion.services.dm.distribution.consumers.forwarding_consumer', 'procclass':'ForwardingConsumer', 'spawnargs':{'attach':[self.queue1, self.queue2],\ 'process parameters':{},\ 'delivery queues':{'queues':[self.queue3]}}\ } child1 = base_consumer.ConsumerDesc(**pd1) child1_id = yield self.test_sup.spawn_child(child1) dc1 = self._get_procinstance(child1_id) self.assertIn(self.queue1,dc1.dataReceivers) self.assertIn(self.queue2,dc1.dataReceivers) dmsg = DataMessageObject() dmsg.notifcation = 'Junk' dmsg.timestamp = pu.currenttime() dmsg = dmsg.encode() # Send to queue1 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.queue3),1) self.assertEqual(received.get(self.queue1),1) # Send to queue2 yield self.test_sup.send(self.queue2, '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.queue3),2) self.assertEqual(received.get(self.queue1),1) self.assertEqual(received.get(self.queue2),1) yield child1.shutdown()
def test_sample(self): result = yield self.driver_client.initialize('some arg') dpsc = DataPubsubClient(self.sup) topicname = 'SBE49 Topic' topic = PubSubTopicResource.create(topicname, "") # Use the service to create a queue and register the topic topic = yield dpsc.define_topic(topic) subscription = SubscriptionResource() subscription.topic1 = PubSubTopicResource.create(topicname, '') subscription.workflow = { 'consumer1': {'module':'ion.services.dm.distribution.consumers.logging_consumer', 'consumerclass':'LoggingConsumer',\ 'attach':'topic1'} } subscription = yield dpsc.define_subscription(subscription) logging.info('Defined subscription: ' + str(subscription)) params = {} params['publish-to'] = topic.RegistryIdentity yield self.driver_client.configure_driver(params) cmd1 = [['ds', 'now']] result = yield self.driver_client.execute(cmd1) self.assertEqual(result['status'], 'OK') yield pu.asleep(1) result = yield self.driver_client.disconnect(['some arg'])
def _start_xmlrpc_daemon(self): """ Starts a dependent XMLRPC server (daemon) on the local host. The daemon serves SNMP and other host data. """ logging.debug('Starting host status daemon') p = subprocess.Popen( [sys.executable, self.HOST_STATUS_DAEMON, 'start'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) # wait 10 seconds for the daemon to power up retries = 20 while p.poll() is None and retries > 0: logging.debug( 'Waiting for server to start. Poll status: %s Retries Left: %s' % (p.poll(), retries)) yield pu.asleep(0.5) retries -= 1 logging.debug('XMLRPC daemon started with return code %s' % str(p.returncode)) if p.returncode != 0: logging.warn("XMLRPC daemon started uncleanly.") out = p.stdout.read() logging.warn("STDOUT: " + str(out)) out = p.stderr.read() logging.warn("STDERR: " + str(out))
def test_fanout(self): messaging = {'fanout1':{'name_type':'fanout', 'args':{'scope':'local'}}} workers = [ {'name':'fanoutProc1','module':'ion.core.worker','class':'WorkerProcess','spawnargs':{'service-name':'fanout1','scope':'local'}}, {'name':'fanoutProc2','module':'ion.core.worker','class':'WorkerProcess','spawnargs':{'service-name':'fanout1','scope':'local'}}, ] yield self._declare_messaging(messaging) yield self._spawn_processes(workers) sup = yield self._get_procid("bootstrap") logging.info("Supervisor: "+repr(sup)) wc = WorkerClient() wcId = yield wc.spawn() wq_name = Container.id + ".fanout1" for i in range(1,6): yield wc.submit_work(wq_name, i, 0.5) yield pu.asleep(5) logging.info("Work results: "+str(wc.workresult)) logging.info("Worker results: "+str(wc.worker)) sum = 0 for w,v in wc.worker.items(): sum += v self.assertEqual(sum, 10)
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 terminate_all(self, rpcwait=False, retries=5, poll=1.0): """Terminate all running nodes and disable provisioner If rpcwait is True, the operation returns a True/False response whether or not all nodes have been terminated yet """ yield self._check_init() if not rpcwait: log.critical('Sending terminate_all request to provisioner') yield self.send('terminate_all', None) else: sent_once = False terminated = False error_count = 0 while not terminated: if not sent_once: log.critical('Sending terminate_all request to provisioner (RPC)') sent_once = True else: yield procutils.asleep(poll) log.critical('Checking on terminate_all request to provisioner') try: terminated, headers, msg = yield self.rpc_send('terminate_all_rpc', None) except (TimeoutError, ReceivedError), e: log.critical("Error from provisioner terminate_all: %s", e, exc_info=True) error_count += 1 if error_count > retries: log.critical("Giving up after %d retries to terminate_all", retries) raise log.critical('All terminated: %s' % terminated)
def test_sample(self): result = yield self.driver_client.initialize('some arg') dpsc = DataPubsubClient(self.sup) topicname = 'SBE49 Topic' topic = PubSubTopicResource.create(topicname,"") # Use the service to create a queue and register the topic topic = yield dpsc.define_topic(topic) subscription = SubscriptionResource() subscription.topic1 = PubSubTopicResource.create(topicname,'') subscription.workflow = { 'consumer1': {'module':'ion.services.dm.distribution.consumers.logging_consumer', 'consumerclass':'LoggingConsumer',\ 'attach':'topic1'} } subscription = yield dpsc.define_subscription(subscription) logging.info('Defined subscription: '+str(subscription)) params = {} params['publish-to'] = topic.RegistryIdentity yield self.driver_client.configure_driver(params) cmd1 = [['ds', 'now']] result = yield self.driver_client.execute(cmd1) self.assertEqual(result['status'], 'OK') yield pu.asleep(1) result = yield self.driver_client.disconnect(['some arg'])
def test_021_acquisition_start_wait_stop(self, close_transaction=True): """ - go active an run - execute START_AUTO_SAMPLING - wait for a few seconds - execute STOP_AUTO_SAMPLING """ self._check_skip() tid = yield self.test_002_go_active_run(False) channel = SiamDriverChannel.INSTRUMENT # Start autosampling # TODO using hard-code "val" which is a channel in the TEstInstrument acq_channel = "val" chans = [acq_channel] cmd = [DriverCommand.START_AUTO_SAMPLING] reply = yield self.ia_client.execute_device(chans, cmd, tid) success = reply['success'] result = reply['result'] # temporary: instead of failing, skip if not InstErrorCode.is_ok(success): raise unittest.SkipTest(str(result)) self.assert_(InstErrorCode.is_ok(success)) print red('=============== autosampling started') # Wait for a few samples to arrive. yield pu.asleep(30) # Stop autosampling. print red('=============== stopping autosampling ') chans = [acq_channel] cmd = [DriverCommand.STOP_AUTO_SAMPLING] #,'GETDATA'] while True: reply = yield self.ia_client.execute_device(chans, cmd, tid) success = reply['success'] result = reply['result'] if InstErrorCode.is_ok(success): break elif InstErrorCode.is_equal(success, InstErrorCode.TIMEOUT): pass else: self.fail('Stop autosample failed with error: ' + str(success)) print red('=============== autosampling result ' + str(result)) self.assert_(InstErrorCode.is_ok(success)) if close_transaction: yield self._close_transaction(tid) defer.returnValue(tid)
def _start_xmlrpc_daemon(self): """ Starts a dependent XMLRPC server (daemon) on the local host. The daemon serves SNMP and other host data. """ logging.debug('Starting host status daemon') p = subprocess.Popen( [sys.executable, self.HOST_STATUS_DAEMON, 'start'], stdout=subprocess.PIPE, stderr=subprocess.PIPE ) # wait 10 seconds for the daemon to power up retries = 20 while p.poll() is None and retries > 0: logging.debug('Waiting for server to start. Poll status: %s Retries Left: %s'%(p.poll(),retries)) yield pu.asleep(0.5) retries -= 1 logging.debug('XMLRPC daemon started with return code %s'%str(p.returncode)) if p.returncode != 0: logging.warn("XMLRPC daemon started uncleanly.") out = p.stdout.read() logging.warn("STDOUT: " + str(out)) out = p.stderr.read() logging.warn("STDERR: " + str(out))
def _work(self,content): myid = self.proc_name + ":" + self.receiver.spawned.id.local workid = str(content['work-id']) waittime = float(content['work']) logging.info("worker="+myid+" job="+workid+" work="+str(waittime)) yield pu.asleep(waittime) logging.info("worker="+myid+" job="+workid+" done at="+str(pu.currenttime_ms()))
def op_getExpected(self, content, headers, msg): """ Returns a list with expected id's that have not been received. If the content includes a 'timeout' parameter, this is used to allow time for expected responses to be received. If not, then the timeout indicated in the last call to op_setExpectedTimeout, if any, will be used. Otherwise, no timeout at all is used. @return: a list with expected id's that have not been received """ log.debug('op_getExpected: ' +str(headers)) timeout = None if 'timeout' in content.keys() and content['timeout']: timeout = content['timeout'] # content in this operation takes precedence else: timeout = self.checkTimeout # use the overall timeout, if any # the total time in seconds we will wait while there is still expected id's remaining = timeout if timeout else 0.0 expected = self._get_still_expected() while len(expected) > 0 and remaining > 0.0: yield pu.asleep(0.2); # sleep for a moment remaining -= 0.2 expected = self._get_still_expected() yield self.reply_ok(msg, expected)
def test_021_acquisition_start_wait_stop(self, close_transaction=True): """ - go active an run - execute START_AUTO_SAMPLING - wait for a few seconds - execute STOP_AUTO_SAMPLING """ self._check_skip() tid = yield self.test_002_go_active_run(False) channel = SiamDriverChannel.INSTRUMENT # Start autosampling # TODO using hard-code "val" which is a channel in the TEstInstrument acq_channel = "val" chans = [acq_channel] cmd = [DriverCommand.START_AUTO_SAMPLING] reply = yield self.ia_client.execute_device(chans,cmd,tid) success = reply['success'] result = reply['result'] # temporary: instead of failing, skip if not InstErrorCode.is_ok(success): raise unittest.SkipTest(str(result)) self.assert_(InstErrorCode.is_ok(success)) print red('=============== autosampling started') # Wait for a few samples to arrive. yield pu.asleep(30) # Stop autosampling. print red('=============== stopping autosampling ') chans = [acq_channel] cmd = [DriverCommand.STOP_AUTO_SAMPLING] #,'GETDATA'] while True: reply = yield self.ia_client.execute_device(chans,cmd,tid) success = reply['success'] result = reply['result'] if InstErrorCode.is_ok(success): break elif InstErrorCode.is_equal(success,InstErrorCode.TIMEOUT): pass else: self.fail('Stop autosample failed with error: '+str(success)) print red('=============== autosampling result ' + str(result)) self.assert_(InstErrorCode.is_ok(success)) if close_transaction: yield self._close_transaction(tid) defer.returnValue(tid)
def _work(self, content): myid = self.proc_name + ":" + self.receiver.spawned.id.local workid = str(content['work-id']) waittime = float(content['work']) logging.info("worker=" + myid + " job=" + workid + " work=" + str(waittime)) yield pu.asleep(waittime) logging.info("worker=" + myid + " job=" + workid + " done at=" + str(pu.currenttime_ms()))
def test_start_stop(self): """ If you want to manually test the proxy, increase this timeout and then telnet to the proxy port and issue strings like GET http://amoeba.ucsd.edu/tmp/test1.txt http/1.0 """ yield pu.asleep(0)
def test_reconfigure_recovery(self): # test that engine config is correctly using reconfigured engine values # in subsequent starts of the controller. Strategy is: # 1. Start up a controller with some config # 2. Call reconfigure with some more config # 3. Kill controller and restart it, verify engine got the merged # config. store = ControllerStore() spawnargs = { 'engine_class': 'epu.epucontroller.test.test_controller_core.FakeEngine', 'engine_conf': { 'a': 'a1', 'b': 'b1' }, 'servicename': 'TestEPUController', 'store': store } controller = EPUControllerService(spawnargs=spawnargs) self.controller = controller yield self._spawn_process(controller) client = EPUControllerClient(targetname='TestEPUController') yield client.attach() yield procutils.asleep(1.01) yield client.reconfigure_rpc({'a': 'a2', 'c': 'c1'}) yield procutils.asleep(1.01) controller.terminate() controller = EPUControllerService(spawnargs=spawnargs) self.controller = controller yield self._spawn_process(controller) yield procutils.asleep(1.01) self.assertEqual(controller.core.engine.initialize_conf, { 'a': 'a2', 'b': 'b1', 'c': 'c1' })
def test_mockloopprovisioner(self): messaging = {'cei':{'name_type':'worker', 'args':{'scope':'local'}}} procs = [ {'name':'sensor_aggregator','module':'ion.services.cei.sensor_aggregator','class':'SensorAggregatorService'}, {'name':'epu_controller','module':'ion.services.cei.epu_controller','class':'EPUControllerService'}, {'name':'provisioner','module':'ion.services.cei.test.mockloop.provisioner','class':'MockLoopProvisionerService'} ] yield self._declare_messaging(messaging) supervisor = yield self._spawn_processes(procs) aggregatorId = yield self.procRegistry.get("sensor_aggregator") controllerId = yield self.procRegistry.get("epu_controller") provisionerId = yield self.procRegistry.get("provisioner") logging.info("aggregatorId: "+repr(aggregatorId) + ", " + "provisionerId: "+repr(provisionerId) + ", " + "controllerId: "+repr(controllerId)) testmsg = { 'operation':'start', 'deployable_type':'extraction-service-dt-5124', 'launch_id':'fa6baaaf-b4a3-4969-b10f-99f87b3117cd', 'instances' : { 'head-node' : { 'id':['e61f0c0e-781e-4681-adb2-7dada1cf31a4'], 'allocation' : 'x-large', 'site':'ec2-west', 'data' : {} }, 'worker-node' : { 'id': ['5fee8009-67db-4efc-8f35-4cee5d65c917', '228a0ea6-e1eb-4634-b57f-1f4162a89f63'], 'allocation' : 'small', 'site':'ec2-west', 'data' : {} }, }, 'subscribers' : ['my_sensor_aggregator'] } yield pu.asleep(5) yield supervisor.send(provisionerId, "provision", testmsg) yield pu.asleep(1000000) # "forever"
def test_stream(self): #Set up a logging consumer as a listenr pd1 = { 'name': 'consumer_number_1', 'module': 'ion.services.dm.distribution.consumers.logging_consumer', 'procclass': 'LoggingConsumer', 'spawnargs': { 'attach': [self.queue1] } } child1 = base_consumer.ConsumerDesc(**pd1) child1_id = yield self.test_sup.spawn_child(child1) dc1 = self._get_procinstance(child1_id) dsp1 = { 'name': 'data_stream_1', 'module': 'ion.services.dm.util.data_stream_producer', 'procclass': 'DataStreamProducer', 'spawnargs': { 'delivery queue': self.queue1, 'delivery interval': 5 } } child2 = ProcessDesc(**dsp1) child2_id = yield self.test_sup.spawn_child(child2) yield pu.asleep(2) rec = dc1.receive_cnt[self.queue1] self.assertEqual(rec, 1) yield pu.asleep(5) rec = dc1.receive_cnt[self.queue1] self.assertEqual(rec, 2)
def op_work(self, content, headers, msg): if not self.laterinitialized: log.error("message got here without the later-init") sleepsecs = int(content['work_amount']) extradict = {"batchid":content['batchid'], "jobid":content['jobid'], "work_amount":sleepsecs} cei_events.event("worker", "job_begin", extra=extradict) log.info("WORK: sleeping for %d seconds ---" % sleepsecs) yield pu.asleep(sleepsecs) yield self.reply(msg, 'result', {'result':'work_complete'}, {}) cei_events.event("worker", "job_end", extra=extradict)
def test_fanout(self): messaging = { 'fanout1': { 'name_type': 'fanout', 'args': { 'scope': 'local' } } } workers = [ { 'name': 'fanoutProc1', 'module': 'ion.core.worker', 'class': 'WorkerProcess', 'spawnargs': { 'service-name': 'fanout1', 'scope': 'local' } }, { 'name': 'fanoutProc2', 'module': 'ion.core.worker', 'class': 'WorkerProcess', 'spawnargs': { 'service-name': 'fanout1', 'scope': 'local' } }, ] yield self._declare_messaging(messaging) yield self._spawn_processes(workers) sup = yield self._get_procid("bootstrap") logging.info("Supervisor: " + repr(sup)) wc = WorkerClient() wcId = yield wc.spawn() wq_name = Container.id + ".fanout1" for i in range(1, 6): yield wc.submit_work(wq_name, i, 0.5) yield pu.asleep(5) logging.info("Work results: " + str(wc.workresult)) logging.info("Worker results: " + str(wc.worker)) sum = 0 for w, v in wc.worker.items(): sum += v self.assertEqual(sum, 10)
def test_basic_init(self): spawnargs = {"queue_name_work" : "testqueuename"} controller = EPUControllerService(spawnargs=spawnargs) self.controller = controller controller_id = yield self._spawn_process(controller) self.assertIn("testqueuename", controller.queue_name_work) # testing workaround for race between slc_init and queue binding. # this is hopefully short term and the workaround can be removed # after the bug is fixed in ioncore. self.assertTrue(controller.core.control_loop is None) yield procutils.asleep(1.1) self.assertTrue(controller.core.control_loop is not None)
def tearDown(self): logging.info('Tearing Down PubSub Test') # Clear the registry on the way out! child_id = yield self.sup.get_child_id('pubsub_service') logging.debug('PubSub Test Service ID:' + str(child_id)) # This is only allowed in a test case - that way we can directly use the service methods! pubsub = self._get_procinstance(child_id) pubsub.reg.clear_registry() yield pu.asleep(1) yield self._stop_container()
def wait_for_state(self, state, nodes=None, poll=0.1, before=None, before_kwargs={}): win = None while not win: if before: yield before(**before_kwargs) elif poll: yield pu.asleep(poll) win = self.assure_state(state, nodes) log.debug('All nodes in %s state', state) defer.returnValue(win)
def test_basic_init(self): spawnargs = {"queue_name_work": "testqueuename"} controller = EPUControllerService(spawnargs=spawnargs) self.controller = controller controller_id = yield self._spawn_process(controller) self.assertIn("testqueuename", controller.queue_name_work) # testing workaround for race between slc_init and queue binding. # this is hopefully short term and the workaround can be removed # after the bug is fixed in ioncore. self.assertTrue(controller.core.control_loop is None) yield procutils.asleep(1.1) self.assertTrue(controller.core.control_loop is not None)
def test_persister_consumer_dap(self): msg = dap_tools.ds2dap_msg(self.ds) yield self.test_sup.send(self.queue1, 'data', msg.encode()) yield pu.asleep(1) msg_cnt = yield self.child1.get_msg_count() received = msg_cnt.get('received', {}) sent = msg_cnt.get('sent', {}) self.assertEqual(sent, {}) self.assertEqual(received.get(self.queue1), 1) yield self.test_sup.send(self.queue1, 'data', msg.encode()) yield pu.asleep(1) msg_cnt = yield self.child1.get_msg_count() received = msg_cnt.get('received', {}) sent = msg_cnt.get('sent', {}) self.assertEqual(sent, {}) self.assertEqual(received.get(self.queue1), 2)
def op_work(self, content, headers, msg): if not self.laterinitialized: log.error("message got here without the later-init") sleepsecs = int(content['work_amount']) extradict = { "batchid": content['batchid'], "jobid": content['jobid'], "work_amount": sleepsecs } cei_events.event("worker", "job_begin", extra=extradict) log.info("WORK: sleeping for %d seconds ---" % sleepsecs) yield pu.asleep(sleepsecs) yield self.reply(msg, 'result', {'result': 'work_complete'}, {}) cei_events.event("worker", "job_end", extra=extradict)
def test_persister_consumer_dap(self): msg=dap_tools.ds2dap_msg(self.ds) yield self.test_sup.send(self.queue1,'data',msg.encode()) yield pu.asleep(1) msg_cnt = yield self.child1.get_msg_count() received = msg_cnt.get('received',{}) sent = msg_cnt.get('sent',{}) self.assertEqual(sent,{}) self.assertEqual(received.get(self.queue1),1) yield self.test_sup.send(self.queue1,'data',msg.encode()) yield pu.asleep(1) msg_cnt = yield self.child1.get_msg_count() received = msg_cnt.get('received',{}) sent = msg_cnt.get('sent',{}) self.assertEqual(sent,{}) self.assertEqual(received.get(self.queue1),2)
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_terminate_all_deferred(self): """Check the specific behavior with terminate_all_deferred. """ service_deferred = defer.Deferred() self.fakecore.deferred = service_deferred client_deferred = self.client.terminate_all(rpcwait=True, poll=0.1) yield procutils.asleep(0.3) # first time the core fires its Deferred, check_terminate_all still # says there are instances. So client should not yet return self.fakecore.all_terminated = False self.fakecore.deferred = defer.Deferred() # set up the next Deferred service_deferred.callback(None) service_deferred = self.fakecore.deferred yield procutils.asleep(0.3) self.assertFalse(client_deferred.called) self.assertEqual(self.fakecore.check_terminate_all_count, 1) # now we flip terminate_all_check to True. client should return # on next cycle self.fakecore.all_terminated = True service_deferred.callback(None) yield client_deferred
def _stop_xmlrpc_daemon(self): """ Stops the XMLRPC server (daemon) on the local host. """ p = subprocess.Popen( [sys.executable, self.HOST_STATUS_DAEMON, 'stop'], stdout=subprocess.PIPE, stderr=subprocess.PIPE ) # wait 10 seconds for the daemon to shutdown retries = 20 while p.poll() is None and retries > 0: logging.debug('Waiting for server to stop. Poll status: %s Retries Left: %s'%(p.poll(),retries)) yield pu.asleep(0.5) retries -= 1 logging.debug('XMLRPC daemon stopped with return code %s'%str(p.returncode)) if p.returncode != 0: logging.debug('XMLRPC daemon stopped uncleanly.')
def test_provisioner_to_controller(self): messaging = {'cei':{'name_type':'worker', 'args':{'scope':'local'}}} procs = [ {'name':'sensor_aggregator','module':'ion.services.cei.sensor_aggregator','class':'SensorAggregatorService'}, {'name':'provisioner','module':'ion.services.cei.provisioner','class':'ProvisionerService'} ] yield self._declare_messaging(messaging) supervisor = yield self._spawn_processes(procs) saId = yield self.procRegistry.get("sensor_aggregator") pId = yield self.procRegistry.get("provisioner") logging.info("saId: "+repr(saId) + " " + "pId: "+repr(pId)) # The below should "kick off" more complex test-able behavior: yield supervisor.send(pId, "provision", {"action":"start-sending-iaas-messages"}) #TODO: add a Counter to Prov, and 'test' counter. (this is a testing hack) yield pu.asleep(5) #async wait
def test_get_set_SBE49_params(self): """ Test the ability of the SBE49 driver to send and receive get, set, and other messages. Best called as RPC message pairs. """ self.simulator = Simulator("123", 9000) self.simulator.start() # Sleep for a while to allow simlator to get set up. yield pu.asleep(1) try: response = yield self.IAClient.get_from_instrument(['baudrate', 'outputformat']) self.assert_(response['status'] == 'OK') self.assertEqual(response['baudrate'], 9600) self.assertEqual(response['outputformat'], 0) response = yield self.IAClient.set_to_instrument({'baudrate': 19200, 'outputformat': 1}) self.assert_(response['status'] == 'OK') self.assertEqual(response['baudrate'], 19200) self.assertEqual(response['outputformat'], 1) response = yield self.IAClient.get_from_instrument(['baudrate', 'outputformat']) self.assert_(response['status'] == 'OK') self.assertEqual(response['baudrate'], 19200) self.assertEqual(response['outputformat'], 1) response = yield self.IAClient.set_to_instrument({'outputformat': 2}) self.assert_(response['status'] == 'OK') self.assertEqual(response['outputformat'], 2) # Try setting something bad response = yield self.IAClient.set_to_instrument({'baudrate': 19200, 'badvalue': 1}) self.assert_(response['status'] == 'ERROR') self.assert_('baudrate' not in response) finally: yield self.simulator.stop()
def _stop_xmlrpc_daemon(self): """ Stops the XMLRPC server (daemon) on the local host. """ p = subprocess.Popen([sys.executable, self.HOST_STATUS_DAEMON, 'stop'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) # wait 10 seconds for the daemon to shutdown retries = 20 while p.poll() is None and retries > 0: logging.debug( 'Waiting for server to stop. Poll status: %s Retries Left: %s' % (p.poll(), retries)) yield pu.asleep(0.5) retries -= 1 logging.debug('XMLRPC daemon stopped with return code %s' % str(p.returncode)) if p.returncode != 0: logging.debug('XMLRPC daemon stopped uncleanly.')
def test_digest_latest(self): pd1={'name':'consumer_number_1', 'module':'ion.services.dm.distribution.consumers.latest_consumer', 'procclass':'LatestConsumer', 'spawnargs':{'attach':self.queue1, 'process parameters':{}, 'delivery queues':{'queues':[self.queue2]}, 'delivery interval':2} } 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 self.test_sup.send(self.queue1, 'data', dmsg) yield self.test_sup.send(self.queue1, 'data', dmsg) yield self.test_sup.send(self.queue1, 'data', dmsg) yield pu.asleep(2) 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),4)
def test_acquisition_start_wait_stop(self): """ - start acquisition by calling test_acquisition_start_verify_data - wait for a few seconds - stop acquisition """ self._check_skip() receiver_service_name='test_acquisition_start_wait_stop' # start acquisition (receiver_client, channel, publish_id) = \ yield self.test_acquisition_start_verify_data(receiver_service_name) # wait for a few samples to be notified to the receiver service yield pu.asleep(20) # stop acquisition ret = yield self.siamci.execute_StopAcquisition(channel=channel, publish_stream=sysname + "." + receiver_service_name) self.assertIsSuccessFail(ret) self.assertEquals(ret.result, OK)
def test_digest_latest(self): pd1 = { 'name': 'consumer_number_1', 'module': 'ion.services.dm.distribution.consumers.latest_consumer', 'procclass': 'LatestConsumer', 'spawnargs': { 'attach': self.queue1, 'process parameters': {}, 'delivery queues': { 'queues': [self.queue2] }, 'delivery interval': 2 } } 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 self.test_sup.send(self.queue1, 'data', dmsg) yield self.test_sup.send(self.queue1, 'data', dmsg) yield self.test_sup.send(self.queue1, 'data', dmsg) yield pu.asleep(2) 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), 4)
def test_acquisition_start_wait_stop(self): """ - start acquisition by calling test_acquisition_start_verify_data - wait for a few seconds - stop acquisition """ self._check_skip() receiver_service_name = 'test_acquisition_start_wait_stop' # start acquisition (receiver_client, channel, publish_id) = \ yield self.test_acquisition_start_verify_data(receiver_service_name) # wait for a few samples to be notified to the receiver service yield pu.asleep(20) # stop acquisition ret = yield self.siamci.execute_StopAcquisition( channel=channel, publish_stream=sysname + "." + receiver_service_name) self.assertIsSuccessFail(ret) self.assertEquals(ret.result, OK)
def terminate_all(self, rpcwait=False, retries=5, poll=1.0): """Terminate all running nodes and disable provisioner If rpcwait is True, the operation returns a True/False response whether or not all nodes have been terminated yet """ yield self._check_init() if not rpcwait: log.critical('Sending terminate_all request to provisioner') yield self.send('terminate_all', None) else: sent_once = False terminated = False error_count = 0 while not terminated: if not sent_once: log.critical( 'Sending terminate_all request to provisioner (RPC)') sent_once = True else: yield procutils.asleep(poll) log.critical( 'Checking on terminate_all request to provisioner') try: terminated, headers, msg = yield self.rpc_send( 'terminate_all_rpc', None) except (TimeoutError, ReceivedError), e: log.critical("Error from provisioner terminate_all: %s", e, exc_info=True) error_count += 1 if error_count > retries: log.critical( "Giving up after %d retries to terminate_all", retries) raise log.critical('All terminated: %s' % terminated)
def test_execute(self): """ Test the execute command to the Instrument Driver """ result = yield self.driver_client.initialize('some arg') dpsc = DataPubsubClient(self.sup) subscription = SubscriptionResource() subscription.topic1 = PubSubTopicResource.create('SBE49 Topic', '') #subscription.topic2 = PubSubTopicResource.create('','oceans') subscription.workflow = { 'consumer1': {'module':'ion.services.dm.distribution.consumers.logging_consumer', 'consumerclass':'LoggingConsumer',\ 'attach':'topic1'} } subscription = yield dpsc.define_subscription(subscription) logging.info('Defined subscription: ' + str(subscription)) cmd1 = [['ds', 'now']] #cmd1 = [['start', 'now']] #cmd2 = [['stop', 'now']] #cmd2 = [['pumpoff', '3600', '1']] result = yield self.driver_client.execute(cmd1) self.assertEqual(result['status'], 'OK') # DHE: wait a while... yield pu.asleep(1) #result = yield self.driver_client.execute(cmd2) #self.assertEqual(result['status'], 'OK') # DHE: disconnecting; a connect would probably be good. result = yield self.driver_client.disconnect(['some arg'])
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 xtest_BasicService(self): retries = 100 while retries > 0: retries -= 1 print 'Waiting for reply...' yield pu.asleep(0.5)