Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Very early draft of a ServiceFactory handling
- Loading branch information
Showing
4 changed files
with
237 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/usr/bin/env python | ||
# -- Content-Encoding: UTF-8 -- | ||
""" | ||
Simple bundle registering a service | ||
:author: Thomas Calmant | ||
""" | ||
|
||
__version__ = (1, 0, 0) | ||
|
||
SVC = "greetings" | ||
|
||
from pelix.constants import BundleActivator | ||
|
||
|
||
class Service: | ||
""" | ||
Service per bundle | ||
""" | ||
def __init__(self, bundle_id): | ||
self.__id = bundle_id | ||
|
||
def show(self): | ||
return self.__id | ||
|
||
|
||
class ServiceFactoryTest: | ||
""" | ||
Simple test service | ||
""" | ||
def get_service(self, bundle, registration): | ||
""" | ||
Provide a new service | ||
""" | ||
return Service(bundle.get_bundle_id()) | ||
|
||
|
||
@BundleActivator | ||
class ActivatorService: | ||
""" | ||
Test activator | ||
""" | ||
def __init__(self): | ||
""" | ||
Constructor | ||
""" | ||
self.context = None | ||
self.factory = None | ||
self.reg = None | ||
|
||
def start(self, context): | ||
""" | ||
Bundle started | ||
""" | ||
self.context = context | ||
|
||
# Register the service | ||
self.factory = ServiceFactoryTest() | ||
self.reg = context.register_service( | ||
SVC, self.factory, {"test": True, "answer": 0}, | ||
factory=True) | ||
|
||
def stop(self, context): | ||
""" | ||
Bundle stopped | ||
""" | ||
self.reg.unregister() | ||
self.reg = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/usr/bin/env python | ||
# -- Content-Encoding: UTF-8 -- | ||
""" | ||
Pelix framework test module. Tests the framework, bundles handling, service | ||
handling and events. | ||
:author: Thomas Calmant | ||
""" | ||
|
||
# Pelix | ||
from pelix.framework import FrameworkFactory, Bundle, BundleException, \ | ||
BundleContext, ServiceReference | ||
import pelix.constants | ||
|
||
# Standard library | ||
try: | ||
import unittest2 as unittest | ||
except ImportError: | ||
import unittest | ||
|
||
# ------------------------------------------------------------------------------ | ||
|
||
__version__ = "1.0.0" | ||
|
||
# ------------------------------------------------------------------------------ | ||
|
||
|
||
class ServicesTest(unittest.TestCase): | ||
""" | ||
Pelix services registry tests | ||
""" | ||
def setUp(self): | ||
""" | ||
Called before each test. Initiates a framework and loads the current | ||
module as the first bundle | ||
""" | ||
self.test_bundle_name = "tests.framework.service_factory_bundle" | ||
|
||
self.framework = FrameworkFactory.get_framework() | ||
self.framework.start() | ||
|
||
def tearDown(self): | ||
""" | ||
Called after each test | ||
""" | ||
self.framework.stop() | ||
FrameworkFactory.delete_framework() | ||
|
||
def test_factory(self): | ||
""" | ||
Tests the basic behaviour of a service factory | ||
""" | ||
context = self.framework.get_bundle_context() | ||
bundle = context.install_bundle(self.test_bundle_name) | ||
module = bundle.get_module() | ||
bundle.start() | ||
|
||
svc_ref = context.get_service_reference(module.SVC) | ||
svc = context.get_service(svc_ref) | ||
self.assertEqual(svc.show(), 0) | ||
context.unget_service(svc_ref) | ||
|
||
svc = context.get_service(svc_ref) | ||
self.assertEqual(svc.show(), 0) | ||
context.unget_service(svc_ref) | ||
|
||
bc = bundle.get_bundle_context() | ||
svc = bc.get_service(svc_ref) | ||
self.assertEqual(svc.show(), bundle.get_bundle_id()) | ||
bc.unget_service(svc_ref) | ||
|
||
# ------------------------------------------------------------------------------ | ||
|
||
if __name__ == "__main__": | ||
# Set logging level | ||
import logging | ||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
unittest.main() |