Browse files

Call the inspect_error middleware hook in Puller

Puller is unrelated with ServerBase (the REQ/REP base server-side class)
and the call to the inspect_error middleware hook introduced in 0.2.0
was just missing.
  • Loading branch information...
1 parent 5547843 commit b65600167785d5a744097a26c04268c2512e0f2c Louis Opter committed May 18, 2012
Showing with 50 additions and 1 deletion.
  1. +42 −0 tests/test_middleware.py
  2. +8 −1 zerorpc/core.py
View
42 tests/test_middleware.py
@@ -564,3 +564,45 @@ def echo(self, msg):
server.close()
assert middleware.called is True
+
+def test_inspect_error_middleware_puller():
+
+ class InspectErrorMiddleware(Tracer):
+ def __init__(self, barrier):
+ self.called = False
+ self._barrier = barrier
+ Tracer.__init__(self, identity='[server]')
+
+ def inspect_error(self, task_context, exc_info):
+ assert 'trace_id' in task_context
+ exc_type, exc_value, exc_traceback = exc_info
+ self.called = True
+ self._barrier.set()
+
+ class Srv(object):
+ def echo(self, msg):
+ raise RuntimeError(msg)
+
+ endpoint = random_ipc_endpoint()
+
+ barrier = gevent.event.Event()
+ middleware = InspectErrorMiddleware(barrier)
+ ctx = zerorpc.Context()
+ ctx.register_middleware(middleware)
+
+ module = Srv()
+ server = zerorpc.Puller(module, context=ctx)
+ server.bind(endpoint)
+ gevent.spawn(server.run)
+
+ client = zerorpc.Pusher()
+ client.connect(endpoint)
+
+ barrier.clear()
+ client.echo('This is a test which should call the InspectErrorMiddleware')
+ barrier.wait()
+
+ client.close()
+ server.close()
+
+ assert middleware.called is True
View
9 zerorpc/core.py
@@ -320,7 +320,14 @@ def _receiver(self):
self._methods[event.name],
*event.args)
except Exception:
- traceback.print_exc(file=sys.stderr)
+ exc_type, exc_value, exc_traceback = sys.exc_info()
+ try:
+ traceback.print_exception(exc_type, exc_value, exc_traceback,
+ file=sys.stderr)
+ self._context.middleware_inspect_error(exc_type, exc_value,
+ exc_traceback)
+ finally:
+ del exc_traceback
def run(self):
self._receiver_task = gevent.spawn(self._receiver)

0 comments on commit b656001

Please sign in to comment.