コード例 #1
0
 def test_adapter(self):
     request = LaunchpadTestRequest()
     subscriber = ILongPollSubscriber(request)
     self.assertIsInstance(subscriber, LongPollApplicationRequestSubscriber)
     # A difference subscriber is returned on subsequent adaptions, but it
     # has the same subscribe_key.
     subscriber2 = ILongPollSubscriber(request)
     self.assertIsNot(subscriber, subscriber2)
     self.assertEqual(subscriber.subscribe_key, subscriber2.subscribe_key)
コード例 #2
0
 def test_subscribe_queue(self):
     # LongPollApplicationRequestSubscriber.subscribe() creates a new queue
     # with a new unique name that is bound to the event's event_key.
     request = LaunchpadTestRequest()
     event = FakeEvent()
     subscriber = ILongPollSubscriber(request)
     subscriber.subscribe(event)
     message = '{"hello": 1234}'
     session = getUtility(IMessageSession)
     routing_key = session.getProducer(event.event_key)
     routing_key.send(message)
     session.flush()
     subscribe_queue = session.getConsumer(subscriber.subscribe_key)
     self.assertEqual(message, subscribe_queue.receive(timeout=5))
コード例 #3
0
 def test_subscribe_queue(self):
     # LongPollApplicationRequestSubscriber.subscribe() creates a new queue
     # with a new unique name that is bound to the event's event_key.
     request = LaunchpadTestRequest()
     event = FakeEvent()
     subscriber = ILongPollSubscriber(request)
     subscriber.subscribe(event)
     message = '{"hello": 1234}'
     session = getUtility(IMessageSession)
     routing_key = session.getProducer(event.event_key)
     routing_key.send(message)
     session.flush()
     subscribe_queue = session.getConsumer(subscriber.subscribe_key)
     self.assertEqual(
         message, subscribe_queue.receive(timeout=5))
コード例 #4
0
def subscribe(target, event_name=u"", request=None):
    """Convenience method to subscribe the current request.

    :param target: Something that can be adapted to `ILongPollEvent`.
    :param event_name: The name of the event to subscribe to. This is used to
        look up a named adapter from `target` to `ILongPollEvent`.
    :param request: The request for which to get an `ILongPollSubscriber`. It
        a request is not specified the currently active request is used.
    :return: The `ILongPollEvent` that has been subscribed to.
    """
    event = getAdapter(target, ILongPollEvent, name=event_name)
    if request is None:
        request = get_current_browser_request()
    subscriber = ILongPollSubscriber(request)
    subscriber.subscribe(event)
    return event
コード例 #5
0
def subscribe(target, event_name=u"", request=None):
    """Convenience method to subscribe the current request.

    :param target: Something that can be adapted to `ILongPollEvent`.
    :param event_name: The name of the event to subscribe to. This is used to
        look up a named adapter from `target` to `ILongPollEvent`.
    :param request: The request for which to get an `ILongPollSubscriber`. It
        a request is not specified the currently active request is used.
    :return: The `ILongPollEvent` that has been subscribed to.
    """
    event = getAdapter(target, ILongPollEvent, name=event_name)
    if request is None:
        request = get_current_browser_request()
    subscriber = ILongPollSubscriber(request)
    subscriber.subscribe(event)
    return event
コード例 #6
0
 def test_longpoll_uri_config(self):
     # The JSON cache contains config.txlongpoll.uri.
     self.pushConfig("txlongpoll", uri="/+longpoll/")
     request = LaunchpadTestRequest()
     cache = IJSONRequestCache(request)
     ILongPollSubscriber(request).subscribe(FakeEvent())
     self.assertEqual('/+longpoll/', cache.objects["longpoll"]["uri"])
コード例 #7
0
 def test_json_cache_not_populated_on_init(self):
     # LongPollApplicationRequestSubscriber does not put the name of the
     # new queue into the JSON cache.
     request = LaunchpadTestRequest()
     cache = IJSONRequestCache(request)
     self.assertThat(cache.objects, Not(Contains("longpoll")))
     ILongPollSubscriber(request)
     self.assertThat(cache.objects, Not(Contains("longpoll")))
コード例 #8
0
 def test_json_cache_populated_on_subscribe(self):
     # To aid with debugging the event_key of subscriptions are added to
     # the JSON cache.
     request = LaunchpadTestRequest()
     cache = IJSONRequestCache(request)
     event1 = FakeEvent()
     ILongPollSubscriber(request).subscribe(event1)  # Side-effects!
     self.assertThat(cache.objects, Contains("longpoll"))
     self.assertThat(cache.objects["longpoll"], Contains("key"))
     self.assertThat(cache.objects["longpoll"], Contains("subscriptions"))
     self.assertEqual([event1.event_key],
                      cache.objects["longpoll"]["subscriptions"])
     # More events can be subscribed.
     event2 = FakeEvent()
     ILongPollSubscriber(request).subscribe(event2)
     self.assertEqual([event1.event_key, event2.event_key],
                      cache.objects["longpoll"]["subscriptions"])
コード例 #9
0
 def test_subscribe(self):
     # subscribe() gets the ILongPollEvent for the given target and the
     # ILongPollSubscriber for the given request (or the current request is
     # discovered). It subscribes the latter to the event, then returns the
     # event.
     session = getUtility(IMessageSession)
     request = LaunchpadTestRequest()
     an_object = FakeObject(12345)
     with ZopeAdapterFixture(FakeEvent):
         event = subscribe(an_object, request=request)
     self.assertIsInstance(event, FakeEvent)
     self.assertEqual("event-key-12345", event.event_key)
     session.flush()
     # Emitting an event-key-12345 event will put something on the
     # subscriber's queue.
     event_data = {"1234": 5678}
     event.emit(**event_data)
     subscriber = ILongPollSubscriber(request)
     subscribe_queue = session.getConsumer(subscriber.subscribe_key)
     message = subscribe_queue.receive(timeout=5)
     self.assertEqual(event_data, message)