Esempio n. 1
0
    def test_concurrent(self):
        bus = VoomBus()
        bus.loader = functools.partial(self.load, 0.1)
        self.assertEqual(self.loaded, None)

        def spawn():
            t = threading.Thread(target=bus.publish, args=(0,))
            t.start()

        spawn()
        spawn()
        time.sleep(.2)
        self.assertEqual(self.loaded, 1)
        bus.publish(0)
        self.assertEqual(self.loaded, 1)
Esempio n. 2
0
    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)
Esempio n. 3
0
    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)]
Esempio n. 4
0
    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
Esempio n. 5
0
class TestHeaders(unittest.TestCase):
    def setUp(self):
        self.bus = VoomBus()

    def test_arbitrary_headers(self):
        uni = u'\u014b'
        r = Session(**{'foo': 1, uni: 2})
        assert r['foo'] == 1
        assert r[uni] == 2
        assert uni in r
        assert r.get('bar') == None
        assert 'bar' not in r

    def test_unicode(self):
        uni = u'\u014b'
        r = Session(**{'foo': 1, uni: 2})
        unicode(r)
        repr(r)
        print r

    def test_abort(self):
        self.bus.resetConfig()
        self.msg = None

        @receiver(str)
        def aborter(msg):
            if msg == "cancel":
                raise AbortProcessing()

        @receiver(str)
        def not_aborter(msg):
            self.msg = msg

        self.bus.raise_errors = True
        self.bus.register(aborter)
        self.bus.register(not_aborter, priority=BusPriority.LOW_PRIORITY)

        self.bus.publish("foo")
        assert self.msg == "foo"
        self.msg = None
        self.bus.publish("cancel")
        assert self.msg == None
Esempio n. 6
0
class TestHeaders(unittest.TestCase):
    def setUp(self):
        self.bus = VoomBus()

    def test_arbitrary_headers(self):
        uni = u'\u014b'
        r = ChainedDict(**{'foo': 1, uni: 2})
        assert r['foo'] == 1
        assert r[uni] == 2
        assert uni in r
        assert r.get('bar') == None
        assert 'bar' not in r

    def test_unicode(self):
        uni = u'\u014b'
        r = ChainedDict(**{'foo': 1, uni: 2})
        unicode(r)
        repr(r)
        print r

    def test_abort(self):
        self.bus.resetConfig()
        self.msg = None

        @receiver(str)
        def aborter(msg):
            if msg == "cancel":
                raise AbortProcessing()

        @receiver(str)
        def not_aborter(msg):
            self.msg = msg

        self.bus.raise_errors = True
        self.bus.register(aborter)
        self.bus.register(not_aborter, priority=BusPriority.LOW_PRIORITY)

        self.bus.publish("foo")
        assert self.msg == "foo"
        self.msg = None
        self.bus.publish("cancel")
        assert self.msg == None
Esempio n. 7
0
 def test_no_concurrent(self):
     bus = VoomBus()
     bus.loader = functools.partial(self.load, 0)
     self.assertEqual(self.loaded, None)
     bus.publish(0)
     self.assertEqual(self.loaded, 1)
     bus.publish(0)
     self.assertEqual(self.loaded, 1)
Esempio n. 8
0
    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()
Esempio n. 9
0
    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
Esempio n. 10
0
    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()
Esempio n. 11
0
    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
Esempio n. 12
0
class TestDefer(unittest.TestCase):
    def setUp(self):
        self.bus = VoomBus()

    def test1(self):
        @receiver(str)
        def h1(msg):
            self.msgs.append(msg)
            self.bus.defer(1)

        @receiver(int, str)
        def h2(msg):
            self.msgs.append(msg)

        self.msgs = []
        self.bus.register(h1)
        self.bus.register(h2)
        self.bus.publish("s")
        assert self.msgs == ['s', 's', 1]
Esempio n. 13
0
class TestDefer(unittest.TestCase):
    def setUp(self):
        self.bus = VoomBus()

    def test1(self):
        @receiver(str)
        def h1(msg):
            self.msgs.append(msg)
            self.bus.defer(1)

        @receiver(int, str)
        def h2(msg):
            self.msgs.append(msg)

        self.msgs = []
        self.bus.register(h1)
        self.bus.register(h2)
        self.bus.publish("s")
        assert self.msgs == ['s', 's', 1]
