def _make_service(self): """ Test helper to make a passable service. """ svc = LocalContextMixin() svc.id = "test_id" svc.name = "test_svc" svc.container = Mock() svc.container.context = LocalContextMixin() return svc
def __init__(self, *args, **kwargs): BaseContainerAgent.__init__(self, *args, **kwargs) # Coordinates the container start self._status = INIT self._is_started = False # set container id and cc_agent name (as they are set in base class call) self.id = get_default_container_id() self.name = "cc_agent_%s" % self.id self.start_time = get_ion_ts() bootstrap.container_instance = self Container.instance = self self.container = self # Make self appear as process to service clients self.CCAP = CCAP self.CFG = CFG log.debug("Container (sysname=%s) initializing ..." % bootstrap.get_sys_name()) # Keep track of the overrides from the command-line, so they can trump app/rel file data self.spawn_args = kwargs # Greenlet context-local storage self.context = LocalContextMixin() # Load general capabilities file and augment with specific profile self._load_capabilities() # Start the capabilities start_order = self.cap_profile['start_order'] for cap in start_order: if cap not in self._cap_definitions: raise ContainerError( "CC capability %s not defined in profile" % cap) if cap in self._capabilities or cap in self._cap_instances: raise ContainerError("CC capability %s already initialized" % cap) try: cap_def = self._cap_definitions[cap] log.debug("__init__(): Initializing '%s'" % cap) cap_obj = named_any(cap_def['class'])(container=self) self._cap_instances[cap] = cap_obj if 'depends_on' in cap_def and cap_def['depends_on']: dep_list = cap_def['depends_on'].split(',') for dep in dep_list: dep = dep.strip() if dep not in self._cap_initialized: raise ContainerError( "CC capability %s dependent on non-existing capability %s" % (cap, dep)) if 'field' in cap_def and cap_def['field']: setattr(self, cap_def['field'], cap_obj) self._cap_initialized.append(cap) except Exception as ex: log.error("Container Capability %s init error: %s" % (cap, ex)) raise log.debug("Container initialized, OK.")
class LocalContextMixinTest(IonIntegrationTestCase): """ Tests LocalContextMixin for thread-level storage """ def setUp(self): self.lcm = LocalContextMixin() # this is not common use but legal def test_set_context(self): old = self.lcm.set_context('new_context') self.assertTrue(old is None) self.assertTrue(hasattr(self.lcm._lcm_context, 'ctx')) self.assertEquals(self.lcm._lcm_context.ctx, 'new_context') def test_get_context(self): obj = {'one':1, 'two':2} self.lcm.set_context(obj) newobj = self.lcm.get_context() self.assertEqual(newobj, obj) self.assertEqual(id(newobj), id(obj)) def test_push_context(self): with self.lcm.push_context('thecontext'): self.assertEqual(self.lcm._lcm_context.ctx, 'thecontext') self.assertTrue(self.lcm._lcm_context.ctx is None) with self.lcm.push_context('thecontext2') as f: self.assertEqual(f, 'thecontext2') def test_separate_greenlets(self): ev1 = event.Event() ev2 = event.Event() def gl_one(): self.lcm.set_context('from event one') ev1.set() def gl_two(): ev1.wait() # wait for one to do its thing curctx = self.lcm.get_context() self.assertNotEqual(curctx, 'from event one') self.assertTrue(curctx is None) ev2.set() spawn(gl_one) spawn(gl_two) ev2.wait(timeout=10)
def test__routing_call(self): svc = LocalContextMixin() p = IonProcessThread(name=sentinel.name, listeners=[], service=svc) p.start() p.get_ready_event().wait(timeout=5) ar = AsyncResult() p._routing_call(ar.set, None, value=sentinel.callarg) v = ar.get(timeout=5) self.assertEquals(v, sentinel.callarg) p._notify_stop() p.stop()
def test_competing__routing_call(self): svc = LocalContextMixin() p = IonProcessThread(name=sentinel.name, listeners=[], service=svc) p.start() p.get_ready_event().wait(timeout=5) sem = Semaphore() # define a callable method that tries to grab a shared semaphore def thecall(ar=None): semres = sem.acquire(blocking=False) if not semres: raise StandardError( "Could not get semaphore, routing_call/control flow is broken!" ) # make this take a sec time.sleep(1) # make sure we release sem.release() # set the ar ar.set(True) # schedule some calls (in whatever order) ar1 = AsyncResult() ar2 = AsyncResult() ar3 = AsyncResult() p._routing_call(thecall, None, ar=ar3) p._routing_call(thecall, None, ar=ar1) p._routing_call(thecall, None, ar=ar2) # wait on all the ARs to be set ar1.get(timeout=5) ar2.get(timeout=5) ar3.get(timeout=5) # just getting here without throwing an exception is the true test! p._notify_stop() p.stop()
def setUp(self): self.lcm = LocalContextMixin() # this is not common use but legal
def __init__(self, *args, **kwargs): BaseContainerAgent.__init__(self, *args, **kwargs) self._is_started = False # set container id and cc_agent name (as they are set in base class call) self.id = get_default_container_id() self.name = "cc_agent_%s" % self.id self._capabilities = [] bootstrap.container_instance = self Container.instance = self log.debug("Container (sysname=%s) initializing ..." % bootstrap.get_sys_name()) # Keep track of the overrides from the command-line, so they can trump app/rel file data self.spawn_args = kwargs # DatastoreManager - controls access to Datastores (both mock and couch backed) self.datastore_manager = DatastoreManager() # TODO: Do not start a capability here. Symmetric start/stop self.datastore_manager.start() self._capabilities.append("DATASTORE_MANAGER") # Instantiate Directory self.directory = Directory() # internal router self.local_router = None # Create this Container's specific ExchangeManager instance self.ex_manager = ExchangeManager(self) # Create this Container's specific ProcManager instance self.proc_manager = ProcManager(self) # Create this Container's specific AppManager instance self.app_manager = AppManager(self) # File System - Interface to the OS File System, using correct path names and setups self.file_system = FileSystem(CFG) # Governance Controller - manages the governance related interceptors self.governance_controller = GovernanceController(self) # sFlow manager - controls sFlow stat emission self.sflow_manager = SFlowManager(self) # Coordinates the container start self._status = "INIT" # protection for when the container itself is used as a Process for clients self.container = self # publisher, initialized in start() self.event_pub = None # context-local storage self.context = LocalContextMixin() log.debug("Container initialized, OK.")