Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Unregister service value error bug #345

Merged
merged 10 commits into from
Oct 27, 2020
12 changes: 10 additions & 2 deletions envisage/core_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from envisage.extension_point import ExtensionPoint
from envisage.plugin import Plugin
from envisage.service_offer import ServiceOffer
from traits.api import List, on_trait_change, Str
from traits.api import List, observe, on_trait_change, Str


class CorePlugin(Plugin):
Expand Down Expand Up @@ -138,7 +138,6 @@ def _service_offers_changed(self, event):

def start(self):
""" Start the plugin. """

# Load all contributed preferences files into the application's root
# preferences node.
self._load_preferences(self.preferences)
Expand Down Expand Up @@ -196,3 +195,12 @@ def _register_service_offer(self, service_offer):
)

return service_id

@observe("application:service_registry:unregistered")
def unregistered_updated(self, event):
""" React to services being unregistered. """

# we only want to reaect to unregistered services that were originally
# registered using the CorePlugins service_offers extension point
if event.new in self._service_ids:
self._service_ids.remove(event.new)
98 changes: 84 additions & 14 deletions envisage/tests/test_core_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,21 @@
from pkg_resources import resource_filename

# Enthought library imports.
from envisage.api import Application, Plugin
from envisage.api import Application, CorePlugin, Plugin
from envisage.api import ServiceOffer
from traits.api import Interface, List
from traits.api import (
HasTraits,
Interface,
List,
on_trait_change,
pop_exception_handler as pop_on_trait_change_handler,
push_exception_handler as push_on_trait_change_handler,
Str,
)
from traits.observation.api import (
pop_exception_handler as pop_observe_handler,
push_exception_handler as push_observe_handler,
)


# This module's package.
Expand All @@ -34,11 +46,17 @@ class TestApplication(Application):
class CorePluginTestCase(unittest.TestCase):
""" Tests for the core plugin. """

def setUp(self):
push_on_trait_change_handler(reraise_exceptions=True)
push_observe_handler(reraise_exceptions=True)

def tearDown(self):
pop_observe_handler()
aaronayres35 marked this conversation as resolved.
Show resolved Hide resolved
pop_on_trait_change_handler()

def test_service_offers(self):
""" service offers """

from envisage.api import CorePlugin

class IMyService(Interface):
pass

Expand Down Expand Up @@ -81,8 +99,6 @@ def _my_service_factory(self, **properties):
def test_dynamically_added_service_offer(self):
""" dynamically added service offer """

from envisage.api import CorePlugin

class IMyService(Interface):
pass

Expand Down Expand Up @@ -137,10 +153,6 @@ def _my_service_factory(self, **properties):
def test_preferences(self):
""" preferences """

# The core plugin is the plugin that offers the preferences extension
# point.
from envisage.api import CorePlugin

class PluginA(Plugin):
id = "A"
preferences = List(contributes_to="envisage.preferences")
Expand All @@ -162,10 +174,6 @@ def _preferences_default(self):
def test_dynamically_added_preferences(self):
""" dynamically added preferences """

# The core plugin is the plugin that offers the preferences extension
# point.
from envisage.api import CorePlugin

class PluginA(Plugin):
id = "A"
preferences = List(contributes_to="envisage.preferences")
Expand All @@ -187,3 +195,65 @@ def _preferences_default(self):

# Make sure we can get one of the preferences.
self.assertEqual("42", application.preferences.get("enthought.test.x"))

# regression test for enthought/envisage#251
def test_unregister_service_offer(self):

aaronayres35 marked this conversation as resolved.
Show resolved Hide resolved
class IJunk(Interface):
trash = Str()

class Junk(HasTraits):
trash = Str("garbage")

class PluginA(Plugin):
# The Ids of the extension points that this plugin contributes to.
SERVICE_OFFERS = "envisage.service_offers"

service_offers = List(contributes_to=SERVICE_OFFERS)

def _service_offers_default(self):

a_service_offer = ServiceOffer(
protocol=IJunk,
factory=self._create_junk_service,
)

return [a_service_offer]

def _create_junk_service(self):
""" Factory method for the 'Junk' service. """

return Junk()

@on_trait_change("application:started")
def _unregister_junk_service(self):
# only 1 service is registered so it has service_id of 1
self.application.unregister_service(1)

application = TestApplication(
plugins=[CorePlugin(), PluginA()],
)

# Run it!
return application.run()
aaronayres35 marked this conversation as resolved.
Show resolved Hide resolved

def test_unregister_service(self):
kitchoi marked this conversation as resolved.
Show resolved Hide resolved

aaronayres35 marked this conversation as resolved.
Show resolved Hide resolved
class IJunk(Interface):
trash = Str()

class Junk(HasTraits):
trash = Str("garbage")

some_junk = Junk()

application = TestApplication(
plugins=[CorePlugin()],
)

application.start()

some_junk_id = application.register_service(IJunk, some_junk)
application.unregister_service(some_junk_id)

application.stop()