Esempio n. 14
0
class TestContextVars(unittest.TestCase):
    def setUp(self):
        self.bus = VoomBus(raise_errors=True)
        self.context2 = 0

    def do1(self, msg):
        self.msg = msg
        self.context = self.bus.message_context['c']
        with self.bus.using(dict(c=self.context + 1), local=True):
            with self.bus.using(dict(c=self.context + 2), local=True):
                self.bus.publish("hello")
            self.bus.publish("hello")

    def do2(self, msg):
        self.msg2 = msg
        self.context2 += self.bus.message_context['c']

    def test_vars(self):
        msg = 1
        self.bus.subscribe(int, self.do1)
        self.bus.subscribe(str, self.do2)
        with self.bus.using(dict(c=100), local=True):
            self.bus.publish(msg)
        assert self.context == 100
        assert self.context2 == 101 + 102
Esempio n. 15
0
class TestSession(unittest.TestCase):
    def setUp(self):
        self.bus = VoomBus()

    def test_1(self):
        session = {}

        @receiver(str)
        def doer1(s):
            self.bus.session[s] = True
            session.update(self.bus.session)

        self.bus.register(doer1)
        with self.bus.using(dict(a=1, b=2)):
            self.bus.publish("meow")
        assert session == dict(a=1, b=2, meow=True)

        session = {}
        self.bus.publish("meow")
        assert session == dict(meow=True)

        @receiver(str)
        def doer2(s):
            if s == "meow":
                self.bus.publish("grr")

        self.bus.register(doer2)
        session = {}
        self.bus.publish("meow")
        assert session == dict(meow=True, grr=True)
Esempio n. 16
0
@receiver(UserDidSomething)
def incrementGlobalActivity(event):
    stats_db[event.thing] += 1
    
@receiver(UserDidSomething, UserDidSomethingElse)
def appendToStream(event):
    activity_db[event.user].append(event)

@receiver(VoomBus.ALL)
def log_it(event):
    print "An event happened, yo:", event

#
# configure the bus
#
bus = VoomBus()
bus.register(incrementUserActivity)
bus.register(incrementGlobalActivity)
bus.register(appendToStream)
bus.register(log_it)

# publish some messages
bus.publish(UserDidSomething("bob", "loggedin", time.time()))
bus.publish(UserDidSomething("bob", "loggedin", time.time()))
bus.publish(UserDidSomethingElse("bob", "viewed_thing", time.time()))

assert len(activity_db["bob"]) == 3 # we see the three activities here
assert user_stats_db["bob"] == {'loggedin': 2} # but counter logic executed for only 2 of them

# publish more messages
bus.publish(UserDidSomething("alice", "loggedin", time.time()))
Esempio n. 17
0
 def setUp(self):
     self.bus = VoomBus()
     self.forward = None
Esempio n. 18
0
    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
Esempio n. 19
0
class TestBusReply(unittest.TestCase):
    def setUp(self):
        self.bus = VoomBus()
        self.forward = None

    def test_errors(self):
        with patch('voom.bus.VoomBus.session', {}):
            nose.tools.assert_raises(InvalidAddressError, self.bus.reply, None) #@UndefinedVariable

        with patch('voom.bus.VoomBus.session', {SessionKeys.REPLY_TO: "badaddr"}):
            assert self.bus.session == {SessionKeys.REPLY_TO: "badaddr"}, self.bus.session
            nose.tools.assert_raises(InvalidStateError, self.bus.reply, None) #@UndefinedVariable

    def test_reply_1(self):
        @receiver(str)
        def what_is_it(_):
            self.bus.reply('ponies')

        @receiver(MessageForwarded)
        def forward(msg):
            self.forward = msg

        self.bus.register(what_is_it)
        self.bus.register(forward)

        with self.bus.using({SessionKeys.REPLY_TO: CurrentThreadChannel.ADDRESS}):
            self.bus.publish("meow")
        assert self.bus.thread_channel.pop_all() == ['ponies']
        assert self.forward
        assert self.forward.message == "ponies", self.forward
        assert self.forward.address == CurrentThreadChannel.ADDRESS

    def test_reply_2(self):
        @receiver(str)
        def what_is_it(_):
            self.bus.reply('ponies')

        self.bus.register(what_is_it)
        self.bus.raise_errors = True

        nose.tools.assert_raises(InvalidAddressError, self.bus.publish, "my little")
Esempio n. 20
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
Esempio n. 21
0
 def setUp(self):
     self.bus = VoomBus()
     self.forward = None
Esempio n. 22
0
 def setUp(self):
     self.bus = VoomBus(raise_errors=True)
     self.context2 = 0
Esempio n. 23
0
class TestBusReply(unittest.TestCase):
    def setUp(self):
        self.bus = VoomBus()
        self.forward = None

    def test_errors(self):
        with patch('voom.bus.VoomBus.session', {}):
            nose.tools.assert_raises(InvalidAddressError, self.bus.reply,
                                     None)  #@UndefinedVariable

        with patch('voom.bus.VoomBus.session',
                   {SessionKeys.REPLY_TO: "badaddr"}):
            assert self.bus.session == {
                SessionKeys.REPLY_TO: "badaddr"
            }, self.bus.session
            nose.tools.assert_raises(InvalidStateError, self.bus.reply,
                                     None)  #@UndefinedVariable

    def test_reply_1(self):
        @receiver(str)
        def what_is_it(_):
            self.bus.reply('ponies')

        @receiver(MessageForwarded)
        def forward(msg):
            self.forward = msg

        self.bus.register(what_is_it)
        self.bus.register(forward)

        with self.bus.using(
            {SessionKeys.REPLY_TO: CurrentThreadChannel.ADDRESS}):
            self.bus.publish("meow")
        assert self.bus.thread_channel.pop_all() == ['ponies']
        assert self.forward
        assert self.forward.message == "ponies", self.forward
        assert self.forward.address == CurrentThreadChannel.ADDRESS

    def test_reply_2(self):
        @receiver(str)
        def what_is_it(_):
            self.bus.reply('ponies')

        self.bus.register(what_is_it)
        self.bus.raise_errors = True

        nose.tools.assert_raises(InvalidAddressError, self.bus.publish,
                                 "my little")
Esempio n. 24
0
 def setUp(self):
     self.bus = VoomBus()
Esempio n. 25
0
class TestSession(unittest.TestCase):
    def setUp(self):
        self.bus = VoomBus()

    def test_1(self):
        session = {}

        @receiver(str)
        def doer1(s):
            self.bus.session[s] = True
            session.update(self.bus.session)

        self.bus.register(doer1)
        with self.bus.using(dict(a=1, b=2)):
            self.bus.publish("meow")
        assert session == dict(a=1, b=2, meow=True)

        session = {}
        self.bus.publish("meow")
        assert session == dict(meow=True)

        @receiver(str)
        def doer2(s):
            if s == "meow":
                self.bus.publish("grr")
        self.bus.register(doer2)
        session = {}
        self.bus.publish("meow")
        assert session == dict(meow=True, grr=True)
Esempio n. 26
0
class TestContextVars(unittest.TestCase):
    def setUp(self):
        self.bus = VoomBus(raise_errors=True)
        self.context = 0
        self.context2 = 0

    def do1(self, msg):
        self.msg = msg
        self.context += self.bus.session['c']
        with self.bus.using(dict(c=self.context + 1)):
            with self.bus.using(dict(c=self.context + 2)):
                self.bus.publish("hello")
            self.bus.publish("hello")

    def do2(self, msg):
        self.msg2 = msg
        self.context2 += self.bus.session['c']

    def test_vars_should_add_up(self):
        msg = 1
        self.bus.subscribe(int, self.do1)
        self.bus.subscribe(str, self.do2)
        with self.bus.using(dict(c=100)):
            self.bus.publish(msg)
        assert self.context == 100
        assert self.context2 == 101 + 102

    def test_vars_should_add_up_twice(self):
        msg = 1
        self.bus.subscribe(int, self.do1)
        self.bus.subscribe(str, self.do2)
        with self.bus.using(dict(c=100)):
            self.bus.publish(msg)
            self.bus.publish(msg)
        self.assertEquals(200, self.context)
        self.assertEquals(606, self.context2)
Esempio n. 27
0
 def setUp(self):
     self.bus = VoomBus()
Esempio n. 28
0
 def setUp(self):
     self.bus = VoomBus(raise_errors=True)
     self.context = 0
     self.context2 = 0
Esempio n. 29
0
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")
Esempio n. 30
0
 def setUp(self):
     self.bus = VoomBus(verbose=True)