def test_publish_acknowledged(self): handler = ApplicationSession() MockTransport(handler) publication = yield handler.publish(u"com.myapp.topic1", options=types.PublishOptions(acknowledge=True)) self.assertTrue(type(publication.id) in (int, long)) publication = yield handler.publish( u"com.myapp.topic1", 1, 2, 3, options=types.PublishOptions(acknowledge=True) ) self.assertTrue(type(publication.id) in (int, long)) publication = yield handler.publish( u"com.myapp.topic1", 1, 2, 3, foo=23, bar="hello", options=types.PublishOptions(acknowledge=True) ) self.assertTrue(type(publication.id) in (int, long)) publication = yield handler.publish( u"com.myapp.topic1", options=types.PublishOptions(exclude_me=False, acknowledge=True) ) self.assertTrue(type(publication.id) in (int, long)) publication = yield handler.publish( u"com.myapp.topic1", 1, 2, 3, foo=23, bar="hello", options=types.PublishOptions(exclude_me=False, exclude=[100, 200, 300], acknowledge=True), ) self.assertTrue(type(publication.id) in (int, long))
def test_publish(self): handler = ApplicationSession() MockTransport(handler) publication = yield handler.publish(u'com.myapp.topic1') self.assertEqual(publication, None) publication = yield handler.publish(u'com.myapp.topic1', 1, 2, 3) self.assertEqual(publication, None) publication = yield handler.publish(u'com.myapp.topic1', 1, 2, 3, foo=23, bar='hello') self.assertEqual(publication, None) publication = yield handler.publish( u'com.myapp.topic1', options=types.PublishOptions(exclude_me=False)) self.assertEqual(publication, None) publication = yield handler.publish(u'com.myapp.topic1', 1, 2, 3, foo=23, bar='hello', options=types.PublishOptions( exclude_me=False, exclude=[100, 200, 300])) self.assertEqual(publication, None)
def test_publish(self): handler = ApplicationSession() MockTransport(handler) publication = yield handler.publish(u"com.myapp.topic1") self.assertEqual(publication, None) publication = yield handler.publish(u"com.myapp.topic1", 1, 2, 3) self.assertEqual(publication, None) publication = yield handler.publish(u"com.myapp.topic1", 1, 2, 3, foo=23, bar="hello") self.assertEqual(publication, None) publication = yield handler.publish(u"com.myapp.topic1", options=types.PublishOptions(exclude_me=False)) self.assertEqual(publication, None) publication = yield handler.publish( u"com.myapp.topic1", 1, 2, 3, foo=23, bar="hello", options=types.PublishOptions(exclude_me=False, exclude=[100, 200, 300]), ) self.assertEqual(publication, None)
def test_publish_undefined_exception(self): handler = ApplicationSession() MockTransport(handler) options = types.PublishOptions(acknowledge=True) yield self.assertFailure(handler.publish(u'de.myapp.topic1', options=options), ApplicationError) yield self.assertFailure(handler.publish(u'', options=options), ApplicationError)
def test_publish_defined_exception(self): handler = ApplicationSession() MockTransport(handler) options = types.PublishOptions(acknowledge=True) handler.define(NotAuthorized) yield self.assertFailure(handler.publish(u'de.myapp.topic1', options=options), NotAuthorized) handler.define(InvalidUri) yield self.assertFailure(handler.publish(u'', options=options), InvalidUri)
def test_publish_callback_exception(self): """ Ensure we handle an exception from the user code. """ handler = ApplicationSession() MockTransport(handler) error_instance = RuntimeError("we have a problem") got_err_d = Deferred() def observer(e, msg): if error_instance == e.value: got_err_d.callback(True) handler.onUserError = observer def boom(): raise error_instance sub = yield handler.subscribe(boom, u'com.myapp.topic1') # MockTransport gives us the ack reply and then we do our # own event message publish = yield handler.publish( u'com.myapp.topic1', options=types.PublishOptions(acknowledge=True, exclude_me=False), ) msg = message.Event(sub.id, publish.id) handler.onMessage(msg) # we know it worked if our observer worked and did # .callback on our Deferred above. self.assertTrue(got_err_d.called)
def publish(self, topic, *args, **kwargs): # WAMP meta events published over the service session are published on the # service session itself (the first in the list of sessions to expose), and potentially # more sessions - namely the management session on the local node router dl = [] for session, prefix, replace_dots in self._expose_on_sessions: translated_topic = topic # we cannot subscribe in CFC to topics of the form # crossbarfabriccenter.node.<node_id>.worker.<worker_id>.realm.<realm_id>.root.*, # where * is an arbitrary suffix including dots, eg "wamp.session.on_join" # # to work around that, we replace the "."s in the suffix with "-", and reverse that # in CFC if replace_dots: translated_topic = translated_topic.replace(u'.', replace_dots) if prefix: translated_topic = u'{}{}'.format(prefix, translated_topic) self.log.debug('RouterServiceSession.publish("{topic}") -> "{translated_topic}" on "{realm}"', topic=topic, translated_topic=translated_topic, realm=session._realm) dl.append(ApplicationSession.publish(session, translated_topic, *args, **kwargs)) # to keep the interface of ApplicationSession.publish, we only return the first # publish return (that is the return from publishing to the user router-realm) if len(dl) > 0: return dl[0]
def test_double_subscribe(self): handler = ApplicationSession() MockTransport(handler) event0 = Deferred() event1 = Deferred() subscription0 = yield handler.subscribe( lambda: event0.callback(42), u'com.myapp.topic1') subscription1 = yield handler.subscribe( lambda: event1.callback('foo'), u'com.myapp.topic1') # same topic, same ID self.assertTrue(subscription0.id == subscription1.id) # do a publish (MockTransport fakes the acknowledgement # message) and then do an actual publish event. The IDs # are the same, so we just do one Event. publish = yield handler.publish( u'com.myapp.topic1', options=types.PublishOptions(acknowledge=True, exclude_me=False), ) handler.onMessage(message.Event(subscription0.id, publish.id)) # ensure we actually got both callbacks self.assertTrue(event0.called, "Missing callback") self.assertTrue(event1.called, "Missing callback")
def test_publish(self): handler = ApplicationSession() transport = MockTransport(handler) publication = yield handler.publish('com.myapp.topic1') self.assertEqual(publication, None) publication = yield handler.publish('com.myapp.topic1', 1, 2, 3) self.assertEqual(publication, None) publication = yield handler.publish('com.myapp.topic1', 1, 2, 3, foo = 23, bar = 'hello') self.assertEqual(publication, None) publication = yield handler.publish('com.myapp.topic1', options = types.PublishOptions(excludeMe = False)) self.assertEqual(publication, None) publication = yield handler.publish('com.myapp.topic1', 1, 2, 3, foo = 23, bar = 'hello', options = types.PublishOptions(excludeMe = False, exclude = [100, 200, 300])) self.assertEqual(publication, None)
def test_publish_acknowledged(self): handler = ApplicationSession() MockTransport(handler) publication = yield handler.publish(u'com.myapp.topic1', options=types.PublishOptions(acknowledge=True)) self.assertTrue(type(publication.id) in (int, long)) publication = yield handler.publish(u'com.myapp.topic1', 1, 2, 3, options=types.PublishOptions(acknowledge=True)) self.assertTrue(type(publication.id) in (int, long)) publication = yield handler.publish(u'com.myapp.topic1', 1, 2, 3, foo=23, bar='hello', options=types.PublishOptions(acknowledge=True)) self.assertTrue(type(publication.id) in (int, long)) publication = yield handler.publish(u'com.myapp.topic1', options=types.PublishOptions(exclude_me=False, acknowledge=True)) self.assertTrue(type(publication.id) in (int, long)) publication = yield handler.publish(u'com.myapp.topic1', 1, 2, 3, foo=23, bar='hello', options=types.PublishOptions(exclude_me=False, exclude=[100, 200, 300], acknowledge=True)) self.assertTrue(type(publication.id) in (int, long))
def test_double_subscribe_double_unsubscribe(self): ''' If we subscribe twice, and unsubscribe twice, we should then get an Unsubscribed message. ''' handler = ApplicationSession() MockTransport(handler) # monkey-patch ApplicationSession to ensure we get our message unsubscribed_d = Deferred() def onMessage(msg): if isinstance(msg, message.Unsubscribed): unsubscribed_d.callback(msg) return ApplicationSession.onMessage(handler, msg) handler.onMessage = onMessage event0 = Deferred() event1 = Deferred() subscription0 = yield handler.subscribe( lambda: event0.callback(42), u'com.myapp.topic1') subscription1 = yield handler.subscribe( lambda: event1.callback('foo'), u'com.myapp.topic1') self.assertTrue(subscription0.id == subscription1.id) yield subscription0.unsubscribe() yield subscription1.unsubscribe() # after the second unsubscribe, we should have gotten an # Unsubscribed message assert unsubscribed_d.called # do a publish (MockTransport fakes the acknowledgement # message) and then do an actual publish event. Sending # the Event should be an error, as we have no # subscriptions left. publish = yield handler.publish( u'com.myapp.topic1', options=types.PublishOptions(acknowledge=True, exclude_me=False), ) try: handler.onMessage(message.Event(subscription0.id, publish.id)) self.fail("Expected ProtocolError") except ProtocolError: pass # since we unsubscribed the second event handler, we # should NOT have called its callback self.assertTrue(not event0.called, "First callback fired.") self.assertTrue(not event1.called, "Second callback fired.")
def test_double_subscribe_errors(self): """ Test various error-conditions when we try to add a second subscription-handler (its signature must match any existing handlers). """ handler = ApplicationSession() MockTransport(handler) event0 = Deferred() event1 = Deferred() def second(*args, **kw): # our EventDetails should have been passed as the # "boom" kwarg; see "details_arg=" below self.assertTrue('boom' in kw) self.assertTrue(isinstance(kw['boom'], types.EventDetails)) event1.callback(args) subscription0 = yield handler.subscribe( lambda arg: event0.callback(arg), u'com.myapp.topic1') subscription1 = yield handler.subscribe( second, u'com.myapp.topic1', types.SubscribeOptions(details_arg='boom'), ) # same topic, same ID self.assertTrue(subscription0.id == subscription1.id) # MockTransport gives us the ack reply and then we do our # own event message. publish = yield handler.publish( u'com.myapp.topic1', options=types.PublishOptions(acknowledge=True, exclude_me=False), ) # note that the protocol serializer converts all sequences # to lists, so we pass "args" as a list, not a tuple on # purpose. handler.onMessage( message.Event(subscription0.id, publish.id, args=['arg0'])) # each callback should have gotten called, each with its # own args (we check the correct kwarg in second() above) self.assertTrue(event0.called) self.assertTrue(event1.called) self.assertEqual(event0.result, 'arg0') self.assertEqual(event1.result, ('arg0', ))
def publish(self, topic, *args, **kwargs): # WAMP meta events published over the service session are published on the # service session itself (the first in the list of sessions to expose), and potentially # more sessions - namely the management session on the local node router dl = [] for session, prefix in self._expose_on_sessions: if prefix: _topic = u'{}{}'.format(prefix, topic) else: _topic = topic dl.append( ApplicationSession.publish(session, _topic, *args, **kwargs)) # to keep the interface of ApplicationSession.publish, we only return the first # publish return (that is the return from publishing to the user router-realm) if len(dl) > 0: return dl[0]
def test_double_subscribe_errors(self): """ Test various error-conditions when we try to add a second subscription-handler (its signature must match any existing handlers). """ handler = ApplicationSession() MockTransport(handler) event0 = Deferred() event1 = Deferred() def second(*args, **kw): # our EventDetails should have been passed as the # "boom" kwarg; see "details_arg=" below self.assertTrue('boom' in kw) self.assertTrue(isinstance(kw['boom'], types.EventDetails)) event1.callback(args) subscription0 = yield handler.subscribe( lambda arg: event0.callback(arg), u'com.myapp.topic1') subscription1 = yield handler.subscribe( second, u'com.myapp.topic1', types.SubscribeOptions(details_arg='boom'), ) # same topic, same ID self.assertTrue(subscription0.id == subscription1.id) # MockTransport gives us the ack reply and then we do our # own event message. publish = yield handler.publish( u'com.myapp.topic1', options=types.PublishOptions(acknowledge=True, exclude_me=False), ) # note that the protocol serializer converts all sequences # to lists, so we pass "args" as a list, not a tuple on # purpose. handler.onMessage( message.Event(subscription0.id, publish.id, args=['arg0'])) # each callback should have gotten called, each with its # own args (we check the correct kwarg in second() above) self.assertTrue(event0.called) self.assertTrue(event1.called) self.assertEqual(event0.result, 'arg0') self.assertEqual(event1.result, ('arg0',))
def test_publish_callback_exception(self): """ Ensure we handle an exception from the user code. """ handler = ApplicationSession() MockTransport(handler) error_instance = RuntimeError("we have a problem") got_err_d = Deferred() def observer(kw): if kw['isError'] and 'failure' in kw: fail = kw['failure'] fail.trap(RuntimeError) if error_instance == fail.value: got_err_d.callback(True) log.addObserver(observer) def boom(): raise error_instance try: sub = yield handler.subscribe(boom, u'com.myapp.topic1') # MockTransport gives us the ack reply and then we do our # own event message publish = yield handler.publish( u'com.myapp.topic1', options=types.PublishOptions(acknowledge=True, exclude_me=False), ) msg = message.Event(sub.id, publish.id) handler.onMessage(msg) # we know it worked if our observer worked and did # .callback on our Deferred above. self.assertTrue(got_err_d.called) # ...otherwise trial will fail the test anyway self.flushLoggedErrors() finally: log.removeObserver(observer)
def test_double_subscribe_single_unsubscribe(self): ''' Make sure we correctly deal with unsubscribing one of our handlers from the same topic. ''' handler = ApplicationSession() MockTransport(handler) # monkey-patch ApplicationSession to ensure we DO NOT get # an Unsubscribed message -- since we only unsubscribe # from ONE of our handlers for com.myapp.topic1 def onMessage(msg): assert not isinstance(msg, message.Unsubscribed) return ApplicationSession.onMessage(handler, msg) handler.onMessage = onMessage event0 = Deferred() event1 = Deferred() subscription0 = yield handler.subscribe( lambda: event0.callback(42), u'com.myapp.topic1') subscription1 = yield handler.subscribe( lambda: event1.callback('foo'), u'com.myapp.topic1') self.assertTrue(subscription0.id == subscription1.id) yield subscription1.unsubscribe() # do a publish (MockTransport fakes the acknowledgement # message) and then do an actual publish event. Note the # IDs are the same, so there's only one Event. publish = yield handler.publish( u'com.myapp.topic1', options=types.PublishOptions(acknowledge=True, exclude_me=False), ) handler.onMessage(message.Event(subscription0.id, publish.id)) # since we unsubscribed the second event handler, we # should NOT have called its callback self.assertTrue(event0.called, "Missing callback") self.assertTrue(not event1.called, "Second callback fired.")
def test_publish_outstanding_errors_async_errback(self): handler = ApplicationSession() MockTransport(handler) error_d = Deferred() # this publish will "hang" because 'noreply.' URI is # handled specially in MockTransport; so this request will # be outstanding publication_d = handler.publish( u'noreply.foo', options=types.PublishOptions(acknowledge=True), ) # further, we add an errback that does some arbitrary async work got_errors = [] def errback(fail): got_errors.append(fail) return error_d publication_d.addErrback(errback) # "leave" the session, which should trigger errbacks on # all outstanding requests. details = types.CloseDetails(reason=u'testing', message=u'how are you?') handler.onLeave(details) # since our errback is async, onLeave should not have # completed yet but we should have already failed the # publication self.assertEqual(1, len(got_errors)) # ...now let the async errback continue by completing the # Deferred we returned in our errback (could be fail or # success, shoudln't matter) error_d.callback(None) # ensure we (now) get our errback try: yield publication_d except ApplicationError as e: self.assertEqual(u'testing', e.error) self.assertEqual(u'how are you?', e.message)
def test_publish_outstanding_errors(self): handler = ApplicationSession() MockTransport(handler) # this publish will "hang" because 'noreply.' URI is # handled specially in MockTransport; so this request will # be outstanding publication = handler.publish( u'noreply.foo', options=types.PublishOptions(acknowledge=True), ) # "leave" the session, which should trigger errbacks on # all outstanding requests. details = types.CloseDetails(reason=u'testing', message=u'how are you?') yield handler.onLeave(details) # ensure we got our errback try: yield publication except ApplicationError as e: self.assertEqual(u'testing', e.error) self.assertEqual(u'how are you?', e.message)
def test_publish_outstanding_errors(self): handler = ApplicationSession() MockTransport(handler) # this publish will "hang" because 'noreply.' URI is # handled specially in MockTransport; so this request will # be outstanding publication = handler.publish( 'noreply.foo', options=types.PublishOptions(acknowledge=True), ) # "leave" the session, which should trigger errbacks on # all outstanding requests. details = types.CloseDetails(reason='testing', message='how are you?') yield handler.onLeave(details) # ensure we got our errback try: yield publication except ApplicationError as e: self.assertEqual('testing', e.error) self.assertEqual('how are you?', e.message)
def stockPublish(self, topic, *args, **kwargs): return ApplicationSession.publish(self, u'' + topic, *args, **kwargs)
def stockPublish(self, topic, *args, **kwargs): return ApplicationSession.publish(self, u''+topic, *args, **kwargs)
def publish(self, topic, *args, **kwargs): # kwargs['options'] = PublishOptions(disclose_me=True) args = (self.pdid, ) + args topic = _prepend(self.pdid, topic) # out.info('cxbr: (%s) publish (%s)' % (self.pdid, topic,)) return ApplicationSession.publish(self, topic, *args, **kwargs)
def absPublish(self, topic, *args, **kwargs): #out.info('riff: (%s) publish (%s)' % (self.pdid, topic,)) return ApplicationSession.publish(self, u'' + topic, *args, **kwargs)
def stockPublish(self, pdid, topic, *args, **kwargs): out.info('cxbr: (%s) publish (%s)' % (self.pdid, topic,)) return ApplicationSession.publish(self, topic, *args, **kwargs)
def publish(self, pdid, topic, *args, **kwargs): # kwargs['options'] = PublishOptions(disclose_me=True) args = (self.pdid,) + args topic = _prepend(pdid, topic) #out.info('riff: (%s) publish (%s)' % (self.pdid, topic,)) return ApplicationSession.publish(self, topic, *args, **kwargs)
def publish(self, topic, *args, **kwargs): self.LOGGER.debug("PUB-fired") return ApplicationSession.publish(self, topic, *args, **kwargs)