1
# Copyright 2011 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Things that help with testing of longpoll."""
8
"capture_longpoll_emissions",
11
from collections import namedtuple
12
from contextlib import contextmanager
13
from functools import partial
15
from lp.services.longpoll.adapters import event
18
LongPollEventRecord = namedtuple(
19
"LongPollEventRecord", ("event_key", "data"))
23
"""A test double for instances of `RabbitRoutingKey`.
25
Saves messages as `LongPollEventRecord` tuples to a log.
27
:param log: A callable accepting a single `LongPollEventRecord`.
28
:param routing_key: See `RabbitRoutingKey.__init__`.
31
def __init__(self, log, routing_key):
33
self.routing_key = routing_key
36
record = LongPollEventRecord(self.routing_key, data)
41
def capture_longpoll_emissions():
42
"""Capture longpoll emissions while this context is in force.
44
This returns a list in which `LongPollEventRecord` tuples will be
45
recorded, in the order they're emitted.
47
Note that normal event emission is *suppressed globally* while this
48
context is in force; *all* events will be stored in the log.
51
original_router_factory = event.router_factory
52
event.router_factory = partial(LoggingRouter, log.append)
56
event.router_factory = original_router_factory