def test_heartbeat_service_bails_when_no_ipc(self): address = "ipc:///tmp/nmos-nodefacade.dummy.for.test" srv_type = "dummy_type" UUT = Facade(srv_type, address=address) self.mocks['nmosnode.facade.Proxy'].side_effect = Exception with mock.patch.object(UUT, 'reregister_all') as reregister_all: UUT.heartbeat_service() self.assertFalse(UUT.srv_registered) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_heartbeat.assert_not_called( ) reregister_all.assert_not_called()
def test_heartbeat_service_reregisters_on_failure(self): address = "ipc:///tmp/nmos-nodefacade.dummy.for.test" srv_type = "dummy_type" UUT = Facade(srv_type, address=address) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_heartbeat.return_value = FAC_OTHERERROR with mock.patch.object(UUT, 'reregister_all') as reregister_all: UUT.heartbeat_service() self.assertFalse(UUT.srv_registered) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_heartbeat.assert_called_once_with( srv_type, mock.ANY) reregister_all.assert_called_once_with()
def test_heartbeat_service_reregisters_on_success_when_told_to(self): """Is this correct? This is current behaviour, but doesn't match the comments""" address = "ipc:///tmp/nmos-nodefacade.dummy.for.test" srv_type = "dummy_type" UUT = Facade(srv_type, address=address) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_heartbeat.return_value = FAC_SUCCESS with mock.patch.object(UUT, 'reregister_all') as reregister_all: UUT.reregister = True UUT.heartbeat_service() self.assertTrue(UUT.srv_registered) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_heartbeat.assert_called_once_with( srv_type, mock.ANY) reregister_all.assert_called_once_with()
class ConnectionManagementService: def __init__(self, logger=None): self.running = False from nmoscommon.logger import Logger from nmosnode.facade import Facade self.logger = Logger("conmanage") self.logger.writeWarning("Could not find ipppython facade") self.facade = Facade("{}/{}".format(CONN_APINAME, CONN_APIVERSIONS[-1]), address="ipc:///tmp/ips-nodefacade", logger=self.logger) self.logger.writeDebug("Running Connection Management Service") self.httpServer = HttpServer(ConnectionManagementAPI, WS_PORT, '0.0.0.0', api_args=[self.logger]) def start(self): '''Call this to run the API without blocking''' if self.running: gevent.signal_handler(signal.SIGINT, self.sig_handler) gevent.signal_handler(signal.SIGTERM, self.sig_handler) self.running = True self.httpServer.start() while not self.httpServer.started.is_set(): self.logger.writeDebug('Waiting for httpserver to start...') self.httpServer.started.wait() if self.httpServer.failed is not None: raise self.httpServer.failed self.logger.writeDebug("Running on port: {}" .format(self.httpServer.port)) self.facade.register_service("http://127.0.0.1:{}".format(self.httpServer.port), "{}{}/".format(CONN_ROOT[1:], CONN_APIVERSIONS[-1])) try: from nmosconnectiondriver.httpIpstudioDriver import httpIpstudioDriver self.logger.writeInfo("Using ipstudio driver") # Start the IPStudio driver self.driver = httpIpstudioDriver( self.httpServer.api, self.logger, self.facade ) except ImportError: # Start the mock driver self.driver = NmosDriver( self.httpServer.api, self.logger, self.facade ) def run(self): '''Call this to run the API in keep-alive (blocking) mode''' self.running = True self.start() itercount = 0 while self.running: gevent.sleep(1) itercount += 1 if itercount == 5: self.facade.heartbeat_service() itercount = 0 self._cleanup() def _cleanup(self): self.httpServer.stop() self.facade.unregister_service() def sig_handler(self): self.stop() def stop(self): '''Gracefully shut down the API''' self._cleanup() self.running = False