def subscribe(self, channel, callback, create_topic=False): class MockMessage(object): def __init__(self, message): self.data = message if channel not in self._messages: if create_topic: self._messages[channel] = deque() else: raise TopicNotFound("Topic {} doesn't exist".format(channel)) future = futures.Future() def create_message(): sleep(0.1) while self._messages[channel]: r = MockMessage(self._messages[channel].pop()) try: return callback(r) except Exception as exc: future.set_exception(exc) thread = threading.Thread(target=create_message) thread.start() return future
def __init__(self, processing_time, resolve_at_msg_count): self._lock = threading.Lock() self._processing_time = processing_time self._pending_ack = 0 self.max_pending_ack = 0 self.completed_calls = 0 self.seen_message_ids = [] self._resolve_at_msg_count = resolve_at_msg_count self.done_future = futures.Future()
def _future(*args, **kwargs): return futures.Future(*args, **kwargs)