def testHandlerAfterBundle(self): """ Test a single component life cycle """ # Install iPOPO ipopo = install_ipopo(self.framework) # Install the component bundle install_bundle(self.framework, COMPONENT_BUNDLE_NAME) # The component must be absent self.assertFalse(ipopo.is_registered_instance(COMPONENT_NAME), "Instance already there") # Install the handler install_bundle(self.framework, HANDLER_BUNDLE_NAME) # The component must have been validated self.assertTrue(ipopo.is_registered_instance(COMPONENT_NAME), "Instance has not been validated") # Remove the handler self.framework.get_bundle_by_name(HANDLER_BUNDLE_NAME).stop() # The component must be absent self.assertFalse(ipopo.is_registered_instance(COMPONENT_NAME), "Instance still there") # Remove the component self.framework.get_bundle_by_name(COMPONENT_BUNDLE_NAME).stop()
def testDuplicatedFactory(self): """ Tests the behavior of iPOPO if two bundles provide the same factory. """ # Start the framework self.framework = FrameworkFactory.get_framework() self.framework.start() ipopo = install_ipopo(self.framework) # Install bundles: both provide a "basic-component-factory" factory # and instantiate a component (both with different names) module_A = install_bundle(self.framework, "tests.ipopo.ipopo_bundle") module_B = install_bundle(self.framework, "tests.ipopo.ipopo_bundle_copy") # Ensure that the module providing the factory is the correct one self.assertIs(module_A, ipopo.get_factory_bundle( module_A.BASIC_FACTORY).get_module(), "Duplicated factory is not provided by the first module") self.assertIs(module_A, ipopo.get_factory_bundle( module_B.BASIC_FACTORY).get_module(), "Duplicated factory is not provided by the first module") # Component of module A must be there self.assertIsNotNone( ipopo.get_instance_details(module_A.BASIC_INSTANCE), "Component from module A not started") # Component of module B must be absent self.assertRaises(ValueError, ipopo.get_instance_details, module_B.BASIC_INSTANCE)
def testDuplicatedFactory(self): """ Tests the behavior of iPOPO if two bundles provide the same factory. """ # Start the framework self.framework = FrameworkFactory.get_framework() self.framework.start() ipopo = install_ipopo(self.framework) # Install bundles: both provide a "basic-component-factory" factory # and instantiate a component (both with different names) module_A = install_bundle(self.framework, "tests.ipopo.ipopo_bundle") module_B = install_bundle(self.framework, "tests.ipopo.ipopo_bundle_copy") # Ensure that the module providing the factory is the correct one self.assertIs( module_A, ipopo.get_factory_bundle(module_A.BASIC_FACTORY).get_module(), "Duplicated factory is not provided by the first module") self.assertIs( module_A, ipopo.get_factory_bundle(module_B.BASIC_FACTORY).get_module(), "Duplicated factory is not provided by the first module") # Component of module A must be there self.assertIsNotNone( ipopo.get_instance_details(module_A.BASIC_INSTANCE), "Component from module A not started") # Component of module B must be absent self.assertRaises(ValueError, ipopo.get_instance_details, module_B.BASIC_INSTANCE)
def testInstantiateKillBeforeIPopo(self): """ Tests if the component is correctly instantiated when added and killed when removed """ # Add the component to the waiting list self.waiting.add(FACTORY_A, NAME_A) # Install iPOPO ipopo = install_ipopo(self.framework) # The component must be absent self.assertFalse(ipopo.is_registered_instance(NAME_A), "Instance already there") # Install the component bundle install_bundle(self.framework) # The instance must have been started self.assertTrue(ipopo.is_registered_instance(NAME_A), "Instance not there") # Remove the component from the waiting list self.waiting.remove(NAME_A) # The instance must have been kill self.assertFalse(ipopo.is_registered_instance(NAME_A), "Instance still there")
def testWaitingFactoryDetails(self): """ Tests the "handlers" entry of factory details dictionary """ # Install iPOPO ipopo = install_ipopo(self.framework) # Install the component bundle install_bundle(self.framework, COMPONENT_BUNDLE_NAME) # Find the component in the waiting list waiting = ipopo.get_waiting_components() for name, factory, missing in waiting: if name == COMPONENT_NAME: break else: # Component not found self.fail("Component not in the waiting handler list") # Check the missing handler self.assertCountEqual(missing, [HANDLER_ID]) # The instance details must fail (instance not ready) self.assertRaises(ValueError, ipopo.get_instance_details, COMPONENT_NAME) # Get factory details factory_details = ipopo.get_factory_details(factory) # The handlers details must be present self.assertIn(HANDLER_ID, factory_details["handlers"]) # Install the handler install_bundle(self.framework, HANDLER_BUNDLE_NAME) # The component is not waiting anymore waiting = ipopo.get_waiting_components() for name, factory, missing in waiting: if name == COMPONENT_NAME: self.fail("Component is still waiting for its handler") # Get instance details (must not fail) ipopo.get_instance_details(COMPONENT_NAME) # Remove the handler self.framework.get_bundle_by_name(HANDLER_BUNDLE_NAME).stop() # The component must be back in the waiting list waiting = ipopo.get_waiting_components() for name, factory, missing in waiting: if name == COMPONENT_NAME: break else: # Component not found self.fail("Component not in the waiting handler list") # The instance details must fail (instance not ready) self.assertRaises(ValueError, ipopo.get_instance_details, COMPONENT_NAME)
def testDuplicateHandler(self): """ Duplicated handler must be ignored """ # Install iPOPO ipopo = install_ipopo(self.framework) # Install the original handler install_bundle(self.framework, HANDLER_BUNDLE_NAME) # Register the duplicated handler properties = {constants.PROP_HANDLER_ID: HANDLER_ID} # Register the handler factory service context = self.framework.get_bundle_context() dummy_handler = DummyHandlerFactory() svc_reg = context.register_service( constants.SERVICE_IPOPO_HANDLER_FACTORY, dummy_handler, properties) # Install the component bundle install_bundle(self.framework, COMPONENT_BUNDLE_NAME) # The component is not waiting waiting = ipopo.get_waiting_components() for info in waiting: if info[0] == COMPONENT_NAME: self.fail("Component is waiting for its handler") # The duplicated handler must not have been called self.assertFalse(dummy_handler.called, "Second handler has been used") # Remove the original handler self.framework.get_bundle_by_name(HANDLER_BUNDLE_NAME).stop() # The component is not waiting waiting = ipopo.get_waiting_components() for info in waiting: if info[0] == COMPONENT_NAME: self.fail("Component is waiting for its handler") # The duplicated handler must have been called self.assertTrue(dummy_handler.called, "Second handler has not been used") # Unregister the duplicated handler svc_reg.unregister() # The component must be back in the waiting list waiting = ipopo.get_waiting_components() for info in waiting: if info[0] == COMPONENT_NAME: break else: # Component not found self.fail("Component not in the waiting handler list")
def testCycleOuterEnd(self): """ Tests if the required service is correctly unbound after the component invalidation """ module = install_bundle(self.framework) # Instantiate A (validated) compoA = self.ipopo.instantiate(module.FACTORY_A, NAME_A) self.assertEqual([IPopoEvent.INSTANTIATED, IPopoEvent.VALIDATED], compoA.states, "Invalid component states: {0}".format(compoA.states)) compoA.reset() # Instantiate B (bound then validated) compoB = self.ipopo.instantiate(module.FACTORY_B, NAME_B) self.assertEqual([IPopoEvent.INSTANTIATED, IPopoEvent.BOUND, IPopoEvent.VALIDATED], compoB.states, "Invalid component states: {0}".format(compoA.states)) compoB.reset() # Uninstantiate A self.ipopo.kill(NAME_A) self.assertEqual([IPopoEvent.INVALIDATED], compoA.states, "Invalid component states: {0}".format(compoA.states)) self.assertEqual([IPopoEvent.INVALIDATED, IPopoEvent.UNBOUND], compoB.states, "Invalid component states: {0}".format(compoB.states)) compoB.reset() # Uninstantiate B self.ipopo.kill(NAME_B) self.assertEqual([], compoB.states, "Invalid component states: {0}".format(compoA.states))
def test_get_instance_details(self): """ Instance details method test """ module = install_bundle(self.framework) # Invalid component names for invalid in (None, "", [1], ["a", "b"]): self.assertRaises(ValueError, self.ipopo.get_instance_details, invalid) # Get its details details = self.ipopo.get_instance_details(module.BASIC_INSTANCE) # Test if instance details are really in the output self.assertIs(type(details), dict, "Details result must be a dictionary") self.assertEqual(details['factory'], module.BASIC_FACTORY, "Invalid factory name") self.assertEqual(details['name'], module.BASIC_INSTANCE, "Invalid component name") self.assertIs(type(details['state']), int, "Component state must be an integer") self.assertIs(type(details['services']), dict, "Services details must be in a dictionary") self.assertIs(type(details['dependencies']), dict, "Dependencies details must be in a dictionary")
def setUp(self): """ Called before each test. Initiates a framework. """ self.framework = FrameworkFactory.get_framework() self.framework.start() self.ipopo_bundle = install_bundle(self.framework, "pelix.ipopo.core")
def testGetInstanceDetails(self): """ Instance details method test """ module = install_bundle(self.framework) # Invalid component names for invalid in (None, "", [1], ["a", "b"]): self.assertRaises(ValueError, self.ipopo.get_instance_details, invalid) # Get its details details = self.ipopo.get_instance_details(module.BASIC_INSTANCE) # Test if instance details are really in the output self.assertIs(type(details), dict, "Details result must be a dictionary") self.assertEqual(details['factory'], module.BASIC_FACTORY, "Invalid factory name") self.assertEqual(details['name'], module.BASIC_INSTANCE, "Invalid component name") self.assertIs(type(details['state']), int, "Component state must be an integer") self.assertIs(type(details['services']), dict, "Services details must be in a dictionary") self.assertIs(type(details['dependencies']), dict, "Dependencies details must be in a dictionary")
def setUp(self): """ Called before each test. Initiates a framework. """ # Prepare the framework self.framework = FrameworkFactory.get_framework() self.framework.start() context = self.framework.get_bundle_context() # Install & start the waiting list bundle install_bundle(self.framework, "pelix.ipopo.waiting") # Get the service svc_ref = context.get_service_reference( constants.SERVICE_IPOPO_WAITING_LIST) self.waiting = context.get_service(svc_ref)
def test_incomplete_properties(self): """ Tests the behaviour when a property is missing """ module = install_bundle(self.framework) context = self.framework.get_bundle_context() assert isinstance(context, BundleContext) answer = 42 random_static = ''.join(random.choice(string.ascii_letters) for _ in range(50)) # Assert that the service is not yet available self.assertIsNone(context.get_service_reference(IEchoService), "Service is already registered") # Instantiate a service, matching the filter svc1 = object() context.register_service( IEchoService, svc1, {"s": random_static, "a": answer}) for name, factory in ( (NAME_A, module.FACTORY_REQUIRES_VAR_FILTER), (NAME_B, module.FACTORY_REQUIRES_VAR_FILTER_AGGREGATE)): # Instantiate the component, without the static property consumer = self.ipopo.instantiate(factory, name, {}) # Force the "answer" property to an int consumer.change(answer) # Component must be instantiated, but not valid self.assertListEqual( [IPopoEvent.INSTANTIATED], consumer.states, "Invalid component states: {0}".format(consumer.states)) self.assertIs(consumer.service, None, "Service injected")
def setUp(self): """ Called before each test. Initiates a framework. """ self.framework = FrameworkFactory.get_framework() self.framework.start() self.ipopo = install_ipopo(self.framework) self.module = install_bundle(self.framework)
def test_requires_var_filter(self): """ Tests the @RequiresVarFilter handler without immediate_rebind (default) """ module = install_bundle(self.framework) self.__internal_test(module, [IPopoEvent.INVALIDATED, IPopoEvent.UNBOUND, IPopoEvent.BOUND, IPopoEvent.VALIDATED])
def test_no_change(self): """ Test the behaviour when the LDAP filter doesn't change with the property """ module = install_bundle(self.framework) context = self.framework.get_bundle_context() assert isinstance(context, BundleContext) random_static = ''.join( random.choice(string.ascii_letters) for _ in range(50)) # Assert that the service is not yet available self.assertIsNone(context.get_service_reference(IEchoService), "Service is already registered") # Instantiate the components consumer_single = self.ipopo.instantiate( module.FACTORY_REQUIRES_VAR_FILTER, NAME_A, {"static": random_static}) consumer_multi = self.ipopo.instantiate( module.FACTORY_REQUIRES_VAR_FILTER_AGGREGATE, NAME_B, {"static": random_static}) consumers = (consumer_single, consumer_multi) # Force the "answer" property to an int for consumer in consumers: consumer.change(42) # Instantiate a service, matching the filter svc1 = object() context.register_service(IEchoService, svc1, { "s": random_static, "a": consumer_single.answer }) # Component must be valid for consumer in consumers: self.assertListEqual([ IPopoEvent.INSTANTIATED, IPopoEvent.BOUND, IPopoEvent.VALIDATED ], consumer.states, "Invalid component states: {0}".format( consumer.states)) consumer.reset() # Set the filter with a similar value (same once formatted) for consumer in consumers: consumer.change("42") # The consumer should not be notified for consumer in consumers: self.assertListEqual([], consumer.states, "Invalid component states: {0}".format( consumer.states)) consumer.reset() self.assertIs(consumer_single.service, svc1, "Wrong service injected") self.assertListEqual(consumer_multi.service, [svc1], "Wrong service injected")
def test_requires_var_filter(self): """ Tests the @RequiresVarFilter handler without immediate_rebind (default) """ module = install_bundle(self.framework) self.__internal_test(module, [ IPopoEvent.INVALIDATED, IPopoEvent.UNBOUND, IPopoEvent.BOUND, IPopoEvent.VALIDATED ])
def test_no_change(self): """ Test the behaviour when the LDAP filter doesn't change with the property """ module = install_bundle(self.framework) context = self.framework.get_bundle_context() assert isinstance(context, BundleContext) random_static = ''.join(random.choice(string.ascii_letters) for _ in range(50)) # Assert that the service is not yet available self.assertIsNone(context.get_service_reference(IEchoService), "Service is already registered") # Instantiate the components consumer_single = self.ipopo.instantiate( module.FACTORY_REQUIRES_VAR_FILTER, NAME_A, {"static": random_static}) consumer_multi = self.ipopo.instantiate( module.FACTORY_REQUIRES_VAR_FILTER_AGGREGATE, NAME_B, {"static": random_static}) consumers = (consumer_single, consumer_multi) # Force the "answer" property to an int for consumer in consumers: consumer.change(42) # Instantiate a service, matching the filter svc1 = object() context.register_service( IEchoService, svc1, {"s": random_static, "a": consumer_single.answer}) # Component must be valid for consumer in consumers: self.assertListEqual( [IPopoEvent.INSTANTIATED, IPopoEvent.BOUND, IPopoEvent.VALIDATED], consumer.states, "Invalid component states: {0}".format(consumer.states)) consumer.reset() # Set the filter with a similar value (same once formatted) for consumer in consumers: consumer.change("42") # The consumer should not be notified for consumer in consumers: self.assertListEqual( [], consumer.states, "Invalid component states: {0}".format(consumer.states)) consumer.reset() self.assertIs(consumer_single.service, svc1, "Wrong service injected") self.assertListEqual(consumer_multi.service, [svc1], "Wrong service injected")
def test_immediate_rebind(self): """ Tests the immediate_rebind flag of @Requires """ module = install_bundle(self.framework) context = self.framework.get_bundle_context() # Instantiate the consumer consumer = self.ipopo.instantiate(module.FACTORY_IMMEDIATE, NAME_A) self.assertListEqual([IPopoEvent.INSTANTIATED], consumer.states, "Invalid component states: {0}".format( consumer.states)) consumer.reset() # Instantiate a service svc1 = object() reg1 = context.register_service(IEchoService, svc1, {}) # The consumer must have been validated self.assertListEqual( [IPopoEvent.BOUND, IPopoEvent.VALIDATED], consumer.states, "Invalid component states: {0}".format(consumer.states)) self.assertIs(consumer.service, svc1, "Wrong service injected") consumer.reset() # Register a second service svc2 = object() reg2 = context.register_service(IEchoService, svc2, {}) # No modification for the consumer self.assertListEqual([], consumer.states, "Invalid component states: {0}".format( consumer.states)) self.assertIs(consumer.service, svc1, "Wrong service injected") consumer.reset() # Unregister service 1 reg1.unregister() # Component must not have been invalidated, and the second service must # have been injected self.assertListEqual( [IPopoEvent.UNBOUND, IPopoEvent.BOUND], consumer.states, "Invalid component states: {0}".format(consumer.states)) self.assertIs(consumer.service, svc2, "Wrong service injected") consumer.reset() # Unregister service 2 reg2.unregister() # Component must have been invalidated self.assertListEqual( [IPopoEvent.INVALIDATED, IPopoEvent.UNBOUND], consumer.states, "Invalid component states: {0}".format(consumer.states)) self.assertIsNone(consumer.service, "Service still injected") consumer.reset()
def test_immediate_rebind(self): """ Tests the immediate_rebind flag of @Requires """ module = install_bundle(self.framework) context = self.framework.get_bundle_context() # Instantiate the consumer consumer = self.ipopo.instantiate(module.FACTORY_IMMEDIATE, NAME_A) self.assertListEqual([IPopoEvent.INSTANTIATED], consumer.states, "Invalid component states: {0}" .format(consumer.states)) consumer.reset() # Instantiate a service svc1 = object() reg1 = context.register_service(IEchoService, svc1, {}) # The consumer must have been validated self.assertListEqual([IPopoEvent.BOUND, IPopoEvent.VALIDATED], consumer.states, "Invalid component states: {0}" .format(consumer.states)) self.assertIs(consumer.service, svc1, "Wrong service injected") consumer.reset() # Register a second service svc2 = object() reg2 = context.register_service(IEchoService, svc2, {}) # No modification for the consumer self.assertListEqual([], consumer.states, "Invalid component states: {0}" .format(consumer.states)) self.assertIs(consumer.service, svc1, "Wrong service injected") consumer.reset() # Unregister service 1 reg1.unregister() # Component must not have been invalidated, and the second service must # have been injected self.assertListEqual([IPopoEvent.UNBOUND, IPopoEvent.BOUND], consumer.states, "Invalid component states: {0}" .format(consumer.states)) self.assertIs(consumer.service, svc2, "Wrong service injected") consumer.reset() # Unregister service 2 reg2.unregister() # Component must have been invalidated self.assertListEqual([IPopoEvent.INVALIDATED, IPopoEvent.UNBOUND], consumer.states, "Invalid component states: {0}" .format(consumer.states)) self.assertIsNone(consumer.service, "Service still injected") consumer.reset()
def testProvides(self): """ Tests if the provides decorator works """ module = install_bundle(self.framework) context = self.framework.get_bundle_context() assert isinstance(context, BundleContext) # Assert that the service is not yet available self.assertIsNone(context.get_service_reference(IEchoService), "Service is already registered") # Instantiate the component compoA = self.ipopo.instantiate(module.FACTORY_A, NAME_A) try: # Service should be there ref = context.get_service_reference(IEchoService) self.assertIsNotNone(ref, "Service hasn't been registered") # Second service should be there ref2 = context.get_service_reference("TestService") self.assertIsNotNone(ref, "Service hasn't been registered") # References must be different self.assertNotEqual(ref, ref2, "Service references must be different") # Compare service instances svc = context.get_service(ref) self.assertIs(svc, compoA, "Different instances for service and component") svc2 = context.get_service(ref2) self.assertEqual(svc, svc2, "Got different service instances") # Clean up context.unget_service(ref) context.unget_service(ref2) svc = None svc2 = None # Invalidate the component self.ipopo.invalidate(NAME_A) # Service should not be there anymore self.assertIsNone(context.get_service_reference(IEchoService), "Service is still registered") finally: try: self.ipopo.kill(NAME_A) except: pass
def test_no_rebind(self): """ Tests the @RequiresBest handler without immediate_rebind """ # Modify the component factory module = install_bundle(self.framework) context = get_factory_context(module.RequiresBestComponentFactory) configs = context.get_handler(RequiresBest.HANDLER_ID) configs["service"].immediate_rebind = False self.__internal_test(module, [IPopoEvent.INVALIDATED, IPopoEvent.UNBOUND, IPopoEvent.BOUND, IPopoEvent.VALIDATED])
def test_immediate_rebind(self): """ Tests the @RequiresVarFilter handler with immediate_rebind """ # Modify component factories module = install_bundle(self.framework) for clazz in (module.RequiresVarFilterComponentFactory, module.RequiresVarFilterAggregateComponentFactory): context = get_factory_context(clazz) configs = context.get_handler(RequiresVarFilter.HANDLER_ID) configs["service"].immediate_rebind = True self.__internal_test(module, [IPopoEvent.UNBOUND, IPopoEvent.BOUND])
def testGetMethodDescription(self): """ Tests the ipopo.decorators.get_method_description() method """ bundle_name = "tests.framework.simple_bundle" bundle = install_bundle(self.framework, bundle_name) descr = decorators.get_method_description(bundle.ActivatorTest.start) # Assert we found sufficient data self.assertTrue(descr.startswith("'start'"), "Method name not found") self.assertIn(bundle_name.replace(".", os.sep) + ".py", descr, "File couldn't determined") # Some methods are unreadable self.assertEqual("'getpid'", decorators.get_method_description(os.getpid), "Invalid description of getpid()")
def test_factory(self): """ Tests @Provides service factory handling """ module = install_bundle(self.framework) context = self.framework.get_bundle_context() # Instantiate the provider component = self.ipopo.instantiate( module.FACTORY_PROVIDES_SVC_FACTORY, "provides.factory") # Ensure the initial state self.assertIsNone(component.caller, "Invalid initial state") self.assertIsNone(component.registration, "Invalid initial state") # Consume the service svc_ref = context.get_service_reference("factory.service") svc = context.get_service(svc_ref) # Ensure the new state self.assertIs(component.caller, self.framework) self.assertIs(component.registration.get_reference(), svc_ref) self.assertIs(component.service, svc) # Reset state component.caller = None component.registration = None # Try to re-get the service svc2 = context.get_service(svc_ref) # Ensure no sub call and same service self.assertIsNone(component.caller) self.assertIsNone(component.registration) self.assertIs(svc, svc2) # Unget the service context.unget_service(svc_ref) self.assertIsNone(component.caller) self.assertIsNone(component.registration) self.assertIs(svc, svc2) # A second time context.unget_service(svc_ref) self.assertIs(component.caller, self.framework) self.assertIs(component.registration.get_reference(), svc_ref) self.assertFalse(component.service)
def test_factory(self): """ Tests @Provides service factory handling """ module = install_bundle(self.framework) context = self.framework.get_bundle_context() # Instantiate the provider component = self.ipopo.instantiate(module.FACTORY_PROVIDES_SVC_FACTORY, "provides.factory") # Ensure the initial state self.assertIsNone(component.caller, "Invalid initial state") self.assertIsNone(component.registration, "Invalid initial state") # Consume the service svc_ref = context.get_service_reference("factory.service") svc = context.get_service(svc_ref) # Ensure the new state self.assertIs(component.caller, self.framework) self.assertIs(component.registration.get_reference(), svc_ref) self.assertIs(component.service, svc) # Reset state component.caller = None component.registration = None # Try to re-get the service svc2 = context.get_service(svc_ref) # Ensure no sub call and same service self.assertIsNone(component.caller) self.assertIsNone(component.registration) self.assertIs(svc, svc2) # Unget the service context.unget_service(svc_ref) self.assertIsNone(component.caller) self.assertIsNone(component.registration) self.assertIs(svc, svc2) # A second time context.unget_service(svc_ref) self.assertIs(component.caller, self.framework) self.assertIs(component.registration.get_reference(), svc_ref) self.assertIsNone(component.service)
def test_get_instance(self): """ Tests the get_instance(name) method """ # Test if the framework is clean self.assertEqual(len(self.ipopo.get_factories()), 0, "Some factories are already registered.") # Test the KeyError behaviour for name in (None, "", "unknown", 42): self.assertRaises(KeyError, self.ipopo.get_instance, name) self.assertRaises(KeyError, self.ipopo.get_instance, BASIC_INSTANCE) # Start the test bundle module = install_bundle(self.framework) # Test the KeyError behaviour for name in (None, "", "unknown", 42): self.assertRaises(KeyError, self.ipopo.get_instance, name) # Check if we got the object self.assertIsInstance(self.ipopo.get_instance(BASIC_INSTANCE), module.BasicComponent) # Check we got the good object factory_name = "dummy-factory" instance_name = "some-instance" context = self.framework.get_bundle_context() @decorators.ComponentFactory(factory_name) class TestComponent(object): pass # Register the factory self.ipopo.register_factory(context, TestComponent) # Start a component component = self.ipopo.instantiate(factory_name, instance_name) # Check the behaviour self.assertIs(self.ipopo.get_instance(instance_name), component) # Clean up self.ipopo.kill(instance_name) self.ipopo.unregister_factory(factory_name)
def testCallbackRaiser(self): """ Tests exception handling during a callback """ module = install_bundle(self.framework) # Instantiate B (no requirement present) compoB = self.ipopo.instantiate(module.FACTORY_B, NAME_B) self.assertEqual([IPopoEvent.INSTANTIATED], compoB.states, "Invalid component states: {0}".format(compoB.states)) compoB.reset() # Set B in raiser mode compoB.raiser = True # Instantiate A (validated) log_off() compoA = self.ipopo.instantiate(module.FACTORY_A, NAME_A) log_on() self.assertEqual([IPopoEvent.INSTANTIATED, IPopoEvent.VALIDATED], compoA.states, "Invalid component states: {0}".format(compoA.states)) compoA.reset() # B must have been validated self.assertEqual([IPopoEvent.BOUND, IPopoEvent.VALIDATED], compoB.states, "Invalid component states: {0}".format(compoB.states)) compoB.reset() # Uninstantiate A log_off() self.ipopo.kill(NAME_A) log_on() self.assertEqual([IPopoEvent.INVALIDATED], compoA.states, "Invalid component states: {0}".format(compoA.states)) # Uninstantiate B self.assertEqual([IPopoEvent.INVALIDATED, IPopoEvent.UNBOUND], compoB.states, "Invalid component states: {0}".format(compoB.states))
def testAggregateDependencyLate(self): """ Tests a component that aggregates dependencies, with one dependency already present before its instantiation """ # Install the test bundle module = install_bundle(self.framework) # Register a first service context = self.framework.get_bundle_context() context.register_service(IEchoService, self, None) # Instantiate C (no requirement present, but they are optional) compoC = self.ipopo.instantiate(module.FACTORY_C, NAME_C) self.assertIn(self, compoC.services, "Existing service not injected") self.assertEqual([IPopoEvent.INSTANTIATED, IPopoEvent.BOUND, IPopoEvent.VALIDATED], compoC.states, "Invalid component states: {0}".format(compoC.states))
def testAggregateDependencyLate(self): """ Tests a component that aggregates dependencies, with one dependency already present before its instantiation """ # Install the test bundle module = install_bundle(self.framework) # Register a first service context = self.framework.get_bundle_context() context.register_service(IEchoService, self, None) # Instantiate C (no requirement present, but they are optional) compoC = self.ipopo.instantiate(module.FACTORY_C, NAME_C) self.assertIn(self, compoC.services, "Existing service not injected") self.assertEqual( [IPopoEvent.INSTANTIATED, IPopoEvent.BOUND, IPopoEvent.VALIDATED], compoC.states, "Invalid component states: {0}".format(compoC.states))
def testInsideFramework(self): """ Tests the behavior of a manipulated class attributes """ # Start the framework self.framework = FrameworkFactory.get_framework() self.framework.start() ipopo = install_ipopo(self.framework) module = install_bundle(self.framework) # Instantiate the test class instance = module.ComponentFactoryA() # Check fields presence and values self.assertTrue(instance._test_ctrl, "Default service controller is On") self.assertIsNone(instance._req_1, "Requirement is not None") self.assertEqual(instance.prop_1, 10, "Constructor property value lost") self.assertEqual(instance.usable, True, "Incorrect property value") del instance # Instantiate component A (validated) instance = ipopo.instantiate(module.FACTORY_A, NAME_A) # Check fields presence and values self.assertTrue(instance._test_ctrl, "Default service controller is On") self.assertIsNone(instance._req_1, "Requirement is not None") self.assertIsNone(instance.prop_1, "Default property value is None") self.assertEqual(instance.usable, True, "Incorrect property value") instance.prop_1 = 42 instance.usable = False self.assertEqual(instance.prop_1, 42, "Property value not modified") self.assertEqual(instance.usable, False, "Property value not modified") # Set A usable again instance.change(True) self.assertEqual(instance.usable, True, "Property value not modified")
def testInsideFramework(self): """ Tests the behavior of a manipulated class attributes """ # Start the framework self.framework = FrameworkFactory.get_framework() self.framework.start() ipopo = install_ipopo(self.framework) module = install_bundle(self.framework) # Instantiate the test class instance = module.ComponentFactoryA() # Check fields presence and values self.assertTrue( instance._test_ctrl, "Default service controller is On") self.assertIsNone(instance._req_1, "Requirement is not None") self.assertEqual( instance.prop_1, 10, "Constructor property value lost") self.assertEqual(instance.usable, True, "Incorrect property value") del instance # Instantiate component A (validated) instance = ipopo.instantiate(module.FACTORY_A, NAME_A) # Check fields presence and values self.assertTrue( instance._test_ctrl, "Default service controller is On") self.assertIsNone(instance._req_1, "Requirement is not None") self.assertIsNone(instance.prop_1, "Default property value is None") self.assertEqual(instance.usable, True, "Incorrect property value") instance.prop_1 = 42 instance.usable = False self.assertEqual(instance.prop_1, 42, "Property value not modified") self.assertEqual(instance.usable, False, "Property value not modified") # Set A usable again instance.change(True) self.assertEqual(instance.usable, True, "Property value not modified")
def testInstantiateConflict(self): """ Try to instantiate a component with a name already used """ # Install iPOPO ipopo = install_ipopo(self.framework) # Install the component bundle module = install_bundle(self.framework) # The component must be present self.assertTrue(ipopo.is_registered_instance(module.BASIC_INSTANCE), "Auto-instance not yet there") # This addition must not fail, but must be logger self.waiting.add(module.BASIC_FACTORY, module.BASIC_INSTANCE) # The original instance must still be there self.assertTrue(ipopo.is_registered_instance(module.BASIC_INSTANCE), "Instance has been killed")
def testConfiguredInstance(self): """ Tests if the filter can be overridden by instance properties """ module = install_bundle(self.framework) # The module filter properties_b = { constants.IPOPO_REQUIRES_FILTERS: { "service": "({0}=True)".format(module.PROP_USABLE) } } # Instantiate A (validated) compoA = self.ipopo.instantiate(module.FACTORY_A, NAME_A) # Set A unusable compoA.change(False) # Instantiate B (must not be bound) compoB = self.ipopo.instantiate(module.FACTORY_B, NAME_B, properties_b) self.assertEqual([IPopoEvent.INSTANTIATED], compoB.states, "Invalid component states: {0}".format(compoB.states)) compoB.reset() # Set A usable compoA.change(True) # B must be bound and validated self.assertEqual([IPopoEvent.BOUND, IPopoEvent.VALIDATED], compoB.states, "Invalid component states: {0}".format(compoB.states)) compoB.reset() # Set A unusable (again) compoA.change(False) # B must have been invalidated self.assertEqual([IPopoEvent.INVALIDATED, IPopoEvent.UNBOUND], compoB.states, "Invalid component states: {0}".format(compoB.states))
def testConfiguredInstance(self): """ Tests if the filter can be overridden by instance properties """ module = install_bundle(self.framework) # The module filter properties_b = {constants.IPOPO_REQUIRES_FILTERS: {"service": "({0}=True)".format(module.PROP_USABLE)}} # Instantiate A (validated) compoA = self.ipopo.instantiate(module.FACTORY_A, NAME_A) # Set A unusable compoA.change(False) # Instantiate B (must not be bound) compoB = self.ipopo.instantiate(module.FACTORY_B, NAME_B, properties_b) self.assertEqual([IPopoEvent.INSTANTIATED], compoB.states, "Invalid component states: {0}".format(compoB.states)) compoB.reset() # Set A usable compoA.change(True) # B must be bound and validated self.assertEqual([IPopoEvent.BOUND, IPopoEvent.VALIDATED], compoB.states, "Invalid component states: {0}".format(compoB.states)) compoB.reset() # Set A unusable (again) compoA.change(False) # B must have been invalidated self.assertEqual([IPopoEvent.INVALIDATED, IPopoEvent.UNBOUND], compoB.states, "Invalid component states: {0}".format(compoB.states))
def test_ipopo_start_installed(self): """ Tests if iPOPO starts instances of already installed bundles """ # Uninstall the iPOPO bundle ipopo_bundle = self.framework.get_bundle_by_name("pelix.ipopo.core") ipopo_bundle.uninstall() self.ipopo = None # Install the test bundle module = install_bundle(self.framework) # Install iPOPO self.ipopo = install_ipopo(self.framework) # Test if the automatic instance is there self.assertTrue(self.ipopo.is_registered_factory(module.BASIC_FACTORY), "Factory not registered") self.assertTrue( self.ipopo.is_registered_instance(module.BASIC_INSTANCE), "Component not created")
def testIPopoStartInstalled(self): """ Tests if iPOPO starts instances of already installed bundles """ # Uninstall the iPOPO bundle ipopo_bundle = self.framework.get_bundle_by_name("pelix.ipopo.core") ipopo_bundle.uninstall() self.ipopo = None # Install the test bundle module = install_bundle(self.framework) # Install iPOPO self.ipopo = install_ipopo(self.framework) # Test if the automatic instance is there self.assertTrue(self.ipopo.is_registered_factory(module.BASIC_FACTORY), "Factory not registered") self.assertTrue( self.ipopo.is_registered_instance(module.BASIC_INSTANCE), "Component not created")
def test_incomplete_properties(self): """ Tests the behaviour when a property is missing """ module = install_bundle(self.framework) context = self.framework.get_bundle_context() assert isinstance(context, BundleContext) answer = 42 random_static = ''.join( random.choice(string.ascii_letters) for _ in range(50)) # Assert that the service is not yet available self.assertIsNone(context.get_service_reference(IEchoService), "Service is already registered") # Instantiate a service, matching the filter svc1 = object() context.register_service(IEchoService, svc1, { "s": random_static, "a": answer }) for name, factory in ((NAME_A, module.FACTORY_REQUIRES_VAR_FILTER), (NAME_B, module.FACTORY_REQUIRES_VAR_FILTER_AGGREGATE)): # Instantiate the component, without the static property consumer = self.ipopo.instantiate(factory, name, {}) # Force the "answer" property to an int consumer.change(answer) # Component must be instantiated, but not valid self.assertListEqual([IPopoEvent.INSTANTIATED], consumer.states, "Invalid component states: {0}".format( consumer.states)) self.assertIs(consumer.service, None, "Service injected")
def testCycleInner(self): """ Tests if the component is bound, validated then invalidated. The component unbind call must come after it has been killed """ module = install_bundle(self.framework) # Instantiate A (validated) compoA = self.ipopo.instantiate(module.FACTORY_A, NAME_A) self.assertEqual([IPopoEvent.INSTANTIATED, IPopoEvent.VALIDATED], compoA.states, "Invalid component states: {0}".format(compoA.states)) compoA.reset() # Instantiate B (bound then validated) compoB = self.ipopo.instantiate(module.FACTORY_B, NAME_B) self.assertEqual( [IPopoEvent.INSTANTIATED, IPopoEvent.BOUND, IPopoEvent.VALIDATED], compoB.states, "Invalid component states: {0}".format(compoB.states)) compoB.reset() # Invalidate B self.ipopo.invalidate(NAME_B) self.assertEqual([IPopoEvent.INVALIDATED], compoB.states, "Invalid component states: {0}".format(compoB.states)) compoB.reset() # Uninstantiate B self.ipopo.kill(NAME_B) self.assertEqual([IPopoEvent.UNBOUND], compoB.states, "Invalid component states: {0}".format(compoB.states)) # Uninstantiate A self.ipopo.kill(NAME_A) self.assertEqual([IPopoEvent.INVALIDATED], compoA.states, "Invalid component states: {0}".format(compoA.states))
def testCycleInner(self): """ Tests if the component is bound, validated then invalidated. The component unbind call must come after it has been killed """ module = install_bundle(self.framework) # Instantiate A (validated) compoA = self.ipopo.instantiate(module.FACTORY_A, NAME_A) self.assertEqual([IPopoEvent.INSTANTIATED, IPopoEvent.VALIDATED], compoA.states, "Invalid component states: {0}".format(compoA.states)) compoA.reset() # Instantiate B (bound then validated) compoB = self.ipopo.instantiate(module.FACTORY_B, NAME_B) self.assertEqual([IPopoEvent.INSTANTIATED, IPopoEvent.BOUND, IPopoEvent.VALIDATED], compoB.states, "Invalid component states: {0}".format(compoB.states)) compoB.reset() # Invalidate B self.ipopo.invalidate(NAME_B) self.assertEqual([IPopoEvent.INVALIDATED], compoB.states, "Invalid component states: {0}".format(compoB.states)) compoB.reset() # Uninstantiate B self.ipopo.kill(NAME_B) self.assertEqual([IPopoEvent.UNBOUND], compoB.states, "Invalid component states: {0}".format(compoB.states)) # Uninstantiate A self.ipopo.kill(NAME_A) self.assertEqual([IPopoEvent.INVALIDATED], compoA.states, "Invalid component states: {0}".format(compoA.states))
def testController(self): """ Tests the service controller """ module = install_bundle(self.framework) context = self.framework.get_bundle_context() assert isinstance(context, BundleContext) # Assert that the service is not yet available self.assertIsNone(context.get_service_reference(IEchoService), "Service is already registered") self.assertIsNone(context.get_service_reference("TestService"), "TestService is already registered") # Instantiate the component self.ipopo.instantiate(module.FACTORY_A, NAME_A) try: # Service should be there (controller default value is True) self.assertIsNotNone(context.get_service_reference(IEchoService), "EchoService hasn't been registered") ref = context.get_service_reference("TestService") self.assertIsNotNone(ref, "TestService hasn't been registered") # Get the service instance svc = context.get_service(ref) # Change the value of the controller svc.change_controller(False) self.assertIsNone(context.get_service_reference("TestService"), "TestService hasn't been unregistered") self.assertIsNotNone(context.get_service_reference(IEchoService), "EchoService has been unregistered") # Re-change the value svc.change_controller(True) self.assertIsNotNone(context.get_service_reference("TestService"), "TestService hasn't been re-registered") self.assertIsNotNone(context.get_service_reference(IEchoService), "EchoService has been unregistered") # Invalidate the component self.ipopo.invalidate(NAME_A) # Re-change the value (once invalidated) svc.change_controller(True) # Service should not be there anymore self.assertIsNone(context.get_service_reference("TestService"), "TestService is still registered") self.assertIsNone(context.get_service_reference(IEchoService), "EchoService is still registered") # Clean up context.unget_service(ref) svc = None ref = None finally: try: self.ipopo.kill(NAME_A) except: pass
def test_temporal_lifecycle(self): """ Tests the component life cycle """ module = install_bundle(self.framework) context = self.framework.get_bundle_context() assert isinstance(context, BundleContext) # Assert that the service is not yet available self.assertIsNone(context.get_service_reference(IEchoService), "Service is already registered") # Import TemporalException here, or the type will be different from the # one loaded by the framework. from pelix.ipopo.handlers.temporal import TemporalException # Get the value from the configuration of the handler factory_context = get_factory_context(module.TemporalComponentFactory) configs = factory_context.get_handler(Temporal.HANDLER_ID) timeout = configs["service"][1] # Instantiate the component consumer = self.ipopo.instantiate(module.FACTORY_TEMPORAL, NAME_A) # Component must be invalid self.assertListEqual([IPopoEvent.INSTANTIATED], consumer.states) consumer.reset() # Instantiate a service svc1 = Dummy() reg1 = context.register_service(IEchoService, svc1, {}) # The consumer must have been validated self.assertListEqual([IPopoEvent.BOUND, IPopoEvent.VALIDATED], consumer.states) consumer.reset() # Make a call self.assertEqual(consumer.call(), svc1.method()) # Register service 2 svc2 = Dummy() reg2 = context.register_service(IEchoService, svc2, {}) # No modification self.assertListEqual([], consumer.states) consumer.reset() # Unregister service 1 reg1.unregister() self.assertListEqual([IPopoEvent.UNBOUND, IPopoEvent.BOUND], consumer.states) self.assertEqual(consumer.call(), svc2.method()) consumer.reset() # Unregister service 2 reg2.unregister() # No modification yet self.assertListEqual([], consumer.states) consumer.reset() # Register a new service svc3 = Dummy() reg3 = context.register_service(IEchoService, svc3, {}) # Service must have been injected before invalidation self.assertListEqual([IPopoEvent.UNBOUND, IPopoEvent.BOUND], consumer.states) self.assertEqual(consumer.call(), svc3.method()) consumer.reset() # Unregister service 3 reg3.unregister() # No modification yet self.assertListEqual([], consumer.states) consumer.reset() start = time.time() try: # Try to call the method consumer.call() except TemporalException: # OK ! pass else: self.fail("No temporal exception raised during call") end = time.time() # Check timeout self.assertLess(end - start, timeout * 2.) self.assertGreater(end - start, timeout / 2.) # Wait a little time.sleep(.2) # Check state self.assertListEqual([IPopoEvent.INVALIDATED, IPopoEvent.UNBOUND], consumer.states) consumer.reset()
def test_invalid_filter(self): """ Tests the behaviour with badly formatted LDAP filters """ module = install_bundle(self.framework) context = self.framework.get_bundle_context() assert isinstance(context, BundleContext) random_static = ''.join(random.choice(string.ascii_letters) for _ in range(50)) # Assert that the service is not yet available self.assertIsNone(context.get_service_reference(IEchoService), "Service is already registered") # Instantiate the components consumer_single = self.ipopo.instantiate( module.FACTORY_REQUIRES_VAR_FILTER, NAME_A, {"static": random_static}) consumer_multi = self.ipopo.instantiate( module.FACTORY_REQUIRES_VAR_FILTER_AGGREGATE, NAME_B, {"static": random_static}) consumers = (consumer_single, consumer_multi) # Force the "answer" property to an int for consumer in consumers: consumer.change(42) # Instantiate a service, matching the filter svc1 = object() context.register_service( IEchoService, svc1, {"s": random_static, "a": consumer_single.answer}) # Component must be valid for consumer in consumers: self.assertListEqual( [IPopoEvent.INSTANTIATED, IPopoEvent.BOUND, IPopoEvent.VALIDATED], consumer.states, "Invalid component states: {0}".format(consumer.states)) consumer.reset() # Set an invalid filter for consumer in consumers: consumer.change(")") # The consumer must have been validated self.assertListEqual( [IPopoEvent.INVALIDATED, IPopoEvent.UNBOUND], consumer.states, "Invalid component states: {0}".format(consumer.states)) consumer.reset() self.assertIs(consumer.service, None, "A service is injected") # Check other invalid filters for consumer in consumers: for invalid in ("", "=", "("): # Force the "answer" property to an invalid value consumer.change(invalid) # Instantiate a service, matching the filter svc = object() reg = context.register_service( IEchoService, svc, {"s": random_static, "a": invalid}) # Nothing should happen self.assertListEqual( [], consumer.states, "Invalid component states: {0}".format(consumer.states)) consumer.reset() reg.unregister()
def test_requires_best(self): """ Tests the @RequiresBest handler with immediate_rebind (default) """ module = install_bundle(self.framework) self.__internal_test(module, [IPopoEvent.UNBOUND, IPopoEvent.BOUND])