def test_mock_read(self): conn = Mock(spec=imaplib.IMAP4_SSL) imap = IMAPConsumer(("imap.gmail.com", 993), "username", 'password', VoomBus(), on_complete=self._on_complete, connection_type=Mock(return_value=conn)) bus = imap.bus msgs = [] bus.subscribe(IMAPMessageDownloaded, msgs.append) bus.subscribe(IMAPMessageDownloaded, self._print, priority=BusPriority.LOW_PRIORITY**3) with open(os.path.join(os.path.dirname(__file__), 'msg.txt')) as f: mime_text = f.read() conn.search.return_value = ['OK', ['1 2']] conn.fetch.return_value = ['OK', [(None, mime_text)]] conn.store.return_value = ['OK', None] conn.close.side_effect = Exception imap.run() assert len(msgs) == 2 m = msgs[0].mime_message expected = "Ned Freed <*****@*****.**>" assert m['To'].strip() == expected, "'%s' '%s'" % (m['To'], expected)
def test_send_other_thread(self): self.rlock = threading.RLock() self.rlock.acquire() def send_on_ready(): LOG.info("waiting for lock...") with self.rlock: LOG.info("lock acquired, sending") properties = pika.BasicProperties( content_type='application/json', reply_to=g.return_queue.queue) self.g.send([range(0, 10)], properties, exchange='', routing_key=work.queue) LOG.info("lock released") t = threading.Thread(target=send_on_ready) t.daemon = True t.start() work = AMQPQueueDescriptor("test_multithread", declare=True, exclusive=False, auto_delete=True) g = AMQPGateway(work.queue, pika.ConnectionParameters(host='localhost'), [work], VoomBus(), ContentCodecRegistry(JSONMessageCodec())) self.g = g bus = g.bus bus.raise_errors = True self.msgs = [] @receiver(AMQPDataReceived) def receives(msg): assert isinstance(msg, AMQPDataReceived) self.msgs.append(msg) bus.publish(GatewayShutdownCmd()) @receiver(AMQPGatewayReady) def on_ready(msg): LOG.info("releasing...") self.rlock.release() bus.register(receives) bus.register(on_ready) g.run() assert len(self.msgs) == 1 msg = self.msgs.pop(0) assert isinstance(msg, AMQPDataReceived) assert msg.headers['From'] == g.return_queue.queue assert msg.headers['Content-Type'] == 'application/json' assert msg.headers['Reply-To'] == g.return_queue.queue assert msg.headers['Routing-Key'] == work.queue assert msg.messages == [range(10)]
def test_nesting(self): bus = VoomBus() with bus.transaction() as (nested, state): assert not nested assert state is not None assert isinstance(state, TrxState) with bus.transaction() as (nested2, state2): assert nested2 assert state2 == state
def test_send_on_exit(self): bus = VoomBus() self.msgs = [] bus.subscribe(bus.ALL, self.msgs.append) with bus.transaction() as (nested, state): with bus.using(dict(a=1)): bus.publish(1) assert not self.msgs assert isinstance(state, TrxState) assert not state.is_queue_empty() assert self.msgs == [1] assert state.is_queue_empty()
def test_send_on_error(self): bus = VoomBus() self.msgs = [] bus.subscribe(bus.ALL, self.msgs.append) with nose.tools.assert_raises(ValueError): #@UndefinedVariable with bus.transaction() as (nested, state): assert not nested with bus.using(dict(a=1)): assert bus.session == dict(a=1), bus.session bus.publish(1) assert not self.msgs int("a") with bus.using(dict(a=1)): bus.publish(1) assert self.msgs == [1] assert state.is_queue_empty() assert state.session == dict(a=1), state.session
def test_read_from_gmail(self): username = os.environ.get('GMAIL_USER') password = os.environ.get('GMAIL_PASS') if not username or not password: warnings.warn( "Skipping read from gmail; to enable, set GMAIL_PASS and GMAIL_USER in the environment before running the test." ) raise SkipTest imap = IMAPConsumer(("imap.gmail.com", 993), username, password, VoomBus(), on_complete=self._on_complete) bus = imap.bus bus.subscribe(IMAPMessageDownloaded, self._print, priority=BusPriority.LOW_PRIORITY**3) imap.run()
def test2(self): bus = VoomBus() data1 = {1: 2} data2 = {'a': True} def func3(): bus.publish("1") def func2(): with bus.using(data2): func3() def func1(): with bus.using(data1): func2() session = {} bus.subscribe(bus.ALL, lambda _: session.update(bus.session)) func1() data1.update(data2) assert session == data1, session
import sys, os sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) # make this file executable ### BEGIN from voom.bus import VoomBus from voom.priorities import BusPriority bus = VoomBus() bus.subscribe(int, lambda msg: sys.stdout.write("I see %d\n" % msg)) bus.subscribe(int, lambda msg: sys.stdout.write("squared %d\n" % msg**2), priority=BusPriority.LOW_PRIORITY) bus.subscribe(str, lambda msg: bus.publish(int(msg[::-1]))) bus.publish(101) bus.publish("102")
def setUp(self): self.bus = VoomBus() self.forward = None
def test_1(self): work = AMQPQueueDescriptor("test_round_trip", declare=True, exclusive=False, auto_delete=True) g = AMQPGateway(work.queue, pika.ConnectionParameters(host='localhost'), [work], VoomBus(), ContentCodecRegistry(JSONMessageCodec())) bus = g.bus bus.raise_errors = True self.msgs = [] @receiver(AMQPDataReceived) def receives(msg): assert isinstance(msg, AMQPDataReceived) self.msgs.append(msg) if len(self.msgs) == 1: properties = pika.BasicProperties( content_type='application/json', content_encoding='zip', reply_to=g.return_queue.queue) g.send([range(0, 100)], properties, exchange='', routing_key=g.return_queue.queue) return if len(self.msgs) == 2: assert bus.session[SessionKeys.RESPONDER] #print bus.session.keys() bus.reply([msg.messages[0]]) return bus.publish(GatewayShutdownCmd()) @receiver(AMQPGatewayReady) def on_ready(msg): properties = pika.BasicProperties(content_type='application/json', reply_to=g.return_queue.queue) g.send([range(0, 10)], properties, exchange='', routing_key=work.queue) bus.register(receives) bus.register(on_ready) g.run() assert len(self.msgs) == 3 msg = self.msgs.pop(0) assert isinstance(msg, AMQPDataReceived) assert msg.headers['From'] == g.return_queue.queue assert msg.headers['Content-Type'] == 'application/json' assert msg.headers['Reply-To'] == g.return_queue.queue assert msg.headers['Routing-Key'] == work.queue assert msg.messages == [range(10)] for msg in self.msgs: assert isinstance(msg, AMQPDataReceived) assert msg.headers['From'] == g.return_queue.queue assert msg.headers['Content-Type'] == 'application/json' assert msg.headers['Content-Encoding'] == 'zip' assert msg.headers['Reply-To'] == g.return_queue.queue assert msg.headers[ 'Routing-Key'] == g.return_queue.queue, msg.headers assert msg.messages == [range(100)], msg.messages
def setUp(self): self.bus = VoomBus()
def setUp(self): self.bus = VoomBus(raise_errors=True) self.context2 = 0
def test1(self): bus = VoomBus() data = {1: 2} with bus.using(data): assert bus._trx_proxy.session_future == data, bus._session_data.data assert bus._trx_proxy.session_future is None, bus._session_data.data
def setUp(self): self.bus = VoomBus(verbose=True)