Permalink
Browse files

Add NoSuchServiceName for eponymous errors.

  • Loading branch information...
rduplain committed Mar 20, 2012
1 parent 3d9059e commit c3d9e1e18397b27040b68d93459df8d63923f231
Showing with 33 additions and 4 deletions.
  1. +17 −2 tests.py
  2. +16 −2 wsgi_party.py
View
@@ -23,9 +23,9 @@ def test_ask_around(self):
class TestWSGIParty(unittest.TestCase):
- def _makeOne(self, app, invites=()):
+ def _makeOne(self, app, invites=(), ignore_missing_services=False):
from wsgi_party import WSGIParty
- return WSGIParty(app, invites)
+ return WSGIParty(app, invites, ignore_missing_services)
def test_ctor_calls_send_invitations(self):
app = DummyWSGIApp()
@@ -133,6 +133,21 @@ def handler2(payload):
self.assertEqual(L, ['payload', 'payload'])
self.assertEqual(result, ['result'])
+ def test_ask_around_no_handler(self):
+ from wsgi_party import NoSuchServiceName
+ app = DummyWSGIApp()
+ inst = self._makeOne(app)
+ self.assertRaises(NoSuchServiceName, inst.ask_around, 'unlucky', None)
+
+ def test_ask_around_no_handler_ignored(self):
+ from wsgi_party import NoSuchServiceName
+ app = DummyWSGIApp()
+ inst = self._makeOne(app, ignore_missing_services=True)
+ try:
+ self.assertEqual(inst.ask_around('who_cares', None), [])
+ except NoSuchServiceName:
+ self.fail('NoSuchServiceName was not suppressed as requested.')
+
class DummyOperator(object):
def __init__(self, handlers=()):
View
@@ -17,6 +17,10 @@ class HighAndDry(PartylineException):
"""A handler raises this when it does not have a response; skip it."""
+class NoSuchServiceName(PartylineException):
+ """Raised when no handlers are registered for a requested service name."""
+
+
class PartylineOperator(object):
"""Expose an API for connecting a handler to the WSGI partyline.
@@ -56,13 +60,16 @@ class WSGIParty(object):
#: Class to use as the partyline operator, for connecting handlers.
operator_class = PartylineOperator
- def __init__(self, application, invites=()):
+ def __init__(self, application, invites=(), ignore_missing_services=False):
#: WSGIParty's wrapped WSGI application.
self.application = application
#: A dict of service name => handler mappings.
self.handlers = {}
+ #: If True, :class:`NoSuchServiceName` errors are suppressed.
+ self.ignore_missing_services = ignore_missing_services
+
self.send_invitations(invites)
def __call__(self, environ, start_response):
@@ -87,7 +94,14 @@ def ask_around(self, service_name, payload, operator=None):
so that partyline applications do not call themselves.
"""
answers = []
- for handler in self.handlers[service_name]:
+ try:
+ service_handlers = self.handlers[service_name]
+ except KeyError:
+ if not self.ignore_missing_services:
+ raise NoSuchServiceName('No handler is registered for %r.' %
+ repr(service_name))
+ service_handlers = []
+ for handler in service_handlers:
if operator is not None and handler in operator.handlers:
# Skip handlers on the same operator, ask *others* for answer.
continue

0 comments on commit c3d9e1e

Please sign in to comment